0

I have a fragment and in onCreateView I have an adapter and a listview:

mAdapter = new ArrayAdapter<String>(getActivity(),
            R.layout.list_item,
            R.id.list_item_textview,
            new ArrayList<String>());

    View rootView = inflater.inflate(R.layout.fragment_main, container, false);

    ListView listView = (ListView) rootView.findViewById(R.id.listeview);
    listView.setAdapter(mAdapter)

I also have a separate AsyncTask class and on its onPostExecute method I want to add items to the mAdapter:

@Override
protected void onPostExecute(String[] result) {
    if(result !=null){
        mAdapter.clear();
        for(String mString: result){
            mAdapter.add(mString);                
        }

I execute the AsyncTask in Fragment's onStart method :

@Override
public void onStart() {
    super.onStart();
    FetchData fetchData = new FetchData();
    fetchData.execute(location);
}

How can I use the Fragment's mAdapter on AsyncTask class to update its data and then notify the listView?

dancer_69
  • 331
  • 2
  • 6
  • 20
  • Is it you AsyncTask class, an inner class of your Fragment class? If yes, you can just use mAdapter. If not, then probably you will need to implement an interface with a callback. Use mAdapter.notifyDataSetChanged() – hmartinezd Feb 18 '15 at 16:33
  • no it's on a separate class – dancer_69 Feb 18 '15 at 16:36
  • @dancer_69,did you see the SO question that i linked to?Also considering this task is specific for a singular API call and that you are quite new to android,you could use the AsyncTask as a nested class. – Droidekas Feb 18 '15 at 17:34

3 Answers3

1

based on the API:

ArrayAdapter.addAll() will add all the data that you have obtained in the async task ArrayAdapter.notifyDataSetChanged() will infor the array adapter that the data has changed You could also set ArrayAdapter.setNotifyOnChange(boolean notifyOnChange) to true so that the adapter is notified of the change.

and incase you want to modify your dataset more explicitly,you could look into

Maybe if you want to empty your dataset before adding the new data there also is: -clear()

Do not forget to call notifyDatasetChanged() after every change you make to your data. to conclude this code should achieve whatever you are trying.

arrayAdapter.addAll(data);
arrayAdapter.notifyDatasetChanged();

PS:I picked all this from the documentation

EDIT:since you mentioned modify,this is for completeness sake:

You can also modify individual items using ArrayAdapter.getItem (int position) ,modify the data and call notifyDataSetChanged(),

or else if you want to replace an item,ArrayAdapter.remove(T) to remove the object and reinsert using ArrayAdapter.insert (T object, int index)

There you go,that completes CRUD!

EDIT2:Since you mentioned separate class,you could use an async listener like mentioned here

Community
  • 1
  • 1
Droidekas
  • 3,464
  • 2
  • 26
  • 40
1

If your AsyncTask class isn't defined as an inner class, then I suggest you to create an interface with a callback. Let's say for example.

public interface Updatable { 
    public void onDataUpdate(List<String> data);
}

Later, you have to implement that interface in your fragment, and create a setUpdatableListener method in your AsyncTask class. So in a way that, you first create your AsyncTask instance, then you call the setUpdatableListener passing the listener, then you call execute.Later on On the postExecute method you have to do this:

mListener.onDataUpdate(result);

Of course, later your listener in the fragment will be on charged of updating the mAdapter data, and the ListView.

Please check this question:

How to Define Callbacks in Android?

for a quick simple explanation on how to use interface and callbacks.

Community
  • 1
  • 1
hmartinezd
  • 1,188
  • 8
  • 11
  • 1
    I haven't use interfaces at all, and I don't understand how to connect it with my code. Can you please give a more detailed example with the code I provided? Thanks – dancer_69 Feb 18 '15 at 17:08
0

Make your ListView listView a member of the class and use listView.setAdapter(mAdapter) on your onPostExecute method.

Your ListView automatically refresh data when you bind a new adapter.

Nino DELCEY
  • 652
  • 1
  • 9
  • 25