我有一个带有复选框以及一些文本的列表视图。选中复选框后,它将打开操作模式。然后,您可以删除该项目。我的问题是:由于不使用ListView的内部检查状态,如何在deleteCurrentItem()中找出选中了哪个复选框?

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);
        }
    }
}


适配器类:

  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;
    }
  }

最佳答案

您必须引用所选项目在ListView中的位置,然后将其与数据数组进行比较。

IE浏览器data [position]将是您的DataSet中已检查的项目

通过覆盖getView()方法并从内部设置onCheckedChangedListener,可以更轻松地获得该位置。这样,您可以轻松参考实际检查的位置

09-03 20:37