Skip to content Skip to sidebar Skip to footer

How To Listview Refresh After Delete An Item On Button Click Event In Android?

I want to delete an item from Listview, and at a time Refresh Listview after deleting an item. How to possible? I am using get all item using JSON Parsing from database and delete

Solution 1:

in your custom adapter call this.notifyDataSetChanged(); where you are performing delete functionality and deleting that element from arrayList which is set to that adapter

Solution 2:

You are writing delete action, in that function only call adapter.notifyDataSetChanged again.So it refr on delete action fetch the data once again and then again call adapter.notifyDataSetChanged it will work

Solution 3:

First of all Put you adapter code in Details.java class

then change this,

publicclassDeleteCommentsextendsAsyncTask<Void, Void, Void> {
        protectedvoidonPreExecute() {
            super.onPreExecute();
        }

        @OverrideprotectedVoiddoInBackground(Void... arg0) {
            ServiceHandler sh = newServiceHandler();
            String jsonStr = sh.makeServiceCall(urlDelete,
                    ServiceHandler.GET);
            Log.d("Response : delete join comments", ">" + jsonStr);

            returnnull;
        }

        protectedvoidonPostExecute(Void result) {
            super.onPostExecute(result);
            Adapter1.remove(Adapter1.getItem(position));
        };
    }

hope it will help you

Solution 4:

     @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder = null;
            if (convertView == null) {
                convertView = layoutInflater.inflate(
                        R.layout.list_item, null);

                viewHolder = new ViewHolder();
                getData = arrData.get(position);

                /** Initialize Widgets */

                viewHolder.imgCancel = (ImageView) convertView
                        .findViewById(R.id.imgCancel);

                viewHolder.imgCancel
                        .setOnClickListener(new OnClickListener() {
                            @Override
                            publicvoidonClick(View v) {
                                strMemberId = arrData.get(
                                        position).get(
                                        Detail.TAG_MEMBER_ID);
                                urlDelete = "http://example.com/delete.php?mem_id="
                                        + strMemberId;
                                new DeleteComments().execute();
                            }
                        });

                /** TextView */
                viewHolder.txtMemberId = (TextView) convertView
                        .findViewById(R.id.txtMemberId);
                viewHolder.txtId = (TextView) convertView
                        .findViewById(R.id.txtId);

                viewHolder.txtDesc = (TextView) convertView
                        .findViewById(R.id.txtDesc);

                /** Set Value */


                viewHolder.txtMemberId.setText(getDetailData 
                        .get(Detail.TAG_MEMBER_ID));
                viewHolder.txtId.setText(getDetailData
                        .get(Detail.TAG_ID));

                viewHolder.txtDesc.setText(getDetailData
                        .get(Detail.TAG_STATUS));

                convertView.setTag(viewHolder);

            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
bDelete.setOnClickListener(new OnClickListener(){
@Override
            publicvoidonClick(View view)
            {
                arrData.remove(position);
                notifyDataSetChanged():
            }

});


            return convertView;
        }

Solution 5:

Why make it that complicated?

Just call remove(Obj); in OnClickListener of your customized adapter Adapter1's. And notifyDataSetChanged will be called in removed method too.

Post a Comment for "How To Listview Refresh After Delete An Item On Button Click Event In Android?"