Android - Listview delete item and Refresh

后端 未结 9 1705
耶瑟儿~
耶瑟儿~ 2020-12-06 02:16

I am trying to implement ListView with Delete functionality to delete item from the listview. I am successful to delete but failed to refresh the listview after deletetion o

相关标签:
9条回答
  • 2020-12-06 02:51

    I'm guessing that using

    getActivity().recreate();
    

    instead of restarting the activity via a new Intent is better because using a new Intent will only stop the current activity and not destroy it.

    Anyway, it works.

    0 讨论(0)
  • 2020-12-06 03:01

    Call that Activity once again Using Intent

    0 讨论(0)
  • 2020-12-06 03:01

    Try calling refreshDrawableState to tell the list to redraw.

    0 讨论(0)
  • 2020-12-06 03:02

    I did something like this in my adapter:

    ((Activity)cxt).finish();
    Intent intent = new Intent(cxt, YourActivity.class);
    cxt.startActivity(intent);
    

    This ends the activity and then starts the same one again.

    0 讨论(0)
  • 2020-12-06 03:05

    I have the solution:

    If you want to delete a row from a list view clicking on the DELETE button of each of that row do this. Here you have an example of a custom adapter class with a name and a delete button. Every time you press the button the row is deleted

    public class UserCustomAdapter extends ArrayAdapter<User>{ 
    
    Context context;
    int layoutResourceId;
    ArrayList<User> data = new ArrayList<User>();
    
    public UserCustomAdapter(Context context, int layoutResourceId,ArrayList<User> data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }
    
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View row = convertView;
        UserHolder holder = null;
    
    
        if (row == null) {
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);
    
            holder = new UserHolder();
            holder.textName = (TextView) row.findViewById(R.id.textView1);
            holder.btnDelete = (Button) row.findViewById(R.id.button2);
            row.setTag(holder);
        } else {
            holder = (UserHolder) row.getTag();
    
        }
    
        User user = data.get(position);
    
    
        holder.btnDelete.setTag(position);
        holder.textName.setText(user.getName());
    
    
    
        holder.btnDelete.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View v) {
    
                String pos = v.getTag().toString();
                int _posicion = Integer.parseInt(pos);
                data.remove(_posicion);
                notifyDataSetChanged();
    
            }
        });
    
        return row;
    
    
    }
    
    static class UserHolder {
        TextView textName;
        Button btnDelete;
    }
    }
    
    0 讨论(0)
  • 2020-12-06 03:07

    Does it remove it from your list adapter? If not that would be the reason the notifyDataSetChanged() won't do you much good.

    Actually looking at your code again i can only find that you're removing it from your database and not the adapter itself.

    edit (to answer comment): Well that's hard to do without your ListView_CustomAdapter class. The problem is, in this adapter there's a data set (the one you put in the constructor (listitemDisplay)) which needs to be updated as well. Only then the notifyDataSetChanged() will work.

    0 讨论(0)
提交回复
热议问题