Qutayba
Qutayba

Reputation: 196

What is the best way to refresh a android's listview?

I want to refresh the listView after an insert or delete it in the database. I've searched and I found notifyDataSetChanged(), but I don't know how to use it.

I used AsyncTask to get the data from the server and insert it into the SQLite database. The problem is that the AsyncTask class is in a another file. So how can I access the adapter or the list view from the AsyncTask class?

Can someone explain how to do that? Even by means of a different way.

Here is my code :

public class devicesController extends SherlockFragment {

    devicesDataSource deviceDS;
    static devicesAdapter adapter;
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){ 
        View view = inflater.inflate(R.layout.devices, container, false);

        /// Set device's datasource
        deviceDS = new devicesDataSource(getActivity());

        /// Assign the listview from the xml layout
        ListView lv1 = (ListView) view.findViewById(R.id.devicesList);

        /// Getting all devices from the SQLite database
        deviceDS.open();
        List<Devices> allDevices = null;
        try {
            allDevices = deviceDS.getAll();
        } catch (ParseException e) {
            e.printStackTrace();
            Log.i("QUTAYBA", "ERROR showing data");
        }


        /// Set the results rows in a list adapter
        adapter = new devicesAdapter(getActivity(), R.layout.devices_list, allDevices);
        lv1.setAdapter(adapter);

    return view;

    }

The AsyncTask class is:

public class bootstrapTask extends AsyncTask<Void, Void, Void> {
    private static Context thisContext = null;
    static devicesDataSource deviceDS;
    private static devicesAdapter da;


    public bootstrapTask(Context context, devicesAdapter deviceAdapt) {
        thisContext = context;
        deviceDS = new devicesDataSource(thisContext);
        da = deviceAdapt;
    }

    @Override
    protected Void doInBackground(Void... params) {

        String data = dataHelper.checkDataFromServer(thisContext);
        try {
            JSONObject jsonData = new JSONObject(data);
            deviceDS.createFromJsonArray(jsonData.getJSONArray("devices"));

        } catch (JSONException e) {
            Log.i("QUTAYBA", e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        //da.notifyDataSetChanged();
        super.onPostExecute(result);
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

    }

    @Override
    protected void onProgressUpdate(Void... values) {
        // TODO Auto-generated method stub
        super.onProgressUpdate(values);
    }

}

Upvotes: 1

Views: 539

Answers (3)

Ankit
Ankit

Reputation: 1916

There is one more good way to achieve what you want without passing adapter object in async task.

From your async task in your onPostExecute() method you can send a broadcast intent and in you activity you can listen for that and call adapater.notifyDataSetChanged(); in onRecieve().

Upvotes: 1

stinepike
stinepike

Reputation: 54672

I think the best thing to use in your case is a CursorLoader. Among all other tutorial you can find out there, you can check this tutorial.

Upvotes: 0

chinabrant
chinabrant

Reputation: 255

Make the adapter as a parameter, put into the asyncTask class, and refresh the ListView in postExcute() method(the method is called by ui thread).

Upvotes: 1

Related Questions