ListView中有Checkbox时的点击和选择问题

之前项目中有用到这块的东西,现在在拿来用,觉得还是写下来比较好。

言简意赅,主界面有个listview:

android:id="@+id/list_food"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@id/food_bottom"
android:layout_below="@id/input_1"
android:divider="@color/voice_bg"
android:cacheColorHint="@color/transparent"
android:dividerHeight="1dip" >

在adapter中有一个checkbox:(可选择状态都设为false)

android:id="@+id/food_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/radiobutton"
android:layout_alignParentRight="true"
android:button="@null"
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"
android:paddingRight="@dimen/margin_max"
android:layout_marginRight="@dimen/margin_max" />

在adapter中进行标记和赋值:

private LayoutInflater mInflater;
/**the the food data list */
private List mData;
/*the context*/
Context context;
int k=0;
public static Map isSelected= new HashMap();
public FoodChooseAdapter(Context context, List list) {
mInflater = LayoutInflater.from(context);
this.context = context;
mData = list;
init();
}
// 初始化
private void init() {
// mData=new ArrayList>();
// 这儿定义isSelected这个map是记录每个listitem的状态,初始状态全部为false。

for (int i = 0; i < mData.size(); i++) {
isSelected.put(i, false);
}
}
public View getView(int position, View convertView, ViewGroup parent) {
HistoryWeightHolder holder = null;
if (convertView == null) {
// init convertView if convertView is null
convertView = mInflater.inflate(R.layout.food_choose,null);
holder = new HistoryWeightHolder();
holder.radio=(CheckBox) convertView.findViewById(R.id.food_add);
holder.name = (TextView) convertView
.findViewById(R.id.name);
holder.unit = (TextView) convertView
.findViewById(R.id.food_unit);
holder.energy = (TextView) convertView
.findViewById(R.id.energy);
convertView.setTag(holder);

} else {
holder = (HistoryWeightHolder) convertView.getTag();
}
holder.name.setText(mData.get(position).getName());
holder.unit.setText(String.valueOf(mData.get(position).getUnit()));
holder.energy.setText(String.valueOf(mData.get(position).getEnergy())+context.getResources().getText(R.string.unit_1).toString());
holder.radio.setChecked(isSelected.get(position));
return convertView;
}


public static final class HistoryWeightHolder {
public CheckBox radio;
public TextView name;
public TextView unit;
public TextView energy;
}


public int getCount() {
return mData.size();
}


public Object getItem(int position) {
return null;
}


public long getItemId(int position) {
return 0;
}

最后是activity中的代码:

fca = new FoodChooseAdapter(CalerySearchActivity.this, mListCount);
mList.setAdapter(fca);

mList.setItemsCanFocus(false);
mList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
mList.setOnItemClickListener(new AdapterView.OnItemClickListener() {


public void onItemClick(AdapterView parent, View view,
int position, long id) {
HistoryWeightHolder holder = (HistoryWeightHolder) view
.getTag();
holder.radio.toggle();
/* click of last enable all left;inverse is all the same */
for (int i = 0; i < mList.getCount(); i++) {
if (i == position) {
FoodChooseAdapter.isSelected.put(position,
holder.radio.isChecked());
} else {
FoodChooseAdapter.isSelected.put(i, false);
}
}
}
});

擦~主要就是这样了,效果就是点击list的一项,这项被选中,有checkbox的状态,其他选项都被置为未选中状态。

大概就是这个效果了

你可能感兴趣的:(ListView中有Checkbox时的点击和选择问题)