gpt4 book ai didi

android - SimpleCursorAdapter 不会填充我的 ListView,没有错误,只是什么也没发生

转载 作者:行者123 更新时间:2023-11-29 15:23:59 25 4
gpt4 key购买 nike

我试图从游标填充 listview,所以我使用了这段代码:

Cursor curVal = db.rawQuery("SELECT * FROM category ORDER BY order_cat ASC", null);

String[] inVal = new String[] { "nume_cat", "order_cat" };
int[] outVal = new int[] { R.id.title_cat, R.id.duration };

SimpleCursorAdapter cadapter = new SimpleCursorAdapter(this, R.layout.list_row, curVal, inVal, outVal);

ListView listView = (ListView) findViewById(R.id.mainListView);
listView.setAdapter(cadapter);
listView.setOnItemClickListener(this);

这是我的 XML:

List_row.xml

......................

<TextView
android:id="@+id/title_cat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/thumbnail"
android:layout_toRightOf="@+id/thumbnail"
android:text="FILL CAT NAME HERE"
android:textColor="#040404"
android:typeface="sans"
android:textSize="15dip"
android:textStyle="bold"/>

<TextView
android:id="@+id/duration"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/title_cat"
android:textColor="#343434"
android:textSize="10dip"
android:layout_marginTop="1dip"
android:layout_toRightOf="@+id/thumbnail"
android:text="FILL DURATION HERE" />

.....................

ma​​in.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView android:id="@+id/title_main"
android:text="TITLE HERE"
android:padding="15dip"
android:textSize="18dip"
android:textStyle="bold"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>


<ListView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="15dip"
android:id="@+android:id/mainListView">
</ListView>

</LinearLayout>

问题:当我运行该应用程序时,什么也没有发生,我也看不到我的 ListView 。 Eclipse 不会在 logcat 中返回任何错误或警告...

我尝试了简单的方法,使用 ArrayAdapter 并且一切正常,但我需要使用 cursoradapter,因为我需要在每个列表项上有超过 1 个 TextView 。

这是工作的 ArrayAdapter:

    ArrayList<String> catList = new ArrayList<String>();
ListView listView = (ListView) findViewById( R.id.mainListView );
ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this, R.layout.list_row, R.id.title_cat, catList);
Cursor curVal = db.rawQuery("SELECT * FROM category ORDER BY order_cat ASC", null);
if (curVal.moveToFirst()) {
do {
String data = curVal.getString(curVal.getColumnIndex("nume_cat"));
listAdapter.add(data);
} while (curVal.moveToNext());
}
listView.setAdapter(listAdapter);
listView.setOnItemClickListener(this);

现在让我们排除任何问题:

,我的光标包含一个 _id 字段。

是的,我仔细检查了我的光标,它有 20 行。

System.out.println("Cursor adapter has " + cadapter.getCount() + " lines.");  

打印“游标适配器有 18 行。

可能来自:

    int[] outVal = new int[] { R.id.title_cat, R.id.duration  };

and

SimpleCursorAdapter cadapter = new SimpleCursorAdapter(this, R.layout.list_row, curVal, inVal, outVal);

NEED TO REPLACE WITH ?

int[] outVal = new int[] { android.R.id.title_cat, android.R.id.duration };

and

SimpleCursorAdapter cadapter = new SimpleCursorAdapter(this, android.R.layout.list_row, curVal, inVal, outVal);

但是我如何将 ID 定义为 android.R.id.title_cat 而不是 R.id.title_cat?如果我放入我的 xml

    <TextView
android:id="@+android:id/title_cat"

在我的类文件中,eclipse 返回错误“title_cat 无法解析或不是字段

最佳答案

我认为您在 XMl 方面有问题。检查您的 ListView 的 ID。android:id="@+android:id/mainListView">

关于android - SimpleCursorAdapter 不会填充我的 ListView,没有错误,只是什么也没发生,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14950433/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com