Reputation: 2211
I have a custom List view with some elements and a checkbox. When I click on a button. I want to know the positions of the elements which have been checked. The following below is my code
public class Results extends ListActivity implements OnClickListener{
String[] donorName,donorPhone;
int totNumber;
Button callBut;
ListView listView;
List<RowItem> rowItems;
public static void main(String[] args) {
// TODO Auto-generated method stub
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.results);
Intent intent = getIntent();
donorName = intent.getStringArrayExtra("name");
donorPhone = intent.getStringArrayExtra("phone");
totNumber = intent.getExtras().getInt("totDonors");
callBut = (Button)findViewById(R.id.callBut);
callBut.setOnClickListener(this);
rowItems = new ArrayList<RowItem>();
for (int i = 0; i < totNumber; i++) {
RowItem item = new RowItem(donorName[i], donorPhone[i]);
rowItems.add(item);
}
ListAdapter adapter = new MySimpleArrayAdapter(this,
R.layout.list_item, rowItems);
setListAdapter(adapter);
};
///////////////////////////////////////////////////////////////////////////////////////////
public static class MySimpleArrayAdapter extends ArrayAdapter<RowItem> implements OnCheckedChangeListener {
Context context;
static List<RowItem> donorList = new ArrayList<RowItem>();
public MySimpleArrayAdapter(Context context, int resourceId,
List<RowItem> donorList) {
super(context, resourceId, donorList);
this.context = context;
this.donorList = donorList;
}
private class ViewHolder {
Button donorCall,exp;
TextView donorName;
TextView donorPhone;
CheckBox chkBox;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
final RowItem rowItem = getItem(position);
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.donorName = (TextView) convertView.findViewById(R.id.donorName);
holder.donorPhone = (TextView) convertView.findViewById(R.id.donorPhone);
holder.donorCall = (Button) convertView.findViewById(R.id.donorCall);
holder.chkBox = (CheckBox) convertView.findViewById(R.id.chkBox);
convertView.setTag(holder);
}
else
holder = (ViewHolder) convertView.getTag();
holder.donorPhone.setText(rowItem.getdonorPhoneS());
holder.donorName.setText(rowItem.getdonorNameS());
holder.chkBox.setTag(position);
holder.chkBox.setOnCheckedChangeListener(this);
holder.donorCall.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.d("Button Clicked",position+"");
Intent startCall = new Intent(Intent.ACTION_CALL);
startCall.setData(Uri.parse("tel:" + rowItem.getdonorPhoneS()));
context.startActivity(startCall);
}
});
return convertView;
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
int position = (Integer) buttonView.getTag();
if (isChecked) {
donorList.get(position).setSelected(true);
Log.d("Tag",donorList.get(position).isSelected()+"");
} else {
buttonView.setSelected(false);
Log.d("Unchecked",isChecked+"");
}
notifyDataSetChanged();
}
}
////////////////////////////////////////////////////////////////////////////////////////////////
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String msgRecipient;
Log.d("MSg","Button Clicked");
for (int x = 0; x<totNumber;x++){
if(MySimpleArrayAdapter.donorList.get(x).isSelected()){
Log.d("position Checked",x+"");
}
else
Log.d("position UnChecked",x+"");
}
}
}
When I click the checkbox on an item I get true in Log .But when I click on the Button all the elements are shown under unchecked.
Upvotes: 0
Views: 87
Reputation: 12219
There may be a couple of problems going on here.
Firstly, you'll want to make sure you have the setChoiceMode of your ListView (internal to ListActivity) to something other than the default value. Ie:
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
See for more details: Losing current selection when calling adapter.notifyDataSetChanged()
Additionally, your call to notifyDataSetChanged() may be causing your selections to reset as described here: Losing current selection of list item after updating the listview items using notifyDataSetChanged
Upvotes: 0
Reputation: 519
You can try this to know selected item position in adapter.
for(int i=0;i<MySimpleArrayAdapter.mCheckStates.size();i++)
{
if(MySimpleArrayAdapter.mCheckStates.get(i)==true)
{
// i is the position of a checked items
}
}
Upvotes: 0
Reputation: 116342
you forgot to set the checked state of the checkboxes inside the getView , so if you scroll down/up you will get the old checkboxes being shown without being updated.
what you need to do is to have a set of integers (or a sparseIntArray, which is better), add items positions into it when the checkbox is checked, and remove when they get unchecked.
in order to get all of the checked checkboxed, just use this set of integers...
Upvotes: 2