Reputation: 7092
Hi I am trying to get the checked item and store in the db, but when i click on button save to get the size of the item checked getting every time size is zero. When click on item of list when it give the only size of the item checked but still if item is not checked. So it creating the problem for me help me Thanks. Here is my Code is
CustomAdapter.java
public class CustomAdapter extends ArrayAdapter<ContactDetails>{
List<ContactDetails> listcontacts=null;
private LayoutInflater mInflater=null;
private SparseBooleanArray mSelectedItemsIds;
public CustomAdapter(Activity context, List<ContactDetails> list) {
super(context, 0);
mInflater = context.getLayoutInflater();
mSelectedItemsIds = new SparseBooleanArray(list.size());
this.listcontacts=list;
}
@Override
public int getCount() {
return listcontacts.size();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
view = mInflater.inflate(R.layout.contacts_row, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.txtname = (TextView) view.findViewById(R.id.textView_name);
viewHolder.txtphonenum = (TextView) view.findViewById(R.id.textView_phonenum);
viewHolder.checkbox = (CheckBox) view.findViewById(R.id.checkBox_check);
viewHolder.checkbox.setFocusable(false);
viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
ContactDetails element = (ContactDetails) viewHolder.checkbox.getTag();
element.setSelected(buttonView.isChecked());
}
});
view.setTag(viewHolder);
viewHolder.checkbox.setTag(listcontacts.get(position));
} else {
view = convertView;
((ViewHolder) view.getTag()).checkbox.setTag(listcontacts.get(position));
}
ViewHolder holder = (ViewHolder) view.getTag();
holder.txtname.setText(listcontacts.get(position).getName());
holder.txtphonenum.setText(listcontacts.get(position).getPhonenumber());
holder.checkbox.setChecked(listcontacts.get(position).isSelected());
return view;
}
private static class ViewHolder {
TextView txtname;
TextView txtphonenum;
CheckBox checkbox;
}
}
MuListview class is
public class AddChoiceContactList extends Activity implements OnClickListener, OnItemClickListener{
private ListView listview=null;
private Context context=null;
private DataBaseHelper db=null;
CustomAdapter adapter=null;
private Button btnsave=null;
List<ContactDetails> list=new ArrayList<ContactDetails>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.choicelist);
context=this;
listview =(ListView)findViewById(R.id.listView_choice);
btnsave=(Button)findViewById(R.id.button_save);
btnsave.setOnClickListener(this);
listview.setOnItemClickListener(this);
}
@Override
protected void onResume() {
super.onResume();
list=db.GetDataContacts();
if(list.size()>0){
//listview=getListView();
Toast.makeText(context, "size is: "+list.size(), Toast.LENGTH_LONG).show();
adapter=new CustomAdapter(AddChoiceContactList.this, list);
listview.setAdapter(adapter);
listview.setItemsCanFocus(false);
listview.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}
else{
Toast.makeText(context, "No Contacts Add contats plz!", Toast.LENGTH_LONG).show();
}
}
@Override
public void onClick(View arg0) {
SparseBooleanArray checked=listview.getCheckedItemPositions();
// here m getting size zero when checked then Toast.makeText(context, "checked size is: "+checked.size(), Toast.LENGTH_LONG).show();
ArrayList<UpdatedContact> selectedItems = new ArrayList<UpdatedContact>();
for (int i = 0; i < checked.size(); i++) {
int position = checked.keyAt(i);
if (checked.valueAt(i)){
ContactDetails detils=adapter.getItem(position);
}
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
}
}
Upvotes: 1
Views: 1472
Reputation: 4344
Please try something like this.
In Your Activity:
private static HashMap<Integer,Integer> selectedPositions = new HashMap<Integer,Integer>();
public static boolean isSelected(int position){
System.out.println("#### Position: " + position + " Value: " + selectedPositions.containsKey(position));
return selectedPositions.containsKey(position);
}
public static void addSelection(int position){
System.out.println("#### Puttin Position: " + position);
selectedPositions.put(position,position);
}
public static void removeSelection(int position){
System.out.println("#### Removing Position: " + position);
selectedPositions.remove(position);
}
In your adapter:
checkbox.setChecked(MainActivity.isSelected(position));
checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton view, boolean state) {
// TODO Auto-generated method stub
if(state == true){
MainActivity.addSelection(position);
}else{
MainActivity.removeSelection(position);
}
}
});
thats it now, whenever you want iterate through the hasmap for finding out selected items, make sure you clear it when job is done, else u will end up piling selected positions again and again.
Upvotes: 0
Reputation: 14590
First take a list ArrayList<ContactDetails> checkedList
as gloabl and initialize it in consstructor..
Write a method like below in your Custom Adapter
private ArrayList<ContactDetails> getCheckedItems() {
return checkedList;
}
and change your checkbox change listener like this..
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
checkedList.add(list.get(position));
}else {
checkedList.remove(list.get(position));
}
}
});
And call adapter.getCheckedItems();
in your onItemClickListener
of your Activity
then you will get the all the checked items..
Upvotes: 1