Reputation: 1924
I am implementing a multiplechoice checkbox. I have successfully populated the listview with the chexbox item using
DestinataireAdapter destinataireAdapter = new DestinataireAdapter(m, destinataire);
mylist.setAdapter(destinataireAdapter);
destinataireAdapter.notifyDataSetChanged();
Here is my DestinataireAdapter class:
public class DestinataireAdapter extends BaseAdapter {
Context ctx;
LayoutInflater lInflater;
ArrayList<Destinataire> data;
public DestinataireAdapter(Context context, ArrayList<Destinataire> products) {
ctx = context;
data = products;
lInflater = (LayoutInflater) ctx
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
view = lInflater.inflate(R.layout.destinataire_item, parent, false);
}
Destinataire pos = getselectedposition(position);
((TextView) view.findViewById(R.id.txDestinataireItem)).setText(data.get(position).getNickName());
CheckBox chkbox = (CheckBox) view.findViewById(R.id.cbBox);
chkbox.setOnCheckedChangeListener(myCheckChangList);
chkbox.setTag(position);
chkbox.setChecked(pos.ischeckedflag);
return view;
}
Destinataire getselectedposition(int position) {
return ((Destinataire) getItem(position));
}
public ArrayList<Destinataire> getcheckedposition() {
ArrayList<Destinataire> checkedposition = new ArrayList<Destinataire>();
for (Destinataire p : data) {
if (p.ischeckedflag)
checkedposition.add(p);
}
return checkedposition;
}
OnCheckedChangeListener myCheckChangList = new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
getselectedposition((Integer) buttonView.getTag()).ischeckedflag = isChecked;
}
};
}
for example i select multiple item on the menu and click on a button to get all position checked from the listview. I use the method below
public void GetSelectedPositions(View v) {
String result = "Selected Position is";
// result += "\n" + p.position;
for (Destinataire p : destinataireAdapter.getcheckedposition()) {
if (p.ischeckedflag){
// int pp= p.position;
result += "," + p.position;
System.out.println(p.position);
}
}
Toast.makeText(this, result, 500).show();
}
and the Destinataire class model where values of id,version, and nickName are stored.
public class Destinataire {
String id;
String version;
String nickName;
public int position;
public boolean ischeckedflag;
public Destinataire(String id, String version, String nickName){
this.id=id;
this.version=version;
this.nickName=nickName;
}
public String getId() {
return id;
}
public String getVersion() {
return version;
}
public String getNickName() {
return nickName;
}
Destinataire(int name, boolean flag) {
position = name;
ischeckedflag = flag;
}
}
When the toast appears i get result 0,0,0 for the choice i made instead of the ids populated from Destinataire class. How can i get the corresponding id when i made choices. any help please.
Upvotes: 0
Views: 1731
Reputation: 187
http://samir-mangroliya.blogspot.in/2012/09/android-multiple-select-listview.html
This above link may be helpful to you
Upvotes: 2