带 CursorAdapter 的 ListView 不显示我的值
我想通过滚动功能向用户显示数据库的一些内容。
所以我扩展了ListActivity并使用了SimpleCursorAdapter。问题是我看不到被替换的值。
private void fillData() {
// Get all of the rows from the database and create the item list
ListData = data.findAllDbValues();
startManagingCursor(ListData);
// Create an array to specify the fields we want to display in the list
String[] from = new String[]{Data.ID, Data.DESC};
// and an array of the fields we want to bind those fields to
int[] to = new int[]{R.id.text1, R.id.text2};
// Now create a simple cursor adapter and set it to display
ListAdapter la = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, ListData, from, to);//R.layout.showdata
setListAdapter(la);
}
如果我使用自定义布局(R.layout.showdata),它只有两个 TextView,我可以看到这些值,但我必须触摸显示的文本才能完成我的工作,而不是只触摸所选内容的任何位置。
所以我想使用 simple_list_item_2 这样我就可以拥有这种能力。有什么想法为什么我看不到这些值或者如何使我的自定义布局产生与 simple_list 相同的功能?
I want to display some of the contents of my database to the user with a scroll ability.
So I have extended the ListActivity and I used the SimpleCursorAdapter. The problem is that i can not see the values displaced.
private void fillData() {
// Get all of the rows from the database and create the item list
ListData = data.findAllDbValues();
startManagingCursor(ListData);
// Create an array to specify the fields we want to display in the list
String[] from = new String[]{Data.ID, Data.DESC};
// and an array of the fields we want to bind those fields to
int[] to = new int[]{R.id.text1, R.id.text2};
// Now create a simple cursor adapter and set it to display
ListAdapter la = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, ListData, from, to);//R.layout.showdata
setListAdapter(la);
}
If I use a custom layout(R.layout.showdata),where it has only two TextViews, I can see the values but I have to touch the text which is displayed to do my job instead of just touching anywhere of the selection.
So I want to use a simple_list_item_2 so I can have this ability. Any ideas why I do not see the values or how can I make my custom layout to produce the same abilities with the simple_list?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这对我有用:
技巧是将
TextView
行包装在LinearLayout
中。这让我可以单击列表元素上的任意位置来选择它。This worked for me:
The trick is to wrap the
TextView
row in aLinearLayout
. This lets me click anywhere on the list element to select it.我知道这个问题很老了,但我通过谷歌来到这里,我想我有了答案,因为我遇到了类似的问题。
您正在
int[] to
数组中引用自定义TextView
,而不是 Android 数组。您必须改为使用:只需检查 XML 中的 id 和类中的 id 是否相同。当您使用自定义视图时,类中的引用是
R.id.text1
和布局中的android:id="@+id:text1"
但当您使用 Android 资源时,类中的引用是android .R.id.text1
和您的布局android:id="@android:id/text1"
中。您可以使用其中任何一个,但必须保持一致。
我在 XML 中使用
android:id="@+id:text1"
(创建一个 ID 为“text1”的新 TextView),但使用android.R.id.text1
> 在我的 Java 类中,所以CursorAdaptor
在一个不存在的视图中设置信息,我得到的正是这个结果:一个列表视图,其中所有行都没有文本。I know this question is old but I arrived here through Google and I think I have the answer since I had similar problem.
You are referencing custom
TextView
in yourint[] to
array, not the Android ones. You must use instead:Just check out that the ids in the XML and the ids in your class are the same. When you use custom views the reference in your class is
R.id.text1
and in your layoutandroid:id="@+id:text1"
but when you use Android resources the reference in your class isandroid.R.id.text1
and in your layoutandroid:id="@android:id/text1"
.You can use any of them but it must be consistent.
I was using
android:id="@+id:text1"
in my XML (creating a new TextView with id "text1") but usingandroid.R.id.text1
in my Java Class so theCursorAdaptor
was setting the information in a non-existent View and I was getting exactly that result: a listview with all the rows without text.