喜 我需要创建一个autocompletetextview中建议应的联系人姓名和组织名称的组合,现在用2.1操作系统。我可以显示联系人的姓名,但orgnization名没有,我怎么能做到这一点。
公共类myactivity延伸活动{
/ **第一次创建活动时调用。 * /
公共静态的EditText编辑;
私人字符串ID;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
AutoCompleteTextView行为=(AutoCompleteTextView)findViewById(R.id.AutoCompleteTextView01);
ED =(EditText上)findViewById(R.id.EditText01);
ContentResolver的内容= getContentResolver();
光标光标= content.query(ContactsContract.Contacts.CONTENT_URI,PEOPLE_PROJECTION,NULL,NULL,NULL);
ContactListAdapter适配器=新ContactListAdapter(这一点,光标);
act.setThreshold(0);
act.setAdapter(适配器);
}
公共静态最后的String [] PEOPLE_PROJECTION =新的String [] {
ContactsContract.Contacts._ID,
Contacts.DISPLAY_NAME,
ContactsContract.Contacts.HAS_PHONE_NUMBER
};
}
类ContactListAdapter扩展的CursorAdapter实现过滤的{
私人ContentResolver的MCR;
公共ContactListAdapter(上下文的背景下,光标C){
超(背景下,C);
MCR = context.getContentResolver();
}
@覆盖
公共无效bindView(查看视图,上下文的背景下,光标光标){
((TextView中)查看).setText(cursor.getString(1));
}
@覆盖
公共查看NewView的(上下文的背景下,光标光标的ViewGroup父){
最后LayoutInflater充气= LayoutInflater.from(上下文);
最后的TextView视图=(TextView中)inflater.inflate(android.R.layout.simple_dropdown_item_1line,父母,假);
view.setText(cursor.getString(1));
返回查看;
}
@覆盖
公共字符串convertToString(光标指针){
返回cursor.getString(1);
}
公共光标runQueryOnBackgroundThread(CharSequence的约束){
如果(getFilterQueryProvider()!= NULL){
返回getFilterQueryProvider()runQuery(约束)。
}
StringBuilder的缓冲= NULL;
字串[] args = NULL;
如果(约束!= NULL){
缓冲区=新的StringBuilder();
buffer.append(UPPER();
buffer.append(Contacts.DISPLAY_NAME);
buffer.append()GLOB?);
的args =新的String [] {constraint.toString()与toUpperCase()+*};
}
返回mCR.query(ContactsContract.Contacts.CONTENT_URI,myactivity.PEOPLE_PROJECTION,缓冲== NULL空:buffer.toString(),指定参数时,
Contacts.DISPLAY_NAME);
}
}
解决方案
要检索的组织名称,你应该寻找的数据表中以下方式:
私有静态最后弦乐ORGANIZATION_PROJECTION =新的String [] {
ContactsContract.CommonDataKinds.Organization.COMPANY
}
公共字符串getOrganizationForContact(INT CONTACT_ID){
光标C;
字符串组织= NULL;
C = getContentResolver()查询(ContactsContract.Data.CONTENT_URI,
ORGANIZATION_PROJECTION,
ContactsContract.Data.MIMETYPE +=?和+ ContactsContract.Data.CONTACT_ID +=?,
新的String [] {ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE,+ CONTACT_ID},
空值
);
如果(C =空&安培;!&安培; c.moveToFirst()){
有机= c.getString(0);
}
c.close();
返回组织;
}
PS:我没有尝试这样做code,但一个非常类似的。我希望这也能工作,或者至少,它可以帮助
Hi i need to create an autocompletetextview in which suggestion should be a combination of Contact name and organization name ,am using 2.1 os. i can display the contact name ,but orgnization name not, how can i do this.
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);
}
}
解决方案
To retrieve the Organization name you should search the Data table in the following way:
private static final String ORGANIZATION_PROJECTION = new String[] {
ContactsContract.CommonDataKinds.Organization.COMPANY
}
public String getOrganizationForContact ( int contact_id ) {
Cursor c;
String org = null;
c = getContentResolver().query ( ContactsContract.Data.CONTENT_URI,
ORGANIZATION_PROJECTION,
ContactsContract.Data.MIMETYPE + "=? AND " + ContactsContract.Data.CONTACT_ID + " =?",
new String[] { ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE, "" + contact_id },
null
);
if ( c != null && c.moveToFirst() ) {
org = c.getString ( 0 );
}
c.close();
return org;
}
PS: I haven't tried this code, but a very similar one. I hope this will work too, or at least that it helps
上一篇:" android.util.AndroidRuntimeException:requestFeature()添加内容&QUOT之前,必须先调用;上的ShowDialog(dialogId)