Android 自动完成 textview 和 Cursoradapter

标签 android

嗨 我需要创建一个自动完成 TextView ,其中建议应该是联系人姓名和组织名称的组合,我使用的是 2.1 操作系统。我可以显示联系人姓名,但不能显示组织名称,我该怎么做。

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);
    }

}

最佳答案

要检索组织名称,您应该按以下方式搜索数据表:

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:我没有试过这段代码,但是非常相似。我希望这也能奏效,或者至少能有所帮助

关于Android 自动完成 textview 和 Cursoradapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4758107/

相关文章:

android - Rails protect_from_forgery 与移动应用程序

Android如何以编程方式创建三角形和矩形?

android - 我们可以在 Android 上的 OpenGL ES 上使用非 2 的幂纹理吗?

Android UI 设计模式 - 选项菜单

java - eclipse 中缺少 android.jar

android - 如何模仿谷歌地图的底页 3 阶段行为?

java - Firebase : NumberFormatException on reference. 子 ("key").toString();

android - 提升在 Android L 中做什么?

android - Realm 什么是 RealmObject.isValid()?

php - 智能手机应用程序的使用如何影响其网站版本排名?