如何在Android中的ArrayList中存储复选框选中的值

时间:2015-11-27 15:06:43

标签: java android

我想将ExpandableListView child的复选框选中值存储在数组列表中,但我无法执行此操作。 ArrayList只给我最后检查的值,但我想存储复选框的所有选中值。

package com.zobrando.contests;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;


public class RightDrawerListAdapter extends BaseExpandableListAdapter {

private Context context;
String xid;
private List<String> navDrawerItems;
private ArrayList<RightNanDrawerItems> childItems;
HashMap<String, List<String>> popuplist;
boolean checkBoxStates[];

HashMap<Integer, boolean[]> mChildCheckStates = new HashMap<Integer,     boolean[]>();
private static final Integer[] Icons = {
        R.drawable.trophy,
        R.drawable.buld,
        R.drawable.brand,
        R.drawable.target,
        R.drawable.age,
        R.drawable.participants,
        R.drawable.reward
};
CheckBox checkBox;

public RightDrawerListAdapter(Context context, List<String>   navDrawerItems,HashMap<String, List<String>> popuplist){
    this.context = context;
    this.navDrawerItems = navDrawerItems;
    this.childItems = childItems;
    this.popuplist = popuplist;
}

@Override
public Object getChild(int groupPosition, int chlidPosition) {
    //String pos = String.valueOf(groupPosition);
    return this.popuplist.get(this.navDrawerItems.get(groupPosition))
            .get(chlidPosition);
}

@Override
public Object getGroup(int groupPosition) {

    return this.navDrawerItems.get(groupPosition);
}

@Override
public int getGroupCount() {

    return this.navDrawerItems.size();
}

@Override
public long getChildId(int groupPosition, int childPosition) {
    return childPosition;
}

 @Override
 public long getGroupId(int groupPosition) {
    return groupPosition;
 }

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

    final String childText = getChild(groupPosition,      childPosition).toString();
    final ArrayList<String> selectedStrings = new ArrayList<String>();


    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this.context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.pop_up_list_items,  null);
    }
    //selectedStrings.clear();

    TextView txtTitle = (TextView) convertView.findViewById(R.id.popuplist);
    checkBox = (CheckBox)convertView.findViewById(R.id.checked);

    //checkBox.setText(childid);
    txtTitle.setText(childText);

    checkBox.setOnCheckedChangeListener(new     CompoundButton.OnCheckedChangeListener() {
        @Override
         public void onCheckedChanged(CompoundButton buttonView, boolean   isChecked) {
                     if (isChecked){
                        xid = childText.substring(0,1);
                        xid = xid.replace("-", "");
                        selectedStrings.add(xid);
                    }

                Toast.makeText(context,xid+"xid",Toast.LENGTH_LONG).show();
                Toast.makeText(context,selectedStrings+"Seleted   Strings",Toast.LENGTH_LONG).show();                                               
                SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
                SharedPreferences.Editor editor = pref.edit();
                editor.putString("xid", mChildCheckStates+"");
                editor.apply();
        }
    });
    //checkBox.setOnCheckedChangeListener(new   CheckchangeListener(groupPosition));
    return convertView;
}


@Override
public int getChildrenCount(int groupPosition) {
    return this.popuplist.get(this.navDrawerItems.get(groupPosition))
            .size();
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
                         View convertView, ViewGroup parent) {
    String headerTitle =  getGroup(groupPosition).toString();
    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this.context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView =    infalInflater.inflate(R.layout.right_drawer_list_items, null);
    }

    ImageView imgIcon = (ImageView) convertView.findViewById(R.id.icon);
    imgIcon.setImageResource(Icons[groupPosition]);
    TextView txtTitle = (TextView) convertView.findViewById(R.id.title);

    imgIcon.setImageResource(navDrawerItems.get(groupPosition).getIcon());
    txtTitle.setText(headerTitle);

    return convertView;
}

@Override
public boolean hasStableIds() {
    return false;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}

class CheckchangeListener implements CompoundButton.OnCheckedChangeListener     {
    private int position;


    public CheckchangeListener(int position) {
        // TODO Auto-generated constructor stub
        this.position= position;


    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView,
                                 boolean isChecked) {
        // TODO Auto-generated method stub
        if (isChecked) {
            //updateyour list and database here

        } else {
            //updateyour list and database here

        }

    }
}

}

1 个答案:

答案 0 :(得分:0)

不要在全球范围内声明checkBoxxid。 Insted在getChildView内声明它们。

您只获得数组列表中的最后一个值,因为您全局声明了字符串。

因此,您的getChildView代码如下所示:

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

    final String childText = getChild(groupPosition,      childPosition).toString();
    final ArrayList<String> selectedStrings = new ArrayList<String>();


    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this.context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.pop_up_list_items,  null);
    }
    //selectedStrings.clear();

    TextView txtTitle = (TextView) convertView.findViewById(R.id.popuplist);
    CheckBox checkBox = (CheckBox)convertView.findViewById(R.id.checked);

    //checkBox.setText(childid);
    txtTitle.setText(childText);

    checkBox.setOnCheckedChangeListener(new     CompoundButton.OnCheckedChangeListener() {
        @Override
         public void onCheckedChanged(CompoundButton buttonView, boolean   isChecked) {
                     if (isChecked){
                        String  xid = childText.substring(0,1);
                        xid = xid.replace("-", "");
                        selectedStrings.add(xid);
                    }

                Toast.makeText(context,xid+"xid",Toast.LENGTH_LONG).show();
                Toast.makeText(context,selectedStrings+"Seleted   Strings",Toast.LENGTH_LONG).show();                                               
                SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
                SharedPreferences.Editor editor = pref.edit();
                editor.putString("xid", mChildCheckStates+"");
                editor.apply();
        }
    });
    //checkBox.setOnCheckedChangeListener(new   CheckchangeListener(groupPosition));
    return convertView;
}