2012-05-01 45 views
5

我想實現一個自定義AutoCompleteTextView從顯示聯繫人姓名,電話號碼類型和電話號碼的建議列表中選擇聯繫人的電話號碼。我創建了一個自定義的CursorAdapter,爲每個建議定義和設置我的Layout和TextView,並根據用戶輸入的文本通過runQueryOnBackgroundThread查詢聯繫人。我遇到的問題是,對於前兩個輸入值(例如「ab」表示「abcd」和「abyz」)而言,建議似乎是正確的,但對於超出此範圍的任何內容(例如「abc」表示「abyz」)則不適用。對於後者,當選擇「abyz」建議時,返回「abcd」的值。Android - 自定義AutoCompleteTextView CursorAdaptor - 建議行爲

代碼的主要活動:

final ContactInfo cont = new ContactInfo(ctx); 
    Cursor contacts = cont.getContacts2(null); 
    startManagingCursor(contacts); 

    ContactsAutoCompleteCursorAdapter adapter = new ContactsAutoCompleteCursorAdapter(this, contacts); 
    mPersonText.setAdapter(adapter); 
    mPersonText.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

     public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 
      long arg3) { 
      Cursor cursor = (Cursor) arg0.getItemAtPosition(arg2); 
      String number = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
      mPersonNum.setText(number); 
     } 
    }); 

代碼爲我的聯繫人類返回所有聯繫人光標:

public Cursor getContacts2(String where) 
{ 
    Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI; 
    String[] projection = new String[] { 
      ContactsContract.CommonDataKinds.Phone._ID, 
      ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, 
      ContactsContract.CommonDataKinds.Phone.TYPE, 
      ContactsContract.CommonDataKinds.Phone.NUMBER}; 

    Cursor people = ctx.getContentResolver().query(uri, projection, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " COLLATE LOCALIZED ASC"); 

    return people; 
} 

代碼爲我的CursorAdapter:

public class ContactsAutoCompleteCursorAdapter extends CursorAdapter implements Filterable { 

private TextView mName, mType, mNumber; 
private ContentResolver mContent; 

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

@Override 
public View newView(Context context, Cursor cursor, ViewGroup parent) { 

    final LayoutInflater mInflater = LayoutInflater.from(context); 
    final View ret = mInflater.inflate(R.layout.contacts_auto_list, null); 

    mName = (TextView) ret.findViewById(R.id.name); 
    mType = (TextView) ret.findViewById(R.id.phonetype); 
    mNumber = (TextView) ret.findViewById(R.id.phonenum); 

    return ret; 
} 

@Override 
public void bindView(View view, Context context, Cursor cursor) { 

    int nameIdx = cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME); 
    int typeIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE); 
    int numberIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER); 

    String name = cursor.getString(nameIdx); 
    int type = cursor.getInt(typeIdx); 
    String number = cursor.getString(numberIdx); 

    mName.setText(name); 
    if (type == 1) {mType.setText("Home");} 
    else if (type == 2) {mType.setText("Mobile");} 
    else if (type == 3) {mType.setText("Work");} 
    else {mType.setText("Other");} 
    mNumber.setText(number); 

} 

@Override 
public String convertToString(Cursor cursor) { 
    int nameCol = cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME); 
    String name = cursor.getString(nameCol); 
    return name; 
} 

@Override 
public Cursor runQueryOnBackgroundThread(CharSequence constraint) { 
    // this is how you query for suggestions 
    // notice it is just a StringBuilder building the WHERE clause of a cursor which is the used to query for results 
    if (getFilterQueryProvider() != null) { return getFilterQueryProvider().runQuery(constraint); } 

    String[] projection = new String[] { 
      ContactsContract.CommonDataKinds.Phone._ID, 
      ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, 
      ContactsContract.CommonDataKinds.Phone.TYPE, 
      ContactsContract.CommonDataKinds.Phone.NUMBER}; 

    return mContent.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection, 
      "UPPER(" + ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + ") LIKE '" + constraint.toString().toUpperCase() + "%'", null, 
      ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC"); 
} 

}

如上所述,當用戶在AutoCompleteTextView中輸入「ab」時,建議是「abcd」和「abyz」,但是當用戶鍵入「abc」時,建議只是「abyz」。當用戶在這種情況下選擇「abyz」時,返回「abcd」的值。這裏有兩個截圖,顯示什麼,我試圖描述:

enter image description hereenter image description here

我讀過的每一個問題,我可以在這裏找到和其他地方,但似乎無法弄清楚這一點。我對Android開發相當陌生,所以如果我的錯誤很簡單,我會提前道歉。提前致謝!

