Please help i am trying to do multiple checkbox selection in ListView. I also tried/refer Link & also Link but now i get new error, when i uncheck CheckBox next CheckBox get Selected after Scrolling ListView. I don't know what's wrong with my code, Please help me out.
//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));
}
}
My Adapter Class is
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;
}
}
Aucun commentaire:
Enregistrer un commentaire