2014-04-26 39 views
0

我已经实现了CursorAdapter来显示手机通讯录,它工作正常。现在我想要在点击时实现删除项目。但是项目将只从列表中删除,而不是从电话数据库中删除。删除功能将在CursorAdapter中执行。尝试,但无法做到这一点..帮助我..Android从光标适配器删除列表项目

我的代码是在这里..

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); 
    } 
} 
+0

你可以发布你的'deleteRecordWithId(itemId);'? – Meghna

+0

我正在尝试这个..但是这是数据库..所以他们没有必要.. – Dhiman

+0

den发布您的数据库类too.fisrt得到您的列表选择的项目,从数据库中删除它,并删除整行。 – Meghna

回答

1

如果你想(从电话本中不)从列表中删除该条目ONLY确保您的适配器不会在每次启动时从电话簿加载数据DataSetChanged - 就像游标适配器可能会做的那样... 将电话簿中的数据加载到某些数据结构中,然后使用ArrayAdapter或类似的东西..

编辑:

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; 
} 
+0

exactly.i要这个...但有一个小问题,当我重新打开应用程序,然后它显示所有,我想永远从列表中删除..plz帮助我.. – Dhiman

+0

加载您的数据一次,并坚持它(很多选项:[存储](http://developer.android.com/guide/topics/data/data-storage.html))之后,你从那里加载你的数据... – bgoeschi