1

我有一個使用CursorAdapter填充列表視圖的列表視圖。我希望允許用戶同時選擇多個項目並對所有選定的項目執行操作(如刪除項目)。我嘗試將CheckBox項目添加到項目item_vocab.xml的佈局xml中。複選框出現在每個項目旁邊,但是我無法弄清楚如何跟蹤選中項目的狀態。我也嘗試加入setChoiceMode(ListView.CHOICE_MODE_MULTIPLE)來代替,但複選框沒有顯示出來。如何添加複選框到使用CursorAdapter的列表視圖

下面是我的一些代碼:

item_vocab.xml

<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:longClickable="true"> 

     <ImageView 
      android:layout_width="36sp" 
      android:layout_height="36sp" 
      android:id="@+id/vocabLevel" 
      android:layout_gravity="right" 
      android:src="@drawable/level_bars" 
      android:scaleType="fitXY" 
      android:contentDescription="@string/vocab_level" 
      android:layout_centerVertical="true" 
      android:layout_alignParentRight="true" 
      android:layout_alignParentEnd="true"/> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceLarge" 
      android:text="Large Text" 
      android:id="@+id/vocabName" 
      android:layout_alignParentTop="true" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentStart="true" 
      android:layout_toLeftOf="@+id/vocabLevel" 
      android:layout_toStartOf="@+id/vocabLevel"/> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceSmall" 
      android:text="Small Text" 
      android:id="@+id/vocabDefinition" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentStart="true" 
      android:layout_toLeftOf="@+id/vocabLevel" 
      android:layout_toStartOf="@+id/vocabLevel" 
      android:layout_below="@id/vocabName"/> 


    </RelativeLayout> 

activity_my_vocab.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        xmlns:tools="http://schemas.android.com/tools" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        tools:context="charlesli.com.personalvocabbuilder.io.MyVocabActivity"> 

     <ListView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/mVocabList" 
      android:layout_alignParentTop="true" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentStart="true"/> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/empty_text_view" 
      android:id="@android:id/empty" 
      android:layout_alignParentTop="true" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentStart="true"/> 

    </RelativeLayout> 

MyVocabActivity.java

public class MyVocabActivity extends ActionBarActivity { 

     private VocabCursorAdapter mVocabAdapter; 
     private ListView mVocabListView; 
     private TextView mEmptyTextView; 
     private Cursor mCursor; 
     private String mSelectedVocab; 
     private CheckBox mCheckBox; 

     private VocabDbHelper mDbHelper = new VocabDbHelper(this); 

     private ArrayList<String> mCheckedItems = new ArrayList<String>(); 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_my_vocab); 

      mVocabListView = (ListView) findViewById(R.id.mVocabList); 
      mEmptyTextView = (TextView) findViewById(android.R.id.empty); 
      mVocabListView.setEmptyView(mEmptyTextView); 
      mCursor = mDbHelper.getCursorMyVocab(mDbHelper); 
      mVocabAdapter = new VocabCursorAdapter(this, mCursor, 0); 
      mVocabListView.setAdapter(mVocabAdapter); 
      mVocabListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); 
      mVocabListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { 
       @Override 
       public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { 
        mSelectedVocab = (String) ((TextView) view.findViewById(R.id.vocabName)).getText(); 
        editVocabAlertDialog(mSelectedVocab, view, position, id); 
        return true; 
       } 
      }); 


     } 

VocabCursorAdapter.java

public class VocabCursorAdapter extends CursorAdapter { 

    private static final int DIFFICULT = 0; 
    private static final int FAMILIAR = 1; 
    private static final int EASY = 2; 
    private static final int PERFECT = 3; 


    public VocabCursorAdapter(Context context, Cursor c, int flags) { 
     super(context, c, 0); 
    } 


    @Override 
    public View newView(Context context, Cursor cursor, ViewGroup parent) { 
     return LayoutInflater.from(context).inflate(R.layout.item_vocab, parent, false); 
    } 


    @Override 
    public void bindView(View view, Context context, Cursor cursor) { 
     // Find fields to populate in inflated template 
     TextView tvVocabName = (TextView) view.findViewById(R.id.vocabName); 
     TextView tvVocabDefinition = (TextView) view.findViewById(R.id.vocabDefinition); 
     ImageView tvVocabLevel = (ImageView) view.findViewById(R.id.vocabLevel); 

     String vocab = cursor.getString(cursor.getColumnIndexOrThrow(VocabDbContract.DatabaseInfo.COLUMN_NAME_VOCAB)); 
     String definition = cursor.getString(cursor.getColumnIndexOrThrow(VocabDbContract.DatabaseInfo.COLUMN_NAME_DEFINITION)); 
     int level = cursor.getInt(cursor.getColumnIndexOrThrow(VocabDbContract.DatabaseInfo.COLUMN_NAME_LEVEL)); 
     tvVocabName.setText(vocab); 
     tvVocabDefinition.setText(definition); 
     if (level == DIFFICULT) { 
      tvVocabLevel.setImageResource(R.drawable.level_bars_difficult); 
     } 
     else if (level == FAMILIAR) { 
      tvVocabLevel.setImageResource(R.drawable.level_bars_familiar); 
     } 
     else if (level == EASY) { 
      tvVocabLevel.setImageResource(R.drawable.level_bars_easy); 
     } 
     else if (level == PERFECT) { 
      tvVocabLevel.setImageResource(R.drawable.level_bars_perfect); 
     } 
    } 
} 

請讓我知道如果我需要提供更多的信息。

+0

發佈您的適配器類 –

+0

@JaiSoni謝謝,我剛剛發佈了我的CursorAdapter類。 –

回答

0

你可以使用CustomListview來製作另一個具有textview和checkbox的xml,並在你的自定義適配器類中使用這個佈局來填充列表,然後它會像你想要的那樣工作。例如看看這個教程:

http://techlovejump.com/android-listview-with-checkbox/ 
+0

非常感謝!這是非常詳細和信息。我會盡快嘗試。但是,我看到他們使用(擴展)了ArrayAdapter而不是CursorAdapter(我使用過)。這會影響到什麼嗎?一旦我嘗試過,我會讓你知道結果。 –

相關問題