回收视图在Expandablelistview android中的子视图的复选框状态中创建问题

时间:2015-03-09 06:21:58

标签: android expandablelistview expandablelistadapter

在我的ExpandableListview中,我使用的ImageviewCheckbox用于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 TextView shape_name_temp=viewHolder.shape_name;

           viewHolder.shape_name.setChecked(itemChecked.get(childPosition));
           final CheckBox shape_name_temp=viewHolder.shape_name;


           viewHolder.shape_name.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if(position_of_click.contains (childPosition)){ 
                    position_of_click.remove(childPosition);
                } 
                else{ 
                    position_of_click.add(childPosition);
                }
            }
        });



           if(position_of_click.contains(childPosition)){ 
               viewHolder.shape_name.setChecked(true);
            }
           else{ 
               viewHolder.shape_name.setChecked(false);
        }

        }
        else{

            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;     

2 个答案:

答案 0 :(得分:1)

这是常见问题。基本上,这是由于在列表中添加子项时内存重用。 你可以克服这个问题,只需维护一个列表,在其中存储已检查项目的位置,在getView方法中检查特定位置是否存储在列表中,如果此位置存储在列表中,则检查它们,否则取消选中它们。 要在该列表中输入数据 - 当您选中任何复选框时,将其位置添加到列表中,如果位置已存在,请从列表中删除。

答案 1 :(得分:0)

试试这个: 在适配器类中重写这两个方法:

@Override
public int getGroupType(int groupPosition) {
    // TODO Auto-generated method stub
    return super.getGroupType(groupPosition);
}

@Override
public int getGroupTypeCount() {
    // TODO Auto-generated method stub
    return super.getGroupTypeCount();
}

或试试这个:覆盖这些:

@Override
public int getChildType(int groupPosition, int childPosition) {
    // TODO Auto-generated method stub
    return super.getChildType(groupPosition, childPosition);
}

@Override
public int getChildTypeCount() {
    // TODO Auto-generated method stub
    return super.getChildTypeCount();
}