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

recycling view create issues in checkbox state of childview in ExpandablelListview android

在我的 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;     

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

试试这个: 在你的适配器中覆盖这两个方法 class:

@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();
}