在 android 列表视图中,当我返回时,数据会多次获取,我该如何消除它?

发布于 2024-12-09 00:43:27 字数 1412 浏览 0 评论 0原文

我有 2 个视图:

  1. Listview
  2. 列表项的详细信息

当我首先运行我的应用程序时,它将显示 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:

  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 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

信仰 2024-12-16 00:43:27

我会尝试 3 件事:

  1. 删除 setTextFilterEnabled 行。除非你专门做一些我不知道的过滤事情。
  2. 您正在传递上下文但不保存它。将其保存到类变量中并将其用于#3。
  3. 将适配器的 getView() 方法的 inflate 行更改为:

    if (v == null) {                
        v = LayoutInflater.from(context).inflate(R.layout.citieslist,parent,false);
    }
    

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);
    }
    
捶死心动 2024-12-16 00:43:27

有两种可能,

一:

当你从详细视图返回到列表视图页面时,

如果你使用“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()"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文