Refresh ListView with BaseAdapter

2019-09-02 01:57发布

I have a ListView with a delete button on each item.I want when the button is pressed the item to be removed from data(An ArrayList of HashMaps) and dismiss from ListView too.

Here is My Code:

BaseAdapter:

public class MyCustomAdapter extends BaseAdapter{

 private ArrayList<HashMap<String, Object>> list;

 private Context context;

 public MyCustomAdapter(ArrayList<HashMap<String, Object>> list,Context  context){

    this.list = list;
    this.context = context;
}


@Override
public int getCount() {
    return list.size();
}

@Override
public Object getItem(int position) {
    return list.get(position);
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    View view = convertView;

    if(view == null){

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        view = inflater.inflate(R.layout.addresslist_item, null);



        TextView address = (TextView) view.findViewById(R.id.listtxtaddress);

        String add = String.valueOf(list.get(position).get("address"));

        address.setText(add);

        Button btndelete = (Button) view.findViewById(R.id.listbtndelete);

        btndelete.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                Log.d("check", "item_positiona:"+position);
                Log.d("check", "lista:"+list.toString());

                AlertDialog.Builder builder = new AlertDialog.Builder(context);

                builder.setTitle("Τιτλος επιχειρησησης");

                builder.setMessage("Θέλετε να διαγάρψετε αυτή τη διεύθυνση;");

                builder.setNegativeButton("NAI", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        dialog.dismiss();

                        Log.d("check", "item_positionb:"+position);


                        list.remove(position);

                        notifyDataSetChanged();


                    }
                });

                builder.setPositiveButton("AKYPO", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        dialog.dismiss();

                    }
                });

                AlertDialog abc = builder.create();

                abc.show();



            }
        });


    }

    return view;


}
}

My problem is that the item is removed from data but the when ListView is update only the last item is removed and not the item that i select.Any ideas why this happening? Thanks in Advance.

3条回答
【Aperson】
2楼-- · 2019-09-02 02:39

After the ArrayList has changed, you must call notifyDataSetChanged() to update your ListView with the changes. This can be done inside or outside the adapter. So, for example:

public class MyCustomBaseAdapter extends BaseAdapter {
    //TIP: Don't make this static, that's just a bad idea
    private ArrayList<Contact> searchArrayList;

    private LayoutInflater mInflater;

    public MyCustomBaseAdapter(Context context, ArrayList<Contact> initialResults) {
        searchArrayList = initialResults;
        mInflater = LayoutInflater.from(context);
    }

    public void updateResults(ArrayList<Contact> results) {
        searchArrayList = results;
        //Triggers the list update
        notifyDataSetChanged();
    }
}
查看更多
爷的心禁止访问
3楼-- · 2019-09-02 02:39

You can try this way also.. Create a Listener interface

package xyz.listeners;

public interface DeleteClickListener {

    public void onDeleteClick(int position);

}

Than in your actvity/fragment that has listview-

private DeleteClickListener mDeleteClickListener;
mDeleteClickListener = new DeleteClickListener() {

            @Override
            public void onDeleteClick(int position) {

     list.remove(position);
adapter.notifyDataSetChanged();
            }
        };

In your adapter class contructor pass that listener.

public MyCustomAdapter(ArrayList<HashMap<String, Object>> list,Context  context,DeleteClickListene mDeleteClickListener){

    this.list = list;
    this.context = context;
    deleteClickListener=mDeleteClickListener;

}

and then onClick of Delete button

builder.setNegativeButton("NAI", new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                              deleteClickListener.onDeleteClick(position);




                            }
                        });
查看更多
放荡不羁爱自由
4楼-- · 2019-09-02 02:57

Calling notifyDataSetChanged() on your Adapter object once you've modified the data should refresh the listview.

You can try this-

Create a private class ViewHolder inside your adapter. This class should contain all the views which your list item has.

private static class ViewHolder {
    TextView address;
    String add;
    Button btndelete;
}

And then inside getView() :

        ViewHolder viewHolder;
       if (convertView == null) {     
                viewHolder = new ViewHolder();
                LayoutInflater layoutInflater = LayoutInflater.from(mContext);
                convertView = layoutInflater.inflate(R.layout.addresslist_item, null);
                viewHolder.address =  (TextView) view.findViewById(R.id.listtxtaddress);
                viewHolder.add = String.valueOf(list.get(position).get("address"));
                viewHolder.address.setText(add);

            viewHolder.btndelete = (Button) view.findViewById(R.id.listbtndelete);
                convertView.setTag(viewHolder);
    }
     else 
   {                


    viewHolder = (ViewHolder) convertView.getTag();

   } 
    viewHolder.btndelete.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {

                    Log.d("check", "item_positiona:"+position);
                    Log.d("check", "lista:"+list.toString());

                    AlertDialog.Builder builder = new AlertDialog.Builder(context);

                    builder.setTitle("Τιτλος επιχειρησησης");

                    builder.setMessage("Θέλετε να διαγάρψετε αυτή τη διεύθυνση;");

                    builder.setNegativeButton("NAI", new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                            dialog.dismiss();

                            Log.d("check", "item_positionb:"+position);


                            list.remove(position);

                            notifyDataSetChanged();


                        }
                    });




    builder.setPositiveButton("AKYPO", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        dialog.dismiss();



                    }
                });

                AlertDialog abc = builder.create();

                abc.show();



            }
        });


    }
    }
    return convertView;
    }
查看更多
登录 后发表回答