动态列表视图保存状态

时间:2016-02-16 09:08:42

标签: android listview

我正在开发洗衣应用程序。我有这样的listviewFragment),因此用户可以选择项目。

enter image description here

我的问题是当我点击继续然后我再次回到此屏幕时它的值变为零。如何保存listview状态。

我在片段中尝试了这段代码,但没有运气。并且还尝试保存和恢复实例状态方法但面临同样的问题。

// Save the ListView state (= includes scroll position) as a Parceble
Parcelable state = listView.onSaveInstanceState();

// e.g. set new items
listView.setAdapter(adapter);

// Restore previous state (including selected item index and scroll position)
listView.onRestoreInstanceState(state);

ManFragment.java

package info.tranetech.laundry.activity;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

import info.tranetech.laundry.R;
import info.tranetech.laundry.adapter.JSONParser;


public class ManFragment extends Fragment {

    private View rootView;
    //Dry
    public static String TAG_ITEM_MAN = "name";
    public static String TAG_PRICE_DRY = "drycleaning_price";
    public static String TAG_PRICE_WASH_IRON = "washiron_price";
    public static String TAG_PRICE_WASH = "wash_price";
    public static String TAG_PRICE_IRON = "iron_price";
    public static String Name, Price_Dry, price_wash_iron, price_wash, price_iron;
    public static String url_dry = "http://openspace.tranetech.com/mis/Laundry/men_dry.php";
    static TextView ItemName, ItemPrice;
    public static int Cat_Position;
    ArrayList<HashMap<String, String>> DryList = new ArrayList<HashMap<String, String>>();
    ArrayList<HashMap<String, String>> WashList = new ArrayList<HashMap<String, String>>();
    ArrayList<HashMap<String, String>> WashIronList = new ArrayList<HashMap<String, String>>();
    ArrayList<HashMap<String, String>> IronList = new ArrayList<HashMap<String, String>>();


    ListView list;
    ProgressDialog pdialog;

    static Spinner Spin_Man;

    public ManFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        rootView = inflater.inflate(R.layout.man_pricelist, container, false);
        // Locate the listview in listview_main.xml
        list = (ListView) rootView.findViewById(R.id.man_listView);
        Spin_Man = (Spinner) rootView.findViewById(R.id.spinner_man);

        new DryData().execute();

        Spin_Man.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {

                try {


                    switch (position) {
                        case 0:

                            Cat_Position = position;
                            Filllist();
                            getView();

                            break;
                        case 1:

                            Cat_Position = position;
                            Filllist();
                            getView();

                            break;
                        case 2:

                            Cat_Position = position;
                            Filllist();
                            getView();

                            break;
                        case 3:

                            Cat_Position = position;
                            Filllist();
                            getView();

                            break;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

                Toast.makeText(getActivity(), "Position " + position, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parentView) {
                // your code here
            }

        });

        return rootView;
    }


    private class DryData extends AsyncTask<Void, Void, Void> {
        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
            pdialog = new ProgressDialog(getActivity());
            pdialog.setMessage("Please Wait...");
            pdialog.setCancelable(true);
            pdialog.show();
        }

        @Override
        protected Void doInBackground(Void... arg0) {
            // TODO Auto-generated method stub
            JSONParser jp = new JSONParser();
            String jsonStr = jp.makeServiceCall(url_dry, JSONParser.GET);
            Log.d("Response: ", "> " + jsonStr);
            if (jsonStr != null) {
                try {
                    JSONObject jobj = new JSONObject(jsonStr);
                    JSONArray jarray = jobj.getJSONArray("Data");

                    for (int i = 0; i < jarray.length(); i++) {
                        JSONObject jobjin = jarray.getJSONObject(i);

                        String name = jobjin.getString(TAG_ITEM_MAN);
                        String price_dry = jobjin.getString(TAG_PRICE_DRY);
                        String price_wash_iron = jobjin.getString(TAG_PRICE_WASH_IRON);
                        String price_wash = jobjin.getString(TAG_PRICE_WASH);
                        String price_iron = jobjin.getString(TAG_PRICE_IRON);

                        HashMap<String, String> add_dry = new HashMap<String, String>();
                        add_dry.put(TAG_ITEM_MAN, name);
                        add_dry.put(TAG_PRICE_DRY, price_dry);
                        HashMap<String, String> add_wash_iron = new HashMap<String, String>();
                        add_wash_iron.put(TAG_ITEM_MAN, name);
                        add_wash_iron.put(TAG_PRICE_WASH_IRON, price_wash_iron);
                        HashMap<String, String> add_wash = new HashMap<String, String>();
                        add_wash_iron.put(TAG_ITEM_MAN, name);
                        add_wash.put(TAG_PRICE_WASH, price_wash);
                        HashMap<String, String> add_iron = new HashMap<String, String>();
                        add_iron.put(TAG_ITEM_MAN, name);
                        add_iron.put(TAG_PRICE_IRON, price_iron);

                        DryList.add(add_dry);
                        WashList.add(add_wash);
                        WashIronList.add(add_wash_iron);
                        IronList.add(add_iron);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    // TODO: handle exception
                }
            } else {
                Log.e("ServiceHandler", "Couldn't get any data from the url_dry");

            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            if (pdialog.isShowing()) {
                pdialog.dismiss();
            }

            Filllist();
        }

    }

    public void Filllist() {
        // TODO Auto-generated method stub
        Cus adapter = new Cus();
        list.setAdapter(adapter);
//        Helper.getListViewSize(list);
    }

    private class Cus extends BaseAdapter {

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return DryList.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.price_row, null);
            }
            ItemName = (TextView) convertView.findViewById(R.id.item_name);
            ItemPrice = (TextView) convertView.findViewById(R.id.price);

            Name = DryList.get(position).get(TAG_ITEM_MAN).toString();
            Price_Dry = DryList.get(position).get(TAG_PRICE_DRY).toString();
            price_wash_iron = WashIronList.get(position).get(TAG_PRICE_WASH_IRON).toString();
            price_wash = WashList.get(position).get(TAG_PRICE_WASH).toString();
            price_iron = IronList.get(position).get(TAG_PRICE_IRON).toString();


            if (Cat_Position == 0) {
                //set item name
                ItemName.setText("" + Name);
                ItemPrice.setText("Rs." + Price_Dry);
            } else if (Cat_Position == 1) {
                //set item name
                ItemName.setText("" + Name);
                ItemPrice.setText("Rs." + price_iron);
            } else if (Cat_Position == 2) {
                //set item name
                ItemName.setText("" + Name);
                ItemPrice.setText("Rs." + price_wash);
            } else if (Cat_Position == 3) {
                //set item name
                ItemName.setText("" + Name);
                ItemPrice.setText("Rs." + price_wash_iron);
            }

            return convertView;
        }

    }

}

1 个答案:

答案 0 :(得分:1)

全局定义Arraylist并将webservices称为:

data