Question

i have two Questions. 1) How to open option menu on click of checkbox in custom listview with checkbox and 2) how to delete listview items when checkbox is selected(it must work for multiple also checkbox).

This is my code..

CheckListModel.java

      public class CheckListModel
     {
          private String name;
          private boolean selected;

      public CheckListModel(String paramString)
       {
           this.name = paramString;
            this.selected = false;
        }

       public String getName()
        {
            return this.name;
         }

        public boolean isSelected()
        {
            return this.selected;
         }

        public void setName(String paramString)
        {
               this.name = paramString;
         }

          public void setSelected(boolean paramBoolean)
         {
              this.selected = paramBoolean;
          }
      }

ListwithCheckboxAdapter.java

    public class ListwithCheckboxAdapter extends ArrayAdapter<CheckListModel>
    implements OnCheckedChangeListener {

private final List<CheckListModel> list;
private final Activity context;
ViewHolder viewHolder;

public ListwithCheckboxAdapter(Activity context, List<CheckListModel> list) {
    super(context, R.layout.listcheckbox, list);
    this.context = context;
    this.list = list;
}

static class ViewHolder {
    protected TextView text;
    protected CheckBox checkbox;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = null;
    if (convertView == null) {
        LayoutInflater inflator = context.getLayoutInflater();
        view = inflator.inflate(R.layout.listcheckbox, null);
        viewHolder = new ViewHolder();
        viewHolder.text = (TextView) view.findViewById(R.id.label);
        viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check);
        viewHolder.checkbox.setOnCheckedChangeListener(this);

        view.setTag(viewHolder);
        viewHolder.checkbox.setTag(list.get(position));
    } else {
        view = convertView;
        ((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
    }
    ViewHolder holder = (ViewHolder) view.getTag();
    holder.text.setText(list.get(position).getName());
    holder.checkbox.setChecked(list.get(position).isSelected());
    return view;

}

@Override
public void onCheckedChanged(CompoundButton cb, boolean isChecked) {
    // TODO Auto-generated method stub

    CheckListModel element = (CheckListModel) viewHolder.checkbox.getTag();

    element.setSelected(cb.isChecked());
}

}

FavouriteActivity.java

       public class FavouriteActivity extends Activity implements OnItemClickListener {

   ListView mainListView;
   List<CheckListModel> list;

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // TODO Auto-generated method stub

    getMenuInflater().inflate(R.menu.fav_menu, menu);

    return true;

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
    super.onOptionsItemSelected(item);

    switch (item.getItemId()) {

    case R.id.delete:

    case R.id.deselect:


        break;

    default:
        break;
    }
    return true;
}

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setContentView(R.layout.favourites);

    // Find the ListView resource.
    mainListView = (ListView) findViewById(R.id.list);


    ArrayAdapter<CheckListModel> adapter = new ListwithCheckboxAdapter(
            this, getModel());
    mainListView.setAdapter(adapter);



}

private List<CheckListModel> getModel() {

    list = new ArrayList<CheckListModel>();

    list.add(get("Mitesh"));
    list.add(get("Agrawal"));


    return list;
}

private CheckListModel get(String s) {
    // TODO Auto-generated method stub
    return new CheckListModel(s);
}

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

}

}

Was it helpful?

Solution

You can use openOptionsMenu(); to Open Option Menu.

And to remove the list Item, Remove entry from you List Object and call adapterObj.notifyDataSetChanged(); method to update the ListView.

OTHER TIPS

i have done it..

open Option Menu in Customized list view with Checkbox.

  Activity activity = context;
 activity.openOptionsMenu();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top