Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reload activity from onClickListener in ArrayAdapter

I'm working on a fragment that contains a listview (this fragment is PlaceHolderFragment generated when create activity). I extends ArrayAdapter to make my custom adapter and fill my listview with this adapter.

One important thing is in one row of listview, there are 2 buttons: first is the enable/disable button to change status of an user (when user's status is active then it's disable, otherwise enable), second is the delete button (to delete user). So I have to implement OnClickListener for this 2 buttons in method getView() of adapter

When click either buttons, it will send request to server and manipulate database (change user's status or delete user from database). The thing is when I click enable button (for example), it is success and user's status in database is changed, or when I click delete button, user will be delete from database successfully

BUT after I click that button, it's state does not changed (I mean if user is enabled, now the button must change to disable, or if user is deleted, that row must be remove from screen). I have to reload this fragment by hand (switch to other fragment and then come back)

The question is how can I reload activity (I already implement onResume to load all data to adapter, so if I can make this method onResume of fragment run, it will work as my expectation), or at least how can I reload the listview to update new data?

Note: notifyDataSetChanged() DOES NOT work because the data in adapter actually doesn't change yet, only data on server are changed

Note 2: if you need me to post my code, please comment and I will edit my post, because I think my code is long

Thank you and best regards!

EDIT 1 I've posted my solution in the answer below, it fix the problem but I have to say that this is a very very BAD practice in android. For example, when you want to delete an item with this approach, you may want to put a AlertDialog for user to confirm, but AlertDialog can only show in Activity (or Fragment), it can't be show from Adapter. So instead, you should use some different methods such as ContextMenu or CustomDialog.

like image 811
Hoang Trinh Avatar asked Jul 09 '14 17:07

Hoang Trinh


1 Answers

After couple of weeks searching google and try different methods, I finally found a way to archive what I want, and it's very simple. I post my answer here for anyone facing this problem like me in the future

public class CustomAdapter extends ArrayAdapter<YourClass> {
    private List<YourClass> items;
    private CustomAdapter adapter;
    private Context context;
    private Button button;
    private YourClass item;

    public CustomAdapter(Context context, List<YourClass> items) {
        super(context, R.layout.custom_list_item, items);
        this.items = items;
        this.context = context;
        this.adapter = this; //This is an important line, you need this line to keep track the adapter variable
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;

        if (v == null) {
            LayoutInflater li = LayoutInflater.from(getContext());
            v = li.inflate(R.layout.custom_list_item, null);
        }
        item = items.get(position);

        button = (Button) v.findViewById(R.id.button); //Find the button
        button.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View view) {
                        //Do something inside here like update, remove your item...
                        //But the important is
                        items.remove(item); //Actually change your list of items here
                        adapter.notifyDataSetChanged(); //notify for change
                    }
                });
    }
    return v;
}

That's it, all you need to implement when you need the listview to reload in case you need to implement OnClick inside the adapter. Hope you guy find it easier than me when you face this problem

like image 52
Hoang Trinh Avatar answered Oct 27 '22 09:10

Hoang Trinh