Listview删除屏幕方向旋转的数据

时间:2014-02-27 20:06:57

标签: android listview dynamically-generated

所以我在一个片段中有一个ListView,当屏幕旋转时,ListView中的数据消失了。

关于为什么ListView可能会这样做的任何想法?这是片段类的一些代码。

    package com.example.ioiocontroller;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class digitalFragment extends Fragment {
    ArrayList<digitalControlObject> digitalList;
    ListView digitalListView;
    View digitalFragmentView;
    Button addDigital;
    Button removeDigital;
    MyAdapter adapter;

    public digitalFragment() {
        // TODO Auto-generated constructor stub
    }

    public View onCreateView(LayoutInflater viewInflation, ViewGroup container,
            Bundle SavedInstantState) {
        digitalList = new ArrayList<digitalControlObject>();
        digitalFragmentView = viewInflation.inflate(
                R.layout.digitalfragment_page, container, false);

        addDigital = (Button) digitalFragmentView
                .findViewById(R.id.digitalAddition);
        removeDigital = (Button) digitalFragmentView
                .findViewById(R.id.digitalRemoval);
        digitalListView = (ListView) digitalFragmentView
                .findViewById(R.id.digitalListView);
        adapter = new MyAdapter(getActivity(), R.id.digitalListView, digitalList);
        digitalListView.setAdapter(adapter);

        // ADDITION CLICK METHOD FOR DIGITAL IO
        addDigital.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                digitalList.add(new digitalControlObject(0,true,true,0));

                adapter.notifyDataSetChanged();
                // create and pass the digitalControlObject to the main activity

            }
        });

        // REMOVAL CLICK METHOD FOR DIGITAL IO
        removeDigital.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

            }
        });

        return digitalFragmentView;
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        adapter = new MyAdapter(getActivity(), R.id.digitalListView, digitalList);
        digitalListView.setAdapter(adapter);
        adapter.notifyDataSetChanged();

    }

    public ListView getDigitalListView() {
        return digitalListView;
    }

    public void setDigitalListView(ListView digitalListView) {
        this.digitalListView = digitalListView;
    }

    public View getDigitalFragmentView() {
        return digitalFragmentView;
    }

    public void setDigitalFragmentView(View digitalFragmentView) {
        this.digitalFragmentView = digitalFragmentView;
    }
//////////////////////////////////////////////////////////////////////////////////
    public class MyAdapter extends ArrayAdapter<digitalControlObject> {

        private Context context;

        public MyAdapter (Context context, int textViewResourceId, List<digitalControlObject> items) {
            super(context, textViewResourceId, items);
            this.context = context;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            View view = convertView;
            if (view == null) {
                LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                view = inflater.inflate(R.layout.digitallistview_layout, null);
            }

            digitalControlObject item = getItem(position);
            if (item != null) {
                TextView itemCol1View = (TextView) view.findViewById(R.id.digcol1);
                TextView itemCol2View = (TextView) view.findViewById(R.id.digcol2);
                TextView itemCol3View = (TextView) view.findViewById(R.id.digcol3);
                //TextView itemCol4View = (TextView) view.findViewById(R.id.digcol4);
                if (itemCol1View != null) {
                    itemCol1View.setText(String.valueOf(item.getPin()));
                }
                if (itemCol2View != null && item.getPinMode()) {
                    itemCol2View.setText("Output");
                }else{
                    itemCol2View.setText("Input");
                }
                if (itemCol3View != null && item.getPowerState()) {
                    itemCol3View.setText("On");
                }else{
                    itemCol3View.setText("Off");
                }
             }

            return view;
        }
    }

}

修改

public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        // Get a layout inflater (inflater from getActivity() or
        // getSupportActivity() works as well)
        LayoutInflater inflater = (LayoutInflater) getActivity()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View newView = inflater.inflate(R.layout.digitalfragment_page, null);
        // This just inflates the view but doesn't add it to any thing.
        // You need to add it to the root view of the fragment
        ViewGroup rootView = (ViewGroup) getView();
        // Remove all the existing views from the root view.
        // This is also a good place to recycle any resources you won't need
        // anymore
        rootView.removeAllViews();
        rootView.addView(newView);
        // Voila, you have the new view setup
    }

2 个答案:

答案 0 :(得分:2)

当旋转屏幕时,Android会重新创建您的片段,因此您必须保存其状态。 在您的情况下,在onSaveInstanceState()方法中保存digitalList列表元素,并在onCreate(Bundle savedInstanceState)期间将它们添加到适配器。这是一个很好的例子:https://stackoverflow.com/a/16819087/1018109

答案 1 :(得分:1)

来自here

  

在某些特殊情况下,您可能希望绕过重新启动   基于一种或多种配置更改的活动。这是   在其清单中使用android:configChanges属性完成。任何   你会说你在那里处理的配置改变类型   接到你当前活动的电话   onConfigurationChanged(配置)方法而不是   重新启动。如果配置更改涉及任何您不这样做   但是,处理活动仍将重新启动   不会调用onConfigurationChanged(配置)。

这意味着您需要修改Android Manifest以便调用onConfigurationChanged

编辑:
here

将这样的内容添加到清单中的Activity:

android:configChanges="orientation"