片段listView返回时加倍

发布于 2025-02-01 19:39:27 字数 1732 浏览 3 评论 0原文

我在片段中为我的listView使用自定义适配器。但是,当我转到第二片片段并按下后键返回时,我的列表会加倍(我的ArrayList中的所有内容都会再次添加到ListView中)。每当我转到第二个片段并回来时,都会发生这种情况(阵列列表再次附加到后面)。我尝试检查适配器是否为空,但是当您回到片段时,它总是始终是零的。

我尝试将代码从本文[https://stackoverflow.com/questions/21446361/android-listview--重复的人when-i-launch-a new-activity and press-back],但这并不能解决我的代码:(

此外,我尝试添加arr.clear() and notifyall()onpause中,这只是打破我的代码(不能过渡到第二片)

public void onStart() {
        super.onStart();
        for (int i = 0; i < device_names.length; i++) {
            arr.add(device_names[i]);
        }

        final ListView list = (ListView)getView().findViewById(R.id.list);
        CustomListView customAdapter = new CustomListView(context, arr);
        if (list.getAdapter() == null) {
            Log.i("SAKEGA", String.valueOf(list.getAdapter()));
            Log.i("SAKEGA", "it's null");
        } else {
            Log.i("SAKEGA", "it's not null waaaaaaaaaah");
            list.setAdapter(null);
        }
        list.setAdapter(customAdapter);
        Log.i("SAKEGA", "HMPH");
        Button tv = (Button)getView().findViewById(R.id.add_devices_text);
        ImageButton add = (ImageButton) getView().findViewById(R.id.add);
        tv.setOnClickListener(add_device_click); // this click listener calls the 2nd fragment
        add.setOnClickListener(add_device);
    }

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_scanneractivity, null);
    return root;
}

。喜欢做的是,一旦我们回到片段,我就清除了listView并使用arraylist重新填充listView。

I use a custom adapter for my ListView in a fragment. When I go to a second fragment and press the back key to go back, though, my list doubles (everything from my ArrayList is added again to the listView). This happens everytime I go to my second fragment and come back (the ArrayList gets appended to the back again). I tried checking if the adapter is null, but apparently it's always null when you go back to the fragment.

I tried moving my code into the onResume() and the onStart() functions from this post [https://stackoverflow.com/questions/21446361/android-listview-duplicates-itself-when-i-launch-a-new-activity-and-press-back], but that doesn't fix my code :(

In addition, I tried adding arr.clear() and notifyAll() in onPause but that just breaks my code (cannot transition into second fragment). Same with onStop()

public void onStart() {
        super.onStart();
        for (int i = 0; i < device_names.length; i++) {
            arr.add(device_names[i]);
        }

        final ListView list = (ListView)getView().findViewById(R.id.list);
        CustomListView customAdapter = new CustomListView(context, arr);
        if (list.getAdapter() == null) {
            Log.i("SAKEGA", String.valueOf(list.getAdapter()));
            Log.i("SAKEGA", "it's null");
        } else {
            Log.i("SAKEGA", "it's not null waaaaaaaaaah");
            list.setAdapter(null);
        }
        list.setAdapter(customAdapter);
        Log.i("SAKEGA", "HMPH");
        Button tv = (Button)getView().findViewById(R.id.add_devices_text);
        ImageButton add = (ImageButton) getView().findViewById(R.id.add);
        tv.setOnClickListener(add_device_click); // this click listener calls the 2nd fragment
        add.setOnClickListener(add_device);
    }

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_scanneractivity, null);
    return root;
}

What I would like to do is, once we go back to the fragment, I clear the ListView and repopulate the listView with the ArrayList.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

你怎么敢 2025-02-08 19:39:27

尝试onstart()本身中创建一个空数组列表在您在那里编写代码时。您正在做的是您坚持那个数组,每当您卷土重来再次加载您的数组

Try to create an empty array list in the onStart() itself as you wrote your code there. what you are doing is you are persisting that array and every time you comeback its loading your array again making the entries again

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