在android上本地保存hashmaps

时间:2014-03-27 13:50:32

标签: android checkbox sharedpreferences expandablelistview

我有一个expandableListView,其中包含许多复选框。对于我的应用程序,我希望能够保存这些检查表的检查值,但是,我不能使用sharedPreferences,因为我必须使用包含布尔数组的Hashmap。这是我的代码

import java.util.HashMap;
import java.util.Map.Entry;

import android.content.Context;
import android.content.SharedPreferences;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

public class ExpListAdapter extends BaseExpandableListAdapter {

    private Context context;
        //second attempt to hold the values
    private SharedPreferences inventorylistcheckStateData;

    private String[] parentList = { "Bedding", "Bedroom Items", "Clothing",
            "Bags", "Stationary", "Documents", "Electronics", "Toiletries",
            "Kitchen Items" };







    // used to retain our checklist values, which for some reason dont stick.
    private HashMap<Integer, boolean[]> checkState;

    // multidimensional string storage is needed as you are storing values for
    // two lists.
    private String[][] childList = {
            { "Single/Double sheets", "Single/Double duvet + Covers",
                    "Pillows + Pillow cases" },
            { "Alarm Clock", "Posters", "Door Wedge", "Lamp", "Small Bin",
                    "Hamper Basket" },
            { "Casual Clothes, i.e T shirts, jeans, hoodies",
                    "Smart clothing for interviews and presentations",
                    "Warm Clothing (especially for newcastle)",
                    "'Party Clothes' clothes for going out",
                    "Underwear and socks", "pyjamas", "Comfortable shoes",
                    "Sports trainers", "Swimwear" },
            { "Everyday bag/backpack", "Gym bag", "Clear Pencil Case",
                    "Purse/Wallet", "Watch" },
            { "Pins", "A4 Notebooks", "Pens/Pencils", "Highlighters", "Ruler",
                    "Rubber", "Selotape", "Hole Puncher", "A4 Binders",
                    "Calculater", "Calender" },
            { "Passport photos", "Passport",
                    "Driving License (some form of id)", "Your NI Card",
                    "Insurance Documents", "NHS Medical Card",
                    "Insurance documents", "Letter of Acceptance",
                    "Scholarship/bursury letters",
                    "Rail Card(if you have one)", "C.V" },
            { "Laptop+Charger", "Mouse", "Phone + Charger", "Ethernet Cables",
                    "USB memory stick", "Headphones", "Digital Camera",
                    "MP3 Player" },
            { "Shampoo", "Razors", "Toothbrush and toothpaste",
                    "Make-up/remover", "HairBrush",
                    "Condoms or other protection!!!" },
            { "Frying Pan", "Wok", "Tin Opener", "Bottle opener", "Glasses",
                    "Cheese Grater", "Knives", "Chopping Board", "Scissors",
                    "Tea Towels", "Tupperware", "Cling Film", "Cutlery",
                    "Crockery" } };

    public ExpListAdapter(Context context) {
        this.context = context;
        // initialised check
        checkState = new HashMap<Integer, boolean[]>();

    }

    public Object getChild(int groupPosition, int childPosition) {

        return childPosition;
    }

    //
    public long getChildId(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return 0;
    }

    public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {

        final int mGroupPosition = groupPosition;
        final int mChildPosition = childPosition;

        CheckBox checkbox = new CheckBox(context);
        checkbox.setOnCheckedChangeListener(null);

        if (checkState.containsKey(mGroupPosition)) {
            boolean getChecked[] = checkState.get(mGroupPosition);
            checkbox.setChecked(getChecked[mChildPosition]);
        } else {
            boolean getChecked[] = new boolean[getChildrenCount(mGroupPosition)];
            checkState.put(mGroupPosition, getChecked);
            checkbox.setChecked(false);
        }
        checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
                 if (isChecked) {

                     boolean getChecked[] = checkState.get(mGroupPosition);
                     getChecked[mChildPosition] = isChecked;
                     checkState.put(mGroupPosition, getChecked);

             } else {

                     boolean getChecked[] = checkState.get(mGroupPosition);
                     getChecked[mChildPosition] = isChecked;
                     checkState.put(mGroupPosition, getChecked);
             }


            }
        });

        checkbox.setText(childList[groupPosition][childPosition]);
        checkbox.setPadding(40, 0, 0, 0);

        //SharedPreferences.Editor editor = inventorylistcheckStateData.edit();
        //for (Entry<Integer, boolean[]> entry: checkState.entrySet()) editor.putBoolean(entry.getKey().toString(), entry.getValue());

        return checkbox;
    }

    // returns the number of children you are having.

    public int getChildrenCount(int groupPosition) {
        // TODO Auto-generated method stub
        return childList[groupPosition].length;
    }

    // returns the number of parents you have.
    public Object getGroup(int groupPosition) {
        return groupPosition;
    }

    public int getGroupCount() {
        // TODO Auto-generated method stub
        return parentList.length;
    }

    public long getGroupId(int groupPosition) {
        // TODO Auto-generated method stub
        return groupPosition;
    }

    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        TextView text = new TextView(context);
        text.setText(parentList[groupPosition]);
        text.setPadding(40, 0, 0, 0);
        return text;
    }

    public boolean hasStableIds() {
        // TODO Auto-generated method stub
        return false;
    }

    public boolean isChildSelectable(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return true;
    }

}

无论如何我可以保存清单,还是我必须改变一切?

1 个答案:

答案 0 :(得分:0)

SharedPreferences keyValues = getContext().getSharedPreferences("Your_Shared_Prefs"), Context.MODE_PRIVATE);
SharedPreferences.Editor keyValuesEditor = keyValues.edit();

for (String s : checklist.keySet()) {
    keyValuesEditor.putString(s, checklist.get(s));
}

keyValuesEditor.commit();