Reputation: 146
I already implemented CursorAdapter to show phone contact list, it works fine. Now I want to implement delete item on click.. But item will delete from only list, not from phone data base.. and delete function will implement in CursorAdapter. Tried but unable to do this.. Help me..
My code is here..
ImageButton remFrnd = (ImageButton) view.findViewById(R.id.remove_frnd);
remFrnd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Animation fadeOut = AnimationUtils.loadAnimation(context, R.anim.request_animate);
fadeOut.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {}
@Override
public void onAnimationRepeat(Animation animation) {}
@Override
public void onAnimationEnd(Animation animation) {
///////////////////////////////////////////////////////////////////////////
//final int position = listContacts.getPositionForView((View) view.getParent());
// datalist.remove(position);
deleteRecordWithId(itemId);
cursor.requery();
// myAdapter.notifyDataSetChanged();
/////////////////////////////////////////////////////////
notifyDataSetChanged();
}
});
view.startAnimation(fadeOut);
}
}
Upvotes: 0
Views: 2223
Reputation: 145
If you want to delete the entry from the list ONLY (not from the phone book) make sure your adapter does not load the data from the phone book every time you fire DataSetChanged - like a cursor adapter would probably do... Instead load the data from the phone book put it into some datastructure and then an ArrayAdapter or something like this...
EDIT:
private ListView initializeListView() {
ListView lv = (ListView)findViewById(R.id.listView);
ArrayList<Person> persons = loadPersonsFromMyChosenStorage();
if(persons==null){
//we haven't stored persons yet
persons = new ArrayList<Person>();
String whereName = ContactsContract.Data.MIMETYPE + " = ?";
String[] whereNameParams = new String[] { ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE };
Cursor cursor = getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, whereName, whereNameParams, ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME);
while (cursor.moveToNext()) {
String firstname = cursor.getString(cursor.getColumnIndex(
ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
String lastname = cursor.getString(cursor.getColumnIndex(
ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));
if(firstname!=null&&lastname!=null){
persons.add(new SimplePerson(firstname,lastname));
}
}
storePersonsToMyChosenStorage(persons);
}
if(lv!=null) {
lv.setAdapter(new ArrayAdapter<Person>(this, android.R.layout.simple_list_item_single_choice, persons));
lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
}
return lv;
}
Upvotes: 1