2016-03-15 68 views
0

我在我的适配器类中有两个CheckBox checkbox1,checkbox2。我想如果我检查CheckBox列表中的所有CheckBox应该检查。例如在我的ListView中有20 CheckBox,如果我检查一个,所有其他19应该检查。自定义列表视图与选中和未选中的所有CheckBox列由android中的另一个CheckBox?

这个适配器类

public class ListViewAdapter extends ArrayAdapter<Friend> { 

private List<Friend> myFriends; 
private Activity activity; 
private int selectedPosition = -1; 

public ListViewAdapter(Activity context, int resource, List<Friend> objects) { 
    super(context, resource, objects); 

    this.activity = context; 
    this.myFriends = objects; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    ViewHolder holder; 
    LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE); 

    // If holder not exist then locate all view from UI file. 
    if (convertView == null) { 
     // inflate UI from XML file 
     convertView = inflater.inflate(R.layout.item_listview, parent, false); 
     // get all UI view 
     holder = new ViewHolder(convertView); 
     // set tag for holder 
     convertView.setTag(holder); 
    } else { 
     // if holder created, get tag from view 
     holder = (ViewHolder) convertView.getTag(); 
    } 

    holder.checkBox1.setTag(position); // This line is important. 
    holder.checkBox2.setTag(position+100); 

    holder.friendName.setText(getItem(position).getName()); 
    if (position == selectedPosition) { 
     holder.checkBox.setChecked(true); 
    } else holder.checkBox.setChecked(false); 

    if(holder.checkBox1.getTag().equals(position)){     holder.checkBox1.setOnClickListener(onStateChangedListener(holder.checkBox1, position)); 
    }else{ 
     holder.checkBox2.setOnClickListener(onStateChangedListener(holder.checkBox2, position)); 
} 
    return convertView; 
} 
private View.OnClickListener onStateChangedListener(final CheckBox checkBox, final int position) { 
    return new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      if (checkBox.isChecked()) { 
       selectedPosition = position; 
      } else { 
       selectedPosition = -1; 
      } 
      notifyDataSetChanged(); 
     } 
    }; 
} 

private static class ViewHolder { 
    private TextView friendName; 
    private CheckBox checkBox1,checkBox2; 

    public ViewHolder(View v) { 
     checkBox1 = (CheckBox)v.findViewById(R.id.check); 
     checkBox2=(CheckBox)v.findViewById(R.id.check1); 
     friendName = (TextView) v.findViewById(R.id.name); 
    } 
} 
} 

回答

0

添加两个布尔标志。对象级别的isChecked1和模型中的类级别(静态变量)的isChecked2。这些默认为false。

我假设checkbox2是一旦选中将更改所有复选框进行检查。

现在你们所有的人都会有isChecked1变量。点击任何checkbox1将模型中的isChecked1布尔值更改为单击位置上的true。并致电notifyDatasetChanged

如果用户检查checkbox2只是将静态变量更改为true。并致电notifyDatasetChanged

在您的getView方法中添加一个简单的条件,如果isChecked2为true,则使所有复选框被选中,否则仅检查其模型的isChecked1为真。

0

为什么你不使用Expandable列表视图而不是列表视图。

<ExpandableListView 
     android:id="@+id/laptop_list" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 
</ExpandableListView> 
+0

在ExpandableListView中如何实现我的帖子 – kumar

+0

分享一些想法 – kumar

相关问题