回答

2

我似乎在更多的研究後回答了我自己的問題。移動從NewView的功能將bindView功能我textViews的意見,設置似乎做的伎倆,我認爲是有道理的......

@Override 
public View newView(Context context, Cursor cursor, ViewGroup parent) { 

    final LayoutInflater mInflater = LayoutInflater.from(context); 
    final View ret = mInflater.inflate(R.layout.contacts_auto_list, null); 

    return ret; 
} 

@Override 
public void bindView(View view, Context context, Cursor cursor) { 

    int nameIdx = cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME); 
    int typeIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE); 
    int numberIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER); 

    String name = cursor.getString(nameIdx); 
    int type = cursor.getInt(typeIdx); 
    String number = cursor.getString(numberIdx); 

    mName = (TextView) view.findViewById(R.id.name); 
    mType = (TextView) view.findViewById(R.id.phonetype); 
    mNumber = (TextView) view.findViewById(R.id.phonenum); 

    mName.setText(name); 
    if (type == 1) {mType.setText("Home");} 
    else if (type == 2) {mType.setText("Mobile");} 
    else if (type == 3) {mType.setText("Work");} 
    else {mType.setText("Other");} 
    mNumber.setText(number); 
} 
0

你已經公共光標runQueryOnBackgroundThread功能在您的適配器,你不需要調用第二次光標活動

你不需要使用getContacts2功能

活動

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.sms_send); 

    Cursor contacts = null; 

    mAdapter= new ContactsAutoCompleteCursorAdapter(this, contacts); 
    mTxtPhoneNo = (AutoCompleteTextView) findViewById(R.id.mmWhoNo); 
    mTxtPhoneNo.setAdapter(mAdapter); 

    mTxtPhoneNo.setOnItemClickListener(new OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 
       long arg3) { 
      // TODO Auto-generated method stub 
      Cursor cursor = (Cursor) arg0.getItemAtPosition(arg2); 
      String number = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
      mTxtPhoneNo.setText(number); 

     } 
    }); 


} 

適配器

public class ContactsAutoCompleteCursorAdapter extends CursorAdapter implements Filterable { 

private TextView mName, mType, mNumber; 
private ContentResolver mContent; 

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




@Override 
public View newView(Context context, Cursor cursor, ViewGroup parent) { 

    final LayoutInflater mInflater = LayoutInflater.from(context); 
    final View ret = mInflater.inflate(R.layout.custcontview, null); 

    return ret; 
} 

@Override 
public void bindView(View view, Context context, Cursor cursor) { 

    int nameIdx = cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME); 
    int typeIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE); 
    int numberIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER); 

    String name = cursor.getString(nameIdx); 
    int type = cursor.getInt(typeIdx); 
    String number = cursor.getString(numberIdx); 

    mName = (TextView) view.findViewById(R.id.ccontName); 
    mType = (TextView) view.findViewById(R.id.ccontType); 
    mNumber = (TextView) view.findViewById(R.id.ccontNo); 

    mName.setText(name); 
    if (type == 1) {mType.setText("Home");} 
    else if (type == 2) {mType.setText("Mobile");} 
    else if (type == 3) {mType.setText("Work");} 
    else {mType.setText("Other");} 
    mNumber.setText(number); 
} 


@Override 
public String convertToString(Cursor cursor) { 
    int nameCol = cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME); 
    String name = cursor.getString(nameCol); 
    return name; 
} 




@Override 
public Cursor runQueryOnBackgroundThread(CharSequence constraint) { 
    // this is how you query for suggestions 
    // notice it is just a StringBuilder building the WHERE clause of a cursor which is the used to query for results 



if (constraint==null) 
    return null; 

    if (getFilterQueryProvider() != null) { return getFilterQueryProvider().runQuery(constraint); } 

    String[] projection = new String[] { 
      ContactsContract.CommonDataKinds.Phone._ID, 
      ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, 
      ContactsContract.CommonDataKinds.Phone.TYPE, 
      ContactsContract.CommonDataKinds.Phone.NUMBER}; 

    return mContent.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection, 
      "UPPER(" + ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + ") LIKE '%" + constraint.toString().toUpperCase() + "%' or UPPER(" + ContactsContract.CommonDataKinds.Phone.NUMBER + ") LIKE '%" + constraint.toString().toUpperCase() + "%' ", null, 
      ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC"); 
} 

} 

我也查詢

添加查詢電話號碼查詢
相關問題