简体   繁体   English

带有listBox的gmail之类的gmail:选择了哪些?

[英]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? 我的问题是:由于不使用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);
        }
    }
}

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. 您必须引用所选项目在ListView中的位置,然后将其与数据数组进行比较。

IE. IE浏览器 data[position] would be the item in your DataSet that was checked data [position]将是您的DataSet中已检查的项目

It may be easier for you to get that position by overriding the getView() method, and setting the onCheckedChangedListener from inside there; 通过覆盖getView()方法并从内部设置onCheckedChangedListener,可以更轻松地获得该位置。 this way you have an easy reference to what position is actually being checked 这样,您可以轻松参考实际检查的位置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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