I have a custom ListView with a checkbox in each item. A user can select a maximum of 3 items from the listview. After selecting items, on a button click i want to get the user selected items. My Adapter class is
public class CBAdapter extends BaseAdapter {
Context context;
public String[] englishNames;
LayoutInflater inflater;
CheckBox[] checkBoxArray;
LinearLayout[] viewArray;
private boolean[] checked;
private ArrayList<NotificationModel> planList;
int count=0;
private LayoutInflater mInflater;
public HashMap<String,String> checked2 = new HashMap<String,String>();
public CBAdapter(Context context, ArrayList<NotificationModel> planList){
this.planList = planList;
mInflater = LayoutInflater.from(context);
this.context = context;
inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return planList.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public void setCheckedItem(int item) {
if (checked2.containsKey(String.valueOf(item))){
checked2.remove(String.valueOf(item));
}
else {
checked2.put(String.valueOf(item), String.valueOf(item));
}
}
public HashMap<String, String> getCheckedItems(){
return checked2;
}
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.pref_list, null);
holder = new ViewHolder();
holder.contactName = (TextView) convertView.findViewById(R.id.name);
holder.checkBox = (CheckBox) convertView.findViewById(R.id.chk);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
NotificationModel browsePlan = planList.get(position);
holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton checkboxView, boolean isChecked)
{
NotificationModel c = (NotificationModel) checkboxView.getTag();
setCheckedItem(position);
if(isChecked)
{
count++;
}
else if(!isChecked)
{
count--;
}
if(count >= 4)
{
Toast.makeText(context, "You can only select a maximum of 3 preference", Toast.LENGTH_LONG).show();
checkboxView.setChecked(false);
count--;
}
else
{
c.setChecked(isChecked);
}
}
});
holder.checkBox.setTag(browsePlan);
holder.checkBox.setChecked(browsePlan.getChecked());
holder.contactName.setText(browsePlan.getName());
return convertView;
}
class ViewHolder {
TextView contactName;
CheckBox checkBox;
}
}
I tried with the following method in my button's onClick method, but it getting Exception
proceed.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Iterator<String> it = adapter.getCheckedItems().values().iterator();
for (int i=0;i<adapter.getCheckedItems().size();i++){
adapter.getItem(Integer.parseInt(it.next()));
Log.e("success",""+adapter.getItem(Integer.parseInt(it.next())));
}
}
});
I am not sure that the method i'm using is correct or not. The error i'm getting from this way is
"java.util.NoSuchElementException".
How can i get items from listview with checked boxes?
Aucun commentaire:
Enregistrer un commentaire