我使用的android記事本教程代碼開始。在我的notes_row.xml中,我有一個textview來顯示筆記的標題以及最初由android隱藏的複選框:visibility =「gone」在Android中遍歷一個listView,使每個項目前的複選框可見
我希望當用戶打開選項菜單並單擊Mark時,列表中每個項目前面的複選框應該可見。但是,當我點擊模擬器中的標記時,只有第一個音符的複選框變爲可見。所以,我想遍歷整個列表,並設置可見性(0),以便所有複選框都可見。我是初學者,所以請詳細回答。提前致謝。
我的代碼片段:
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch(item.getItemId()) {
case INSERT_ID:
createNote();
return true;
case MARK_ID:
// only first item check box appears with following 2 lines:
CheckBox checkBox = (CheckBox) findViewById(R.id.checkBox1);
checkBox.setVisibility(0);
/* tried the following, but no chekbox appears with this:
Cursor c = mDbHelper.fetchAllNotes();
c.moveToPosition(0);
while(c.isAfterLast()){
CheckBox checkBox = (CheckBox) findViewById(R.id.checkBox1);
checkBox.setVisibility(0);
c.moveToNext();
}
*/
return true;
}
我的新代碼閱讀的答案後,仍只出現在第一項的複選框:
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch(item.getItemId()) {
case INSERT_ID:
createNote();
return true;
case MARK_ID:
CheckBox checkBox = (CheckBox) findViewById(R.id.checkBox1);
String[] lvItems = new String[]{NotesDbAdapter.KEY_TITLE};
MyAdapter arrAdapter = new MyAdapter(this, android.R.layout.simple_list_item_multiple_choice, lvItems);
arrAdapter.toggleCheckBoxVisibility(checkBox);
arrAdapter.notifyDataSetChanged();
return true;
return super.onMenuItemSelected(featureId, item);
}
這裏是我的適配器:
private class MyAdapter extends ArrayAdapter<String>{
public MyAdapter(Context context, int textViewResourceId,
String[] objects) {
super(context, textViewResourceId, objects);
// TODO Auto-generated constructor stub
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.notes_row, parent, false);
//String[] from = new String[]{NotesDbAdapter.KEY_TITLE};
return row;
//return super.getView(position, convertView, parent);
}
public boolean toggleCheckBoxVisibility(CheckBox checkBox){
checkBox.setVisibility(0);
super.notifyDataSetChanged();
return true;
}
}
該適配器您正在起訴在列表顯示各個項目 – Maneesh
我使用: 光標notesCursor = mDbHelper.fetchAllNotes(); \t \t startManagingCursor(notesCursor); \t \t //創建一個陣列指定我們希望在列表中顯示(僅TITLE) \t \t字符串[]從=新的String [] {NotesDbAdapter的字段。KEY_TITLE}; \t \t //,我們希望給這些字段(在這種情況下只是文本1)結合 \t \t INT []到新= INT [] {R.id.textView1}字段的陣列; \t \t //現在創建一個簡單的光標適配器,並將其設置到顯示 \t \t SimpleCursorAdapter筆記= \t \t \t \t新SimpleCursorAdapter(此,R.layout.notes_row,notesCursor,從,到); \t \t setListAdapter(附註); } –