在我的应用程序中,我正在获取联系人详细信息并在 listView 中列出,并且我正在使用 SimpleCursorAdapter 来获取联系人详细信息。问题是我想在我的联系人列表中添加一个搜索选项。
我用于此的代码是
SimpleCursorAdapter mAdapter;
MatrixCursor mMatrixCursor;
MatrixCursor dummyMatrixCursor;
int index = 0;
String FILENAMENAME = "Name.txt";
String FILENAMENAME2 = "name1.txt";
String FILENUMBER = "number.txt";
String FILENUMBER1 = "number1.txt";
String value;
OpenHelper db_obj;
EditText inputSearch;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listcontact);
inputSearch = (EditText)findViewById(R.id.inputSearch);
value = getIntent().getExtras().getString("First");
System.out.println("The Value"+value);
// The contacts from the contacts content provider is stored in this cursor
mMatrixCursor = new MatrixCursor(new String[] { "_id","name","photo","details"} );
dummyMatrixCursor = new MatrixCursor(new String[] {"id","name","homephone","mobilenumber","workphone","homeemail","workemail","company"});
// Adapter to set data in the listview
mAdapter = new SimpleCursorAdapter(getBaseContext(),
R.layout.lv_layout,
null,
new String[] { "name","photo","details"},
new int[] { R.id.tv_name,R.id.iv_photo,R.id.tv_details}, 0);
// Getting reference to listview.
final ListView lstContacts = (ListView) findViewById(R.id.lst_contacts);
// Setting the adapter to listview.
lstContacts.setAdapter(mAdapter);
// Creating an AsyncTask object to retrieve and load listview with contacts.
ListViewContactsLoader listViewContactsLoader = new ListViewContactsLoader();
// Starting the AsyncTask process to retrieve and load listview with contacts.
listViewContactsLoader.execute();
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
ContactListerClass.this.mAdapter.getFilter().filter(cs);
}
@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
}
});
lstContacts.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
System.out.println("You CLicked me");
System.out.println(mMatrixCursor);
if (mMatrixCursor.moveToPosition(arg2)){
String data = mMatrixCursor.getString(mMatrixCursor.getColumnIndex("name"));
System.out.println(data);
String data2 = mMatrixCursor.getString(mMatrixCursor.getColumnIndex("details"));
System.out.println("The Details are"+data2);
if(dummyMatrixCursor.moveToPosition(arg2)){
String data1111 = dummyMatrixCursor.getString(dummyMatrixCursor.getColumnIndex("name"));
System.out.println(".............."+data1111);
String dummyhomephone = dummyMatrixCursor.getString(dummyMatrixCursor.getColumnIndex("homephone"));
System.out.println("....."+dummyhomephone);
String mobilenumberdummy = dummyMatrixCursor.getString(dummyMatrixCursor.getColumnIndex("mobilenumber"));
System.out.println("..................."+mobilenumberdummy);
String workphonedummy = dummyMatrixCursor.getString(dummyMatrixCursor.getColumnIndex("workphone"));
System.out.println("..................."+workphonedummy);
String dummyhomeemail = dummyMatrixCursor.getString(dummyMatrixCursor.getColumnIndex("homeemail"));
System.out.println("..................."+dummyhomeemail);
String dummyworkemail = dummyMatrixCursor.getString(dummyMatrixCursor.getColumnIndex("workemail"));
System.out.println("..................."+dummyworkemail);
String dummycompany = dummyMatrixCursor.getString(dummyMatrixCursor.getColumnIndex("company"));
System.out.println("..................."+dummycompany);
String dummyworkphone = dummyMatrixCursor.getString(dummyMatrixCursor.getColumnIndex("workphone"));
System.out.println("..................."+dummyworkphone);
String details = "";
// Concatenating various information to single string
if(dummyhomephone != null && !dummyhomephone.equals("") )
details = " HomePhone : " + dummyhomephone + "\n";
if(mobilenumberdummy != null && !mobilenumberdummy.equals("") )
details += " MobilePhone : " + mobilenumberdummy + "\n";
if(workphonedummy != null && !workphonedummy.equals("") )
details += " WorkPhone : " + workphonedummy + "\n";
if(dummyhomeemail != null && !dummyhomeemail.equals("") )
details += " HomeEmail : " + dummyhomeemail + "\n";
if(dummyworkemail != null && !dummyworkemail.equals("") )
details += " WorkEmail : " + dummyworkemail + "\n";
if(dummycompany != null && !dummycompany.equals("") )
details += " CompanyName : " + dummycompany + "\n";
db_obj = new OpenHelper(getApplicationContext());
if(value.equals("First"))
{
//Inset Value into the first database....
db_obj.contact_insertionfirst(data1111,details);
//Write the first name to the text.
FileOutputStream fos1;
try {
fos1 = openFileOutput(FILENAMENAME, Context.MODE_PRIVATE);
fos1.write(data1111.getBytes());
System.out.println("---------Data written to files is:" + data1111);
fos1.close();
} catch (Exception e) {
e.printStackTrace();
}
//Write the number to the Text
FileOutputStream fos2;
try {
fos2 = openFileOutput(FILENUMBER, Context.MODE_PRIVATE);
fos2.write(mobilenumberdummy.getBytes());
System.out.println("---------Data written to files is:" + mobilenumberdummy);
fos2.close();
} catch (Exception e) {
e.printStackTrace();
}
}
else{
//Insert value into the second database
db_obj.contact_insertionSecond(data1111,details);
//Write the second name to text.
FileOutputStream fos1;
try {
fos1 = openFileOutput(FILENAMENAME2, Context.MODE_PRIVATE);
fos1.write(data.getBytes());
System.out.println("---------Data written to files is:" + data1111);
fos1.close();
} catch (Exception e) {
e.printStackTrace();
}
//Write the number to the Text
FileOutputStream fos2;
try {
fos2 = openFileOutput(FILENUMBER1, Context.MODE_PRIVATE);
fos2.write(mobilenumberdummy.getBytes());
System.out.println("---------Data written to files is:" + mobilenumberdummy);
fos2.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
mMatrixCursor.close();
ContactListerClass.this.finish();
Intent returnIntent = new Intent();
returnIntent.setClass(getApplicationContext(), MainActivity.class);
startActivity(returnIntent);
}
});
}
/** An AsyncTask class to retrieve and load listview with contacts */
private class ListViewContactsLoader extends AsyncTask<Void, Void, Cursor>{
@Override
protected Cursor doInBackground(Void... params) {
Uri contactsUri = ContactsContract.Contacts.CONTENT_URI;
// Querying the table ContactsContract.Contacts to retrieve all the contacts
Cursor contactsCursor = getContentResolver().query(contactsUri, null, null, null,
ContactsContract.Contacts.DISPLAY_NAME + " ASC ");
if(contactsCursor.moveToFirst()){
do{
long contactId = contactsCursor.getLong(contactsCursor.getColumnIndex("_ID"));
Uri dataUri = ContactsContract.Data.CONTENT_URI;
// Querying the table ContactsContract.Data to retrieve individual items like
// home phone, mobile phone, work email etc corresponding to each contact
Cursor dataCursor = getContentResolver().query(dataUri, null,
ContactsContract.Data.CONTACT_ID + "=" + contactId,
null, null);
String displayName="";
String nickName="";
String homePhone="";
String mobilePhone="";
String workPhone="";
String photoPath="" + R.drawable.tfff;
byte[] photoByte=null;
String homeEmail="";
String workEmail="";
String companyName="";
String title="";
if(dataCursor.moveToFirst()){
// Getting Display Name
displayName = dataCursor.getString(dataCursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME ));
do{
// Getting NickName
if(dataCursor.getString(dataCursor.getColumnIndex("mimetype")).equals(ContactsContract.CommonDataKinds.Nickname.CONTENT_ITEM_TYPE))
nickName = dataCursor.getString(dataCursor.getColumnIndex("data1"));
// Getting Phone numbers
if(dataCursor.getString(dataCursor.getColumnIndex("mimetype")).equals(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)){
switch(dataCursor.getInt(dataCursor.getColumnIndex("data2"))){
case ContactsContract.CommonDataKinds.Phone.TYPE_HOME :
homePhone = dataCursor.getString(dataCursor.getColumnIndex("data1"));
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE :
mobilePhone = dataCursor.getString(dataCursor.getColumnIndex("data1"));
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_WORK :
workPhone = dataCursor.getString(dataCursor.getColumnIndex("data1"));
break;
}
}
// Getting EMails
if(dataCursor.getString(dataCursor.getColumnIndex("mimetype")).equals(ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE ) ) {
switch(dataCursor.getInt(dataCursor.getColumnIndex("data2"))){
case ContactsContract.CommonDataKinds.Email.TYPE_HOME :
homeEmail = dataCursor.getString(dataCursor.getColumnIndex("data1"));
break;
case ContactsContract.CommonDataKinds.Email.TYPE_WORK :
workEmail = dataCursor.getString(dataCursor.getColumnIndex("data1"));
break;
}
}
// Getting Organization details
if(dataCursor.getString(dataCursor.getColumnIndex("mimetype")).equals(ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE)){
companyName = dataCursor.getString(dataCursor.getColumnIndex("data1"));
title = dataCursor.getString(dataCursor.getColumnIndex("data4"));
}
// Getting Photo
if(dataCursor.getString(dataCursor.getColumnIndex("mimetype")).equals(ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE)){
photoByte = dataCursor.getBlob(dataCursor.getColumnIndex("data15"));
if(photoByte != null) {
Bitmap bitmap = BitmapFactory.decodeByteArray(photoByte, 0, photoByte.length);
// Getting Caching directory
File cacheDirectory = getBaseContext().getCacheDir();
// Temporary file to store the contact image
File tmpFile = new File(cacheDirectory.getPath() + "/wpta_"+contactId+".png");
// The FileOutputStream to the temporary file
try {
FileOutputStream fOutStream = new FileOutputStream(tmpFile);
// Writing the bitmap to the temporary file as png file
bitmap.compress(Bitmap.CompressFormat.PNG,100, fOutStream);
// Flush the FileOutputStream
fOutStream.flush();
//Close the FileOutputStream
fOutStream.close();
} catch (Exception e) {
e.printStackTrace();
}
photoPath = tmpFile.getPath();
}
}
}while(dataCursor.moveToNext());
String details = "";
// Concatenating various information to single string
if(homePhone != null && !homePhone.equals("") )
details = "HomePhone : " + homePhone + "\n";
if(mobilePhone != null && !mobilePhone.equals("") )
details += "MobilePhone : " + mobilePhone + "\n";
if(workPhone != null && !workPhone.equals("") )
details += "WorkPhone : " + workPhone + "\n";
if(nickName != null && !nickName.equals("") )
details += "NickName : " + nickName + "\n";
if(homeEmail != null && !homeEmail.equals("") )
details += "HomeEmail : " + homeEmail + "\n";
if(workEmail != null && !workEmail.equals("") )
details += "WorkEmail : " + workEmail + "\n";
if(companyName != null && !companyName.equals("") )
details += "CompanyName : " + companyName + "\n";
if(title != null && !title.equals("") )
details += "Title : " + title + "\n";
// Adding id, display name, path to photo and other details to cursor
mMatrixCursor.addRow(new Object[]{ Long.toString(contactId),displayName,photoPath,details});
dummyMatrixCursor.addRow(new Object[]{ Long.toString(contactId),displayName,homePhone,mobilePhone,workPhone,homeEmail,workEmail,companyName});
}
}while(contactsCursor.moveToNext());
}
return mMatrixCursor;
}
@Override
protected void onPostExecute(Cursor result) {
// Setting the cursor containing contacts to listview
mAdapter.swapCursor(result);
}
}
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
Intent in =new Intent();
in.setClass(getApplicationContext(), MainActivity.class);
startActivity(in);
super.onBackPressed();
}
但即使在编辑文本中插入值之后也没有任何变化。有人请帮我快速解决