在我的ExpandableListview
中,我使用的Imageview
和Checkbox
用于childview
,类似于形状图像和名称。
当我勾选特定子项的checkbox
时,滚动选中的状态更改后,另一个子项也会被勾选。
我需要选择多个值。
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private static final int[] EMPTY_STATE_SET = {};
private static final int[] GROUP_EXPANDED_STATE_SET =
{android.R.attr.state_expanded};
private static final int[][] GROUP_STATE_SETS = {
EMPTY_STATE_SET, // 0
GROUP_EXPANDED_STATE_SET // 1
};
private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;
private ArrayList<Boolean> itemChecked = new ArrayList<Boolean>();
private boolean[] itemCheckedBoolean = new boolean[100];
Bitmap bmp;
LayoutInflater infalInflater;
ImageLoader imageLoader;
public ExpandableListAdapter(Context context, List<String> listDataHeader, HashMap<String, List<String>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
infalInflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
imageLoader=new ImageLoader(_context);
for (int i = 0; i < SearchWhiteDiamondsActivity.shape_list.size(); i++) {
itemChecked.add(i, false);
}
}
@Override
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosititon);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
String childText = (String) getChild(groupPosition, childPosition);
Log.e("_childText", "karjeevch "+childText);
int itemType = getChildType(groupPosition,childPosition);
ViewHolder viewHolder = null;
switch (itemType) {
case 0:
viewHolder = null;
if (convertView==null) {
viewHolder=new ViewHolder();
convertView = infalInflater.inflate(R.layout.list_child_shape, null);
viewHolder.shape_name = (CheckBox) convertView.findViewById(R.id.shape_chk_box);
//viewHolder.shape_name = (TextView) convertView.findViewById(R.id.shape_chk_box);
viewHolder.img_shape_icon=(ImageView)convertView.findViewById(R.id.img_shape);
imageLoader.DisplayImage("http://rosycontact.com/shashvat/images/"+childText.toLowerCase()+".png", viewHolder.img_shape_icon);
Log.e("shape", "karjeevshp "+childText);
viewHolder.shape_name.setText(childText);
convertView.setTag(viewHolder);
final CheckBox shape_name_temp=viewHolder.shape_name;
//final TextView shape_name_temp=viewHolder.shape_name;
viewHolder.shape_name.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
int id=buttonView.getId();
if (id==R.id.shape_chk_box) {
if (shape_name_temp.isChecked()==true) {
String shape_str=shape_name_temp.getText().toString();
All_link.SHAPE_LIST.add(shape_str);
Toast.makeText(_context, shape_name_temp.getText().toString(), Toast.LENGTH_SHORT).show();
Log.e("chk_shape", "karjeevch "+shape_name_temp.getText().toString());
itemChecked.set(childPosition, true);
}
else{
String shape_str=shape_name_temp.getText().toString();
All_link.SHAPE_LIST.remove(shape_str);
itemChecked.set(childPosition, false);
}
}
}
});
}
else{
//viewHolder=new ViewHolder();
viewHolder=(ViewHolder)convertView.getTag();
imageLoader.DisplayImage("http://rosycontact.com/shashvat/images/"+childText.toLowerCase()+".png", viewHolder.img_shape_icon);
viewHolder.shape_name.setText(childText);
convertView.setTag(viewHolder);
}
return convertView;
答案 0 :(得分:0)
保存复选框的状态....
viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int getPosition = (Integer) buttonView.getTag(); // Here we get the position that we have set for the checkbox using setTag.
list.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state.
}
});