2015-11-27 44 views
0

我想存储数组列表中ExpandableListView子项的复选框选中值,但我无法执行此操作。 ArrayList只给我最后一次选中的值,但我想存储所有复选框的选中值。如何存储复选框选中Android 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 

     } 

    } 
} 

}

+0

那是什么:selectedStrings.add(0,XID); ??? –

+0

selectedStrings在字符串类型的数组列表中,我在索引0处添加xid –

+0

我也做过类似 xid = childText.substring(0,1); xid = xid.replace(“ - ”,“”); selectedStrings.add(xid); –

回答

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; 
} 
+0

感谢您的回复...实际上我宣布selectedStrings最终是这个问题 –

相关问题