-1
我已創建自定義適配器和兩個佈局文件 第一個佈局是包含ListView
和Button
安卓:做的時候複選框被選中的東西
第二佈局包含將在ListView
所以
我希望在Checkbox
被檢查使Button
可見
所有這些操作都將在我的自定義適配器類來完成
這是myCustom適配器
更新
public class LampControllerAdapter extends BaseAdapter {
Context mContext;
List<LampModel> mLampModels;
HashMap<Long,LampModel> selection;
Button sC;
public LampControllerAdapter(Context context, ArrayList<LampModel> lampList) {
mContext = context;
mLampModels = lampList;
selection = new HashMap<Long,LampModel>();
LayoutInflater in = (LayoutInflater) LayoutInflater.from(context);
View v = in.inflate(R.layout.fragment_home,null);
sC = (Button)v.findViewById(R.id.selection_control);
}
@Override
public int getCount() {
return mLampModels.size();
}
@Override
public Object getItem(int i) {
return mLampModels.get(i);
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(final int i, View view, ViewGroup parent) {
ViewHolder holder;
final LampModel model = (LampModel) getItem(i);
if (view == null) {
// Inflating The Layout
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
view = inflater.inflate(R.layout.lamp_controller_item, parent, false);
holder = new ViewHolder();
holder.name = (TextView) view.findViewById(R.id.lamp_item_name);
holder.description = (TextView) view.findViewById(R.id.lamp_item_description);
holder.select = (CheckBox) view.findViewById(R.id.lamp_item_select);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.name.setText(model.getName());
holder.description.setText(model.getDescription());
holder.select.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(b){
selection.put(model.getId(),model);
}else {
selection.remove(model.getId());
}
if(selection.size() > 0){
Toast.makeText(mContext, "Not Empty", Toast.LENGTH_SHORT).show();
sC.setVisibility(View.GONE);
}else {
Toast.makeText(mContext, "Empty", Toast.LENGTH_SHORT).show();
}
}
});
return view;
}
class ViewHolder {
TextView name, description;
CheckBox select;
Switch aSwitch;
}
}
複選框在哪裏? – Bills
在列表視圖中顯示的佈局 –
在複選框'checkedListener'上使用回調接口,並使動態可見性 –