如何正确调用setListAdapter [英] how to correctly call setListAdapter
问题描述
我已经搜索了很多解决此问题的方法,但是没有得到任何结果.我唯一需要的帮助是,请看一下关于stackoverflow和答案的问题;
I've searched how to solve this problem a lot, but i did not get any result. the only help that i need is, please take a look at this question asked on stackoverflow and on answer;
这是来自此链接的问题:
我的活动未扩展ListActivity,因此此行引发错误.setListAdapter(fileList);应该填充列表视图..."
"my activity does not extend ListActivity so this line throws an error. setListAdapter(fileList); it is supposed to populate a listview..."
ArrayAdapter<String> fileList =
new ArrayAdapter<String>(this, R.layout.row, item);
setListAdapter(fileList);
这个问题的答案是:
使用 findViewById(int)
获取 ListView
的引用.然后,使用您的适配器作为参数,在该引用上调用方法 ListView.setAdapter(ListAdapter)
.
Obtain the reference of your ListView
using findViewById(int)
. Then call the method ListView.setAdapter(ListAdapter)
on that reference with your adapter as the parameter.
现在这是我的代码:
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(this, R.layout.reminder_row, R.id.text1, items);
setListAdapter(adapter);
}
考虑到问/答,有人可以解释我如何正确地正确编写它.任何帮助将不胜感激.
considering the Q/A, could someone please explain how exactly do i write it in the correct way. Any help would be very much appreciated.
推荐答案
如链接的问题和该问题的注释中所述,它是这样的:
As explained in the linked question and in this question's comments, it goes like this:
ArrayAdapter<String> adapter =new ArrayAdapter<String>(this, R.layout.reminder_row, R.id.text1, items);
ListView lv = (ListView)findViewById(R.id.your_listview_id);
lv.setAdapter(adapter);
ListActivity
是专用的 Activity
,它承载一个 ListView
,并具有一些访问和操作它的便捷方法.但是,没有什么能阻止您在非 ListActivity
Activity
中使用 ListView
,您只是失去了便捷方法而不得不编写更多内容像上面那样编写自己的代码.
ListActivity
is a specialized Activity
that hosts a ListView
and has some convenience methods for accessing and manipulating it. However, nothing prevents you from having a ListView
in a non-ListActivity
Activity
, you just lose the convenience methods and will have to write some more code yourself, like above.
这篇关于如何正确调用setListAdapter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!