请帮助我尝试在ListView中进行多个复选框选择。 我也试过/推荐Link&还Link但是现在我得到了新的错误,当我取消选中CheckBox时,下一个CheckBox在滚动ListView后获得选中。我不知道我的代码有什么问题,请帮帮我。
//Main ListView Class
public View getViewByPosition(int position, ListView listView) {
final int firstListItemPosition = closeMListView
.getFirstVisiblePosition();
final int lastListItemPosition = firstListItemPosition
+ closeMListView.getChildCount() - 1;
if (position < firstListItemPosition || position > lastListItemPosition) {
return closeMListView.getAdapter().getView(position, null,
closeMListView);
} else {
final int childIndex = position - firstListItemPosition;
return closeMListView.getChildAt(childIndex);
}
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
System.out.println("Inside onItemClick position " + position);
System.out.println("Inside onItemClick ckStatus " + ckStatus.size());
View vi = getViewByPosition(position, closeMListView);
cb = (CheckBox) vi.findViewById(R.id.ckHStatus);
// cb.setOnCheckedChangeListener(null);
// cb.setOnCheckedChangeListener(this);
System.out.println("Inside onItemClick ckStatus "
+ (ckStatus.get(position) == true));
if (ckStatus.get(position) == true) {
ckStatus.add(position, false);
cb.setChecked(false);
ListViewAdapterForCloseM.mIds
.remove(ListViewAdapterForCloseM.mId.get(position));
} else {
cb.setChecked(true);
ckStatus.add(position, true);
ListViewAdapterForCloseM.mIds
.add(ListViewAdapterForCloseM.mId.get(position));
}
}
我的适配器类是
public class ListViewAdapterForCloseM extends BaseAdapter {
Context mContext;
MyAppDB helper;
public static ArrayList<String> mId;
ArrayList<String> vName, vMDate, mStatus;
public static ArrayList<String> mIds;
LayoutInflater inflater;
ViewHolder holder;
Cursor cr;
View vi;
int myPosition;
public ListViewAdapterForCloseM(Context context,
int listviewItemClosemeeting,
List<HashMap<String, String>> arrayList) {
// TODO Auto-generated constructor stub
System.out.println("Inside ListView AD arrayList "
+ arrayList.size());
mContext = context;
helper = new MyAppDB(mContext, MyAppDB.DB_NAME, null, 1);
mId = new ArrayList<String>();
vName = new ArrayList<String>();
vMDate = new ArrayList<String>();
mStatus = new ArrayList<String>();
mIds = new ArrayList<String>();
for (int i = 0; i < arrayList.size(); i++) {
HashMap<String, String> content = new HashMap<String, String>();
content = arrayList.get(i);
mId.add(content.get("Id"));
vName.add(content.get("vName"));
vMDate.add(content.get("Date"));
mStatus.add(content.get("status"));
}
}
class ViewHolder {
TextView tvVName, tvMDate, tvMStatus;
CheckBox cb;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mId.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
vi = convertView;
holder = null;
myPosition = position;
System.out.println("Inside getView " + position);
if (vi == null) {
vi = LayoutInflater.from(mContext).inflate(
R.layout.listview_adapter_item, null);
holder = new ViewHolder();
holder.tvVName = (TextView) vi.findViewById(R.id.tvLvVName);
holder.tvMDate = (TextView) vi.findViewById(R.id.tvLvDate);
holder.tvMStatus = (TextView) vi.findViewById(R.id.tvLvStatus);
holder.cb = (CheckBox) vi.findViewById(R.id.ckHStatus);
vi.setTag(R.id.ckHStatus, holder.cb);
vi.setTag(holder);
} else {
holder = (ViewHolder) vi.getTag();
}
System.out.println("Inside getView status "
+ CloseM.ckStatus.get(position));
holder.tvVName.setText(vName.get(position));
holder.tvMDate.setText(vMDate.get(position));
holder.tvMStatus.setText(mStatus.get(position));
holder.cb.setChecked(CloseM.ckStatus.get(position));
// holder.cb.setTag(holder);
return vi;
}
}
答案 0 :(得分:0)
删除代码
holder.cb.setChecked(CloseM.ckStatus.get(position));
在适配器类的getView内部,当你滚动listview时,将调用adatper类的getView方法,因此复选框是gettting selected。
您可以根据需要在LIstItem或其他地方选中复选框。