2011-07-06 50 views
4

我在多選「Ok,Cancel」對話框中顯示聯繫人。我已經爲在對話框中顯示聯繫人的適配器實現了Filterable。問題是,一旦我在使用類型前嘗試選擇(檢查)聯繫人時,將檢查該特定位置中的複選框,而不是聯繫人。帶有警報對話框中複選框的列表視圖

初始畫面是這樣

提前輸入後,

當我打退格鍵,看到原來的列表中,選定的聯繫人未選中。

這是我的活動。

Cursor c = getContentResolver().query(People.CONTENT_URI, 
    PROJECTION, 
    null, 
    null, 
    Contacts.People.DEFAULT_SORT_ORDER 
); 

startManagingCursor(c); 
ListAdapter adapter1 = new ContactListAdapter(this, c); 
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
View view = (View) inflater.inflate(R.layout.list_view, null); 
listView = (ListView) view.findViewById(R.id.contactlist); 
listView.setTextFilterEnabled(true); 
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); 
listView.setAdapter(adapter1); 
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); 
listView.setOnItemSelectedListener(this); 
alertDialog.setView(view); 

適配器是這樣:

public class ContactListAdapter extends CursorAdapter implements Filterable 
{ 
    public static final String[] PEOPLE_PROJECTION = new String[] { 
     People._ID, 
     People.NAME, 
     People.NUMBER 
    }; 

    public ContactListAdapter(Context context, Cursor c) { 
     super(context, c); 
     mContent = context.getContentResolver(); 
    } 

    @Override 
    public View newView(Context context, Cursor cursor, ViewGroup parent) { 
     final LayoutInflater inflater = LayoutInflater.from(context); 
     final TextView view = (TextView) inflater.inflate(
      android.R.layout.simple_list_item_multiple_choice, 
      parent, 
      false 
     ); 
     view.setText(cursor.getString(1)); 
     return view; 
    } 

    @Override 
    public void bindView(View view, Context context, Cursor cursor) { 
     ((TextView) view).setTag(cursor.getLong(0)); 
     ((TextView) view).setText(cursor.getString(1)); 
    } 

    @Override 
    public String convertToString(Cursor cursor) { 
     return cursor.getString(1); 
    } 

    @Override 
    public Cursor runQueryOnBackgroundThread(CharSequence constraint) { 
     if (getFilterQueryProvider() != null) { 
      return getFilterQueryProvider().runQuery(constraint); 
     } 

     StringBuilder buffer = null; 
     String[] args = null; 
     if (constraint != null) { 
      buffer = new StringBuilder(); 
      buffer.append("UPPER("); 
      buffer.append(Contacts.ContactMethods.NAME); 
      buffer.append(") GLOB ?"); 
      args = new String[] { constraint.toString().toUpperCase() + "*" }; 
     } 

     return mContent.query(Contacts.People.CONTENT_URI, 
      PEOPLE_PROJECTION, 
      buffer == null ? null : buffer.toString(), 
      args, 
      Contacts.People.DEFAULT_SORT_ORDER 
     ); 
    } 

    private ContentResolver mContent; 

} 
+0

我認爲你需要添加到控制複選框按鈕行爲的方法你適配器...我看不到,可能是原因... –

回答

0

「當我打退格鍵,看到原來的列表中,選定的聯繫人未選中。」

嘗試爲您的dialog.setOnCancleListener

和onCancel調用adapter.notifyDataSetChanged()

也許是幫助