ListView 的 SimpleCursorAdapter?
在我的应用程序中,我需要为我的列表视图使用 SimpleCursorAdapter,该列表视图由 string[] 或 ArrayList[] 填充...
我尝试了以下代码:
ArrayList<String> mDisplay = new ArrayList<String>();
Cursor c = (Cursor) mDisplay.iterator();
lv_contactlist.setAdapter(new SimpleCursorAdapter(this, android.R.layout.simple_list_item_checked, c ,new String[]{mDisplay.toString()}, new int[]{android.R.id.text1}));
但显示错误如下:
由以下原因引起:java.lang.ClassCastException:java.util.ArrayList$ArrayListIterator 在 com.kpj4s.way2sms.ContactsList.onCreate(ContactsList.java:62) 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1586)
我认为 Cursor 对象“c”未正确设置,
任何人都可以帮助我编写代码或向我展示替代方案(注意:我只需要使用 SimpleCursorAdapter )
谢谢 :)
in my app i need to use the SimpleCursorAdapter for my listview which is being populated by an string[] or ArrayList[]...
i tried the following code:
ArrayList<String> mDisplay = new ArrayList<String>();
Cursor c = (Cursor) mDisplay.iterator();
lv_contactlist.setAdapter(new SimpleCursorAdapter(this, android.R.layout.simple_list_item_checked, c ,new String[]{mDisplay.toString()}, new int[]{android.R.id.text1}));
but show me error as follows:
Caused by: java.lang.ClassCastException: java.util.ArrayList$ArrayListIterator
at com.kpj4s.way2sms.ContactsList.onCreate(ContactsList.java:62)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1586)
i think that the Cursor object "c" is not properly set
can any one help me with the code or show me the alternative (note: i need to use only SimpleCursorAdapter)
thanks :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Cursor
通常是数据库查询的结果。您不是在查询数据库,并且Iterator
不是Cursor
,因此会出现ClassCastException
。如果您希望查询 ContentProvider 以收集联系信息,请参阅 http://developer.android.com/guide/topics/providers/content-providers.html
如果你真的只想显示你的
ArrayList
尝试ArrayAdapter
:http://developer.android.com /reference/android/widget/ArrayAdapter.htmlA
Cursor
is usually the result of a database query. You are not querying a database and anIterator
is not aCursor
, hence theClassCastException
.If you are hoping to query a ContentProvider to collect the Contact information see the section "Querying a Content Provider" at http://developer.android.com/guide/topics/providers/content-providers.html
If you really only want to display your
ArrayList<String>
tryArrayAdapter
: http://developer.android.com/reference/android/widget/ArrayAdapter.html