Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Android:屏幕旋转后列表视图双重输入大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个在onCreate()中填充的listView,因为在屏幕旋转中再次调用onCreate(),它再次填充它,因此在每次旋转后我得到的条目当然添加了我不想要的内容. onCreate基本上是:

@Override
public void onActivityCreated(Bundle savedInstanceStatE) 
{
    super.onActivityCreated(savedInstanceStatE);
    myList = new ArrayList<SingleEntry>();
    new getList().execute(); //Async task to fill myList

    ListView lv = (ListView) getActivity().findViewById(R.id.ListView01);

    itemAdapter = new ItemAdapterOverview(getActivity().getApplicationContext(),myList);


    lv.setAdapter(itemAdapter);
}

其中myList是ArrayList和类变量.我在onCreate()尝试填充之前设置了一个null适配器,这就是Google向我建议的.但它没有用.

解决方法

您可以将myList保存在Android的方法onSaveInstanceState中,如下所示:

protected void onSaveInstanceState(Bundle bundlE) {
    bundle.putserializable("myList",myList);
    super.onSaveInstanceState(bundlE);
}

只需确保通过使SingleEntry实现Serializable interface来使类SingleEntry可序列化(注意:如果在SingleEntry类中有任何复杂的数据结构,您还应该使它们实现serializable接口).然后在你的onCreate中你可以使用这样的东西:

@Override
public void onActivityCreated(Bundle savedInstanceStatE) 
{
    super.onActivityCreated(savedInstanceStatE);

    if(savedInstanceState != null) { //check if the save instance state is not null

       //If is not null,retrieve the saved values of the myList variable
       myList = (ArrayList<SingleEntry>) savedInstanceState.getserializable("myList");

       ListView lv = (ListView) getActivity().findViewById(R.id.ListView01);

       itemAdapter = new ItemAdapterOverview(getActivity().getApplicationContext(),myList);

       lv.setAdapter(itemAdapter);
    }
    else { //Bundle is empty so you should intialize the myList variable
       myList = new ArrayList<SingleEntry>();
       new getList().execute(); //Async task to fill myList

       ListView lv = (ListView) getActivity().findViewById(R.id.ListView01);

       itemAdapter = new ItemAdapterOverview(getActivity().getApplicationContext(),myList);


       lv.setAdapter(itemAdapter);
    }
}

大佬总结

以上是大佬教程为你收集整理的Android:屏幕旋转后列表视图双重输入全部内容,希望文章能够帮你解决Android:屏幕旋转后列表视图双重输入所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。