Salut J'ai besoin de créer un autocompletetextview dans lequel suggestion devrait être une combinaison de nom de contact et le nom de l'organisation, je suis en utilisant 2.1 os. Je peux afficher le nom du contact, mais pas le nom de l'organisation, comment puis-je le faire.Android Autocomplete textview et Cursoradapter
public class myactivity extends Activity {
/** Called when the activity is first created. */
public static EditText ed;
private String id;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
AutoCompleteTextView act=(AutoCompleteTextView)findViewById(R.id.AutoCompleteTextView01);
ed=(EditText)findViewById(R.id.EditText01);
ContentResolver content = getContentResolver();
Cursor cursor = content.query(ContactsContract.Contacts.CONTENT_URI,PEOPLE_PROJECTION, null, null, null);
ContactListAdapter adapter = new ContactListAdapter(this, cursor);
act.setThreshold(0);
act.setAdapter(adapter);
}
public static final String[] PEOPLE_PROJECTION = new String[] {
ContactsContract.Contacts._ID,
Contacts.DISPLAY_NAME,
ContactsContract.Contacts.HAS_PHONE_NUMBER
};
}
class ContactListAdapter extends CursorAdapter implements Filterable {
private ContentResolver mCR;
public ContactListAdapter(Context context, Cursor c) {
super(context, c);
mCR = context.getContentResolver();
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
((TextView) view).setText(cursor.getString(1));
}
@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_dropdown_item_1line, parent, false);
view.setText(cursor.getString(1));
return view;
}
@Override
public String convertToString(Cursor cursor) {
return cursor.getString(1);
}
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.DISPLAY_NAME);
buffer.append(") GLOB ?");
args = new String[] { constraint.toString().toUpperCase() + "*" };
}
return mCR.query(ContactsContract.Contacts.CONTENT_URI,myactivity.PEOPLE_PROJECTION , buffer == null ? null : buffer.toString(), args,
Contacts.DISPLAY_NAME);
}
}
Avez-vous résolu ceci? Si résolu, puis ajouter la réponse et l'accepter.Il pourrait aider sur d'autres personnes face à un problème comme vous faites face .. – nKandel
duplication possible de [AutoCompeleteTextView comme application de messagerie Android par défaut] (http://stackoverflow.com/questions/3905092/autocompeletetextview- comme-android-default-messagerie-application) –