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 ge开发者_开发知识库tItemId(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'd try 3 things:
- Remove the setTextFilterEnabled line. Unless you're doing something specifically with the filtering that I don't know about.
- You're passing in the context but not saving it. Save it to a class variable and use it for #3.
Change the inflate line of your getView() method of the adapter to:
if (v == null) { v = LayoutInflater.from(context).inflate(R.layout.citieslist, parent, false); }
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()"
精彩评论