0

我正在尝试使用搜索和标记功能实现联系人列表;除了搜索之外的一切都正常工作。我正在使用的代码如下所示。有人可以帮我解决这个问题吗?

public class MainActivity extends ListActivity implements Runnable {
    Cursor phoneCursor;
    public static Button done;
    ListView lv;
    /*    OpenHelper db_obj;*/
    final int PROGRESS_DIALOG = 0;
    ProgressDialog progressDialog;
    ArrayAdapter<ContactModel> adapter;
    ArrayList<HashMap<String, String>> CONTACT_DATA = new ArrayList<HashMap<String, String>>();

    @Override
    public void onCreate(Bundle saveinst) {
        super.onCreate(saveinst);
        progressDialog=ProgressDialog.show(MainActivity.this, "LoadContacts", "Please wait...", true, false);
        Thread loadContact = new Thread(this);
        loadContact.start();
    }

    public void run() {
        List<ContactModel>contactList = new ArrayList<ContactModel>();
        try {
            Looper.prepare();
            Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
                                                       null, Phone.DISPLAY_NAME + " ASC");

            while (phones.moveToNext()) {
                String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                String phoneNumber11 = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                contactList.add(get(name,phoneNumber11));
            }
            adapter = new MyContactListAdapter(MainActivity.this, contactList);
            handler.sendEmptyMessage(0);
        } catch(Exception exce) {
            exce.printStackTrace();
        }

        private final Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                    progressDialog.dismiss();
                    setListAdapter(adapter);
                    setContentView(R.layout.activity_main);
                    EditText editText;
                    /*   db_obj = new OpenHelper(getApplicationContext());*/
                    lv = getListView();
                    editText = (EditText)findViewById(R.id.editText1);
                    editText.addTextChangedListener(new TextWatcher() {
                        @Override
                        public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
                            // When user changed the Text
                            MainActivity.this.adapter.getFilter().filter(cs);
                            setListAdapter(adapter);
                        }

                        @Override
                        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                                                      int arg3) {
                            // TODO Auto-generated method stub.....
                        }

                        @Override
                        public void afterTextChanged(Editable arg0) {
                            // TODO Auto-generated method stub
                        }
                    });
                    done =  (Button)findViewById(R.id.button1done);
                    done.setVisibility(View.INVISIBLE);
                    done.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            // TODO Auto-generated method stub
                            MainActivity.this.finish();
                            Intent in = new Intent();
                            in.setClass(getApplicationContext(), MainActivity.class);
                            startActivity(in);
                        }
                    });
                    lv.setOnItemClickListener(new OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                                long arg3) {
                            // TODO Auto-generated method stub
                            System.out.println("Entered List Activity");
                        }
                    });
                }
            };

        public ContactModel get(String name,String number)
        {
            return new ContactModel(name,number);
        }
4

1 回答 1

0

尝试使用 AutoCompleteTextView,而不是 EditText。它的工作方式与您在此处所拥有的类似,只是搜索由 Android 处理,而不必在 onTextChanged 中使用过滤器。

myACTV.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub                          
            myACTV.showDropDown();

            myACTV.setOnItemClickListener(new OnItemClickListener() {                   
                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1,
                        int arg2, long arg3)
                    //Do something here
                }

            });
            return false;
        }

    });

为了更好地了解这是如何工作的,请查看http://developer.android.com/reference/android/widget/AutoCompleteTextView.html

于 2013-06-26T17:00:30.873 回答