开发者

In android listview data is getting multiple times when i'm going back how can i eleminate it?

开发者 https://www.devze.com 2023-04-12 10:35 出处:网络
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

I'm having 2 views:

  1. Listview
  2. 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:

  1. Remove the setTextFilterEnabled line. Unless you're doing something specifically with the filtering that I don't know about.
  2. You're passing in the context but not saving it. Save it to a class variable and use it for #3.
  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()"

0

精彩评论

暂无评论...
验证码 换一张
取 消