1

我有一个包含复选框和一些文本的列表视图。当一个复选框被选中时,它会打开操作模式。然后您可以删除该项目。我的问题是:我如何在 deleteCurrentItem() 中找出选中了哪个复选框,因为我不使用 ListView 的内部选中状态?

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

1 回答 1

0

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

IE。data[position] 将是您的 DataSet 中被检查的项目

通过覆盖 getView() 方法并从那里设置 onCheckedChangedListener 可能更容易获得该位置;这样您就可以轻松参考实际检查的位置

于 2013-05-18T18:53:21.157 回答