gpt4 book ai didi

android - 复用同一个ListView展示不同的数据

转载 作者:搜寻专家 更新时间:2023-11-01 08:10:39 25 4
gpt4 key购买 nike

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;
}
};

});

最佳答案

您是否考虑过使用 BaseAdapter 并将其设置为列表适配器 http://developer.android.com/reference/android/widget/BaseAdapter.html

关于android - 复用同一个ListView展示不同的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9967245/

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