简体   繁体   中英

gmail like listView with checkBoxes: which ones are selected?

I have a list view that has a check box as well as some text. When a checkbox gets selected, it opens up the action mode. Then you can delete this item. My question is: how can I find out in deleteCurrentItem() which checkbox is selected since I don't use ListView's internal checked state?

public class MainActivity extends Activity implements LoaderManager.LoaderCallbacks<Cursor> {

private SimpleCursorAdapter mSimpleCursorAdapter = null;
private final  String[] PROJECTION = { DatabaseHelper.KEY_ID, DatabaseHelper.KEY_TITLE, DatabaseHelper.KEY_DATE };

private ActionMode mActionMode = null;
private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {

    // Called when the action mode is created; startActionMode() was called
    @Override
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        // Inflate a menu resource providing context menu items
        MenuInflater inflater = mode.getMenuInflater();
        inflater.inflate(R.menu.context_menu, menu);
        return true;
    }

    // Called each time the action mode is shown. Always called after onCreateActionMode, but
    // may be called multiple times if the mode is invalidated.
    @Override
    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
        return false; // Return false if nothing is done
    }

    // Called when the user selects a contextual menu item
    @Override
    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        switch (item.getItemId()) {
            case R.id.context_menu_delete:
                deleteCurrentItem();
                mode.finish(); // Action picked, so close the CAB
                return true;
            case R.id.context_menu_edit:
                editCurrentItem();
                mode.finish();
                return true;
            case R.id.context_menu_markAs_done:
                mode.finish();
                return true;
            default:
                return false;
        }
    }

    // Called when the user exits the action mode
    @Override
    public void onDestroyActionMode(ActionMode mode) {
        mActionMode = null;
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTheme(android.R.style.Theme_Holo_Light);

    setContentView(R.layout.activity_main);
    getLoaderManager().initLoader(0, null, this);

    mSimpleCursorAdapter = new SpecialAdapter(this, 
            R.layout.row,
            null,
            PROJECTION,
            new int[] { R.id.titleID, R.id.dateTimeOrLocationID },
            CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);

    ListView listView = (ListView) findViewById(R.id.list);
    listView.setAdapter(mSimpleCursorAdapter);
}

    public void startActionMode()
{
    mActionMode = startActionMode(mActionModeCallback);
}

public void deleteCurrentItem()
{
    LayoutInflater inflater = LayoutInflater.from(this);
    final View view = inflater.inflate(R.layout.row, null, false);
    CheckBox chxBox = (CheckBox)view.findViewById(R.id.itemChxBoxID);

    ListView listView = (ListView)findViewById(R.id.list);

    long[] ids = listView.getCheckedItemIds();
    for(int i=0; i<ids.length; i++) {
        String where = Long.toString(ids[i]);
        if(!where.isEmpty()) {
            getContentResolver().delete(ReminderContentProvider.CONTENT_URI, where, null);
        }
    }
}

adapter class:

  public class SpecialAdapter extends SimpleCursorAdapter {
   ...
    @Override
     public View newView(Context context, Cursor cursor, ViewGroup parent)  {
    super.newView(context, cursor, parent);

    final int pos = cursor.getPosition();
    System.out.println();

    ViewHolder holder = new ViewHolder();
    LayoutInflater inflater = LayoutInflater.from(context);
    final View view = inflater.inflate(R.layout.row, parent, false);
    CheckBox chxBox = (CheckBox)view.findViewById(R.id.itemChxBoxID);
    chxBox.setFocusable(false);
    chxBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(isChecked)
            {

                MainActivity activity = (MainActivity)view.getContext();
                activity.startActionMode();

            }
        }
    });

    int colorPos = cursor.getPosition() % colors.length;
    view.setBackgroundColor(colors[colorPos]);

    holder.mTitle = (TextView) view.findViewById(R.id.titleID);
    int col = cursor.getColumnIndex(DBAdapter.KEY_TITLE);
    holder.mTitle.setText(cursor.getString(col));
    holder.mTitle.setTag(holder);

    col = cursor.getColumnIndex(DBAdapter.KEY_DATE);
    Date date = new Date(cursor.getLong(col));
    holder.mDate = (TextView) view.findViewById(R.id.dateTimeOrLocationID);
    holder.mDate.setText(date.toString());
    holder.mDateString = date.toString();
    holder.mDate.setTag(holder);

    Calendar cal = Calendar.getInstance();
    long diff = cal.getTimeInMillis() - date.getTime();
    Date diffDate = new Date(diff);
    SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");

    holder.mCountDown = (TextView) view.findViewById(R.id.countdownID); 
    holder.mCountDown.setText(timeFormat.format(diffDate));
    holder.mCountDownString = timeFormat.format(diffDate);
    holder.mCountDown.setTag(holder);

    return view;
    }
  }

You'll have to reference the position of the item in your ListView that was selected, and then compare said position to your array of data.

IE. data[position] would be the item in your DataSet that was checked

It may be easier for you to get that position by overriding the getView() method, and setting the onCheckedChangedListener from inside there; this way you have an easy reference to what position is actually being checked

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM