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