dimanche 5 mars 2017

android:How to delete a ListView item with checkbox?

I am a beginner level android developer and was working on creating a basic To-Do list app. The app contains custom listview with a coloured circle to show priority, the title and a checkbox to check and delete the item when the user has done it. The app stores these to-dos using SQLite database and uses Loader to retrieve data.

What I'm trying is to delete the item from the list as soon as the checkbox is checked.

I tried to implement deletion task in the Adapter class like this (Also tried to animate deletion.):

CheckBox itemCheck = (CheckBox)view.findViewById(R.id.todo_checked);
    itemCheck.setChecked(false);
    itemCheck.setTag(priority);
    itemCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(isChecked){
                int idIndex = cursor.getColumnIndex(TodoTable.ID);
                final int id = cursor.getInt(idIndex);
                final Uri uriTodDelete = ContentUris.withAppendedId(TodoTable.TABLE_URI,id);
                Animation slideOut = AnimationUtils.loadAnimation(context,android.R.anim.slide_out_right);
                slideOut.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        context.getContentResolver().delete(uriTodDelete,null,null);


                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                });
                view.startAnimation(slideOut);

The problem is, whenever I delete the top item of the listview, the item just below it gets deleted. But it works fine if I delete from the bottom.

How do I fix this? Any help will be appreciated.

P.S the delete method of the Content Resolver class is handling notifyDataSetChanged() method.




Aucun commentaire:

Enregistrer un commentaire