调试SimpleCursorAdapter [英] Debugging SimpleCursorAdapter

查看:108
本文介绍了调试SimpleCursorAdapter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作我的第一个Android应用程序,并无法弄清楚如何让我的 SimpleCursorAdpater 来填充视图。说我传递光标在它的结果,所以这个问题必须以实例化适配器或绑定到视图某处。我是那种在我束手无策,因为没有引发异常,我真的不能一步
到setListAdapter。

I'm working on my first Android app and can't figure out how to get my SimpleCursorAdpater to populate the view. The cursor that I'm passing in has results in it, so the problem must be somewhere in instantiating the adapter or in binding it to the view. I'm sort of at my wits end since no exceptions are thrown and I can't really step into setListAdapter.

下面是如何让我的光标在首位:

Here is how i get my cursor in the first place:

    Searches searches = new Searches(this);

    SQLiteDatabase db = searches.getReadableDatabase();
    //select _id, Name, Search FROM Searches;
    Cursor c = db.query(
                SearchConstants.TABLE_NAME, 
                FROM, null, null, null, 
                null, null);
    startManagingCursor(c);

这是架构做我的DB:

And this is the schema do my db:

CREATE TABLE Searches (_id INTEGER PRIMARY KEY, Name Text, Search TEXT)

下面是两行事情开始土崩瓦解:

Here are the two lines where things start to fall apart:

SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.search, cursor, FROM, TO);
setListAdapter(adapter);

我主要布局是这样的:

My main layout looks like this:

<ListView
    android:id="@android:id/android:list"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
<TextView
    android:id="@android:id/android:empty"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/empty" />

下面是填补与每个结果的看法:

Here is the view to fill with each result:

<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:orientation="horizontal"
  android:padding="10sp">
  <TextView
    android:id="@+id/_id"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
  <TextView
    android:id="@+id/name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
  <TextView
    android:id="@+id/colon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=": "
    android:layout_toRightOf="@id/name" />
  <TextView
    android:id="@+id/search"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:ellipsize="end"
    android:singleLine="true"
    android:textStyle="italic"
    android:layout_toRightOf="@id/colon" />
</RelativeLayout>

最后,这里我使用的静态变量:

Finally here are the static variables I used:

//search query stuff
private static String[] FROM = {SearchConstants._ID, SearchConstants.NAME_COLUMN, SearchConstants.SEARCH_COLUMN};

//where to paste search results
private static int[] TO = {R.id._id, R.id.name, R.id.search};

/**
 * Table name
 */
public static final String TABLE_NAME = "Searches";

/**
 * Name Column
 */
public static final String NAME_COLUMN = "Name";

/**
 * Search Column
 */
public static final String SEARCH_COLUMN = "Search";

我想这是所有相关的code的。我不知道如何在这一点上进行,因此在所有的任何建议将是有益的。

I think this is all of the relevant code. I have no idea how to proceed at this point, so any suggestions at all would be helpful.

谢谢,
布赖恩

Thanks, brian

PS:这里看起来孤单了很多很好的建议 - 我不是不理他们,我只是还没有有机会呢。感谢您的建议!在某些时候我会去直通所有这些,尽量给一些反馈,哪些东西对我来说效果不错。

PS: Looks like theres a lot of great suggestions here - i'm not ignoring them i just havent had the chance yet. Thanks for the advice! At some point i'll go thru them all and try to give some feedback as to which things worked well for me.

推荐答案

您可以踏进code,如果你有源$ C ​​$ C。幸运的是,Android是开源的。可轻松连接源$ C ​​$ C在Eclipse中看到:

You can step into the code if you have the source code. Luckily, Android is open source. To easily attach source code in Eclipse, see:

http://android.opensourceror.org/2010/01/18 / Android的源/

至于问题本身,您在上面您在创建适配器之前遍历所有项目的注释说。如果你没有创建迭代之后一个新的光标,你可能需要倒带或适配器可能会认为它是空的。

As for the problem itself, you said in a comment above that you iterate all of the items before creating the adapter. If you are not creating a new cursor after iteration, you probably need to rewind it or the adapter might think it's empty.

cursor.moveToFirst()

这篇关于调试SimpleCursorAdapter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