在 android 列表视图中,当我返回时,数据会多次获取,我该如何消除它?
我有 2 个视图:
- Listview
- 列表项的详细信息
当我首先运行我的应用程序时,它将显示 ListView
然后当我选择 ListView
中的项目将进入下一个称为详细信息的视图 列表项。现在,当我返回到 ListView
时, 列表出现多次。我该如何解决这个问题?
谢谢
在下面显示的方式中,我将数据添加到 ArrayAdapter。
MyArrayAdapter adapter = new MyArrayAdapter(SampleActivity.this, R.layout.citieslist, scity);
lv.setAdapter(adapter);
lv.setTextFilterEnabled(true);
MyArrayAdapter 如下...
private class MyArrayAdapter extends BaseAdapter {
String[] items;
public MyArrayAdapter(Context context, int
textViewResourceId, String[] items) {
this.items = items;
}
@Override
public int getCount() {
return items.length;
}
@Override
public Object getItem(int position) {
return
position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.citieslist, null);
}
TextView tv = (TextView) v.findViewById(R.id.tvCity);
tv.setText(items[position]);
return v;
}
}
I'm having 2 views:
- Listview
- Details of list item
When I run my app first it'll show ListView
then when I selected the
item from ListView
it'll enter into the next view called details of
list item. Now when I'm going back to the ListView
the items of the
list are appearing multiple times. How can I solve this problem?
Thanks
In the below shown way I'm adding data to ArrayAdapter.
MyArrayAdapter adapter = new MyArrayAdapter(SampleActivity.this, R.layout.citieslist, scity);
lv.setAdapter(adapter);
lv.setTextFilterEnabled(true);
MyArrayAdapter is as follows...
private class MyArrayAdapter extends BaseAdapter {
String[] items;
public MyArrayAdapter(Context context, int
textViewResourceId, String[] items) {
this.items = items;
}
@Override
public int getCount() {
return items.length;
}
@Override
public Object getItem(int position) {
return
position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.citieslist, null);
}
TextView tv = (TextView) v.findViewById(R.id.tvCity);
tv.setText(items[position]);
return v;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会尝试 3 件事:
将适配器的 getView() 方法的 inflate 行更改为:
I'd try 3 things:
Change the inflate line of your getView() method of the adapter to:
有两种可能,
一:
当你从详细视图返回到列表视图页面时,
如果你使用“startActivity(intent)”,则替换为“finish()”。
二:
在“onCreateView()”中初始化列表视图“scity”
There is two possibilities,
one:
when you go back from detail view to listview page,
if you use "startActivity(intent)", replace with "finish()".
two:
initialize listview "scity" in "onCreateView()"