如何自定义自动完成TextView?

时间:2015-07-11 03:58:23

标签: java android listview autocompletetextview

我试图打电话目录的搜索任务,我不知道问题出在哪里。

这是我的代码:

适应器:

public class ListContactAdapter extends ArrayAdapter<Contact> {
    private  Context context;
    private int layoutResourceId;
  private List<Contact> contact;
    public ListContactAdapter(Context context, int layoutResourceId, List<Contact> contact) {
        super(context, layoutResourceId, contact);
        this.context = context;
        this.layoutResourceId = layoutResourceId;
        this.contact = contact;
    }
    public View getView(final int position, View convertView, ViewGroup parent) {
        TextView tvPhoneNumber;
        TextView tvName;
        View row = convertView;
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);
        tvPhoneNumber= (TextView) row.findViewById(R.id.textViewPhoneNo);
        tvName = (TextView) row.findViewById(R.id.textViewName);
        final Contact data = contact.get(position);
        tvPhoneNumber.setText(data.getPhoneNumber());
        tvName.setText(data.getName());
        return row;

    }
}

主要:

public class CustomCompleTextView extends ActionBarActivity {
AutoCompleteTextView auto;
    List<Contact> listContact =new ArrayList<>();
    ListView listViewContact;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_complete_textview);
        ContentResolver cr = getContentResolver();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        if (cur.getCount() > 0) {
            while (cur.moveToNext()) {
                Contact a = new Contact(null,null);
                String id = cur.getString(cur.getColumnIndex(
                        ContactsContract.Contacts._ID));
                String name = cur.getString(cur.getColumnIndex(
                        ContactsContract.Contacts.DISPLAY_NAME));
                if (Integer.parseInt(cur.getString(cur.getColumnIndex(
                        ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                    Cursor pCur = cr.query(
                            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                            null,
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
                            new String[]{id}, null);
                    while (pCur.moveToNext()) {
                        a.setName(name);
                        String phoneNumber = pCur.getString(pCur.getColumnIndex(
                                ContactsContract.CommonDataKinds.Phone.NUMBER));
                        a.setPhoneNumber(phoneNumber);
                        listContact.add(a);
                    }
                    pCur.close();
                }
            }
        }

        ListContactAdapter adapter = new ListContactAdapter(this,R.layout.layout_item,listContact);
      auto= (AutoCompleteTextView) findViewById(R.id.textViewAuto);

        auto.setThreshold(3);
        auto.setAdapter(adapter);

    }

我尝试使用ListView运行,但它确实有效。 任何人都可以告诉我我错在哪里,非常感谢。

0 个答案:

没有答案