Android:如何使用CursorAdapter中的OnClickListener动态设置选项菜单

时间:2014-01-28 13:26:25

标签: java android listview checkbox android-cursoradapter

我在MainActivity中设置了一个带有自定义CursorAdapter的ListView,当单击“编辑”菜单时,CursorAdapter会更改其视图以设置CheckBox es的可见性。

此时,我发现onClickListener知道CheckBox的更改不在MainAcitivity但在CursorAdapter类中。因此,我编码了ArrayList只要点击CheckBox,就会在MainActivity中恢复,并在CheckBox中调用方法时返回。

问题是,如果选中"DELETE",我希望更改选项菜单(例如,"MOVE"并且CheckBox菜单在没有CheckBox时无效单击MainActivity后单击检查并启用,但不知道如何。

我的CursorAdapterpublic class MainActivity extends Activity { private MenuItem itEdit, itSort, itView, itSelectAll, itDeselectAll, itDelete, itMove; private boolean isEditChecked; private ListView lvWords; private WordAdapter wordadapter; private ArrayList<Integer> checkedWords; private Cursor wordCursor; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lvWords = (ListView) findViewById(R.id.lv_words); dbhandler = DBHandler.open(this); wordCursor = dbhandler.selectWords(activeNoteId); wordadapter = new WordAdapter(this, wordCursor, isEditChecked); lvWords.setAdapter(wordadapter); .... ... .. } protected void onResume() { super.onResume(); refreshListViews(); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); itEdit = menu.findItem(R.id.it_edit); itSort = menu.findItem(R.id.it_sort); itView = menu.findItem(R.id.it_view); itSelectAll = menu.findItem(R.id.it_select_all); itDeselectAll = menu.findItem(R.id.it_deselect_all); itDelete = menu.findItem(R.id.it_delete); itMove = menu.findItem(R.id.it_move); itEdit.setVisible(true); itSort.setVisible(true); itView.setVisible(true); itSelectAll.setVisible(false); itDeselectAll.setVisible(false); itDelete.setVisible(false); itMove.setVisible(false); return true; } @Override public boolean onPrepareOptionsMenu(Menu menu) { .... ... return super.onPrepareOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.it_edit: onEditMenuClicked(); break; case R.id.it_delete: checkedWords = wordadapter.getCheckedArrayList(); final int checkedDeleteSize = checkedWords.size(); .... ... break; } return super.onOptionsItemSelected(item); } // when edit menu clicked private void onEditMenuClicked() { isEditChecked = !isEditChecked; itSort.setVisible(!isEditChecked); itView.setVisible(!isEditChecked); itSelectAll.setVisible(isEditChecked); itDeselectAll.setVisible(isEditChecked); itDelete.setVisible(isEditChecked); itMove.setVisible(isEditChecked); wordadapter.setVisibility(isEditChecked); refreshListViews(); } private void refreshListViews() { wordCursor.close(); wordCursor = dbhandler.selectWords(activeNoteId); wordadapter.changeCursor(wordCursor); setVisibilityViews(); } }

的某些部分

MainActivity.java

public class WordAdapter extends CursorAdapter {

private boolean isCheckBoxVisable;
private ArrayList<Integer> checkedArray;
private boolean setCheck;

public WordAdapter(Context context, Cursor c, boolean check) {
    super(context, c);
    isCheckBoxVisable = check;
    checkedArray = new ArrayList<Integer>();
    setCheck = false;
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
    CheckBox check = (CheckBox) view.findViewById(R.id.cb_list_word_check);

    final int id = cursor.getInt(cursor.getColumnIndex("_id"));

    if(isCheckBoxVisable)
        check.setVisibility(View.VISIBLE);
    else check.setVisibility(View.GONE);

    check.setOnCheckedChangeListener(new OnCheckedChangeListener() {    
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            /////// I think something is added here /////

            if(isChecked){
                checkedArray.add(id);
            }
            else{
                checkedArray.remove(new Integer(id));
            }
        }
    });
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    Log.d("newView()","start of newView");
    LayoutInflater inflater = LayoutInflater.from(context);
    View v = inflater.inflate(R.layout.word_list_row, parent, false);
    Log.d("newView()","end of newView");
    return v;
}

// set visibility of checkbox
public void setVisibility(boolean check){
    isCheckBoxVisable = check;
}

public ArrayList<Integer> getCheckedArrayList(){
    return checkedArray;
}
}

WordAdapter.java

{{1}}

1 个答案:

答案 0 :(得分:0)

如果要执行菜单更新,则必须致电invalidateOptionsMenu()。如果您根据活动所处的状态更新(重新创建)菜单,它会回拨onCreateOptionsMenu(Menu menu)

菜单中的运行时更改详细解释了here