【发布时间】:2012-04-01 18:53:26
【问题描述】:
ListViews 一直是我的弱点,现在我正在练习将Listview 放在Listview 中。无论如何,我首先在我的程序开始时调用我的ListView,它会使用保存在我的strings.xml 中的数组加载它:
String[] departments = getResources().getStringArray(
R.array.departments_array);
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item,
departments));
setContentView(R.layout.main);
ListView lv = getListView();
lv.setTextFilterEnabled(true);
我想要做的是每次单击列表项时使用新的值数组更新此ListView。我尝试这样做的原因是因为我计划为每个位置设置 27 个具有不同值的不同数组,并且我觉得如果不是为每个项目数组创建 ListView,我的资源会更轻,我会更新这个ListView。我知道我可能不是这样做最有效的方式,但如果有另一种实现我的想法的方式,请告诉我。
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
switch (position) {
case 0:
try {
//It is here that i dont know what to do, I was going to call
//the Listview the same way i did previously using my setlistadapter,
//but i kept getting errors about the code being undefined
String[] listitems1 = getResources().getStringArray(
R.array.items_array);
} catch (ClassCastException e) {
Toast.makeText(getApplicationContext(), "Error",
Toast.LENGTH_SHORT).show();
}
break;
case 1:
try {
//The listview will be changed again here
} catch (ClassCastException e) {
Toast.makeText(getApplicationContext(), "Error",
Toast.LENGTH_SHORT).show();
}
break;
}
};
});
【问题讨论】:
标签: android android-listview adapter listadapter