2016-03-15 64 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

相關問題