Using an AsyncTask to populate a ListView in a Fragment From a SQLite table This is the first time that I have played around with AsyncTask in Android and I wanted to make sure I'm using it correctly. The idea is I'm grabbing all the rows from a table in the database using dao and putting them into an Arraylist based off my Item class. Once finished, I added it to a custom Adapter and add it to my ListView. I wasn't sure if the placement to the AsyncTask class inside the ListFragment is correct or if I used getActivity() correctly ( would it have been better to pass it as a variable?). Any other constructive criticism is welcome as well.

package com.domain.myapp;

import java.util.ArrayList;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;

import com.domain.myapp.adapters.ItemsListAdapter;
import com.domain.myapp.database.DAO;
import com.domain.myapp.models.Item;

public class ItemsListFragment extends ListFragment {

    private ItemsListAdapter falAdp;
    private OnFragmentInteractionListener mListener;

    public ItemsListFragment() {
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

        inflater.inflate(R.menu.item_list_view_menu, menu);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getItemLists gfl = new getItemLists();
        gfl.execute();
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mListener = (OnFragmentInteractionListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        mListener.onFragmentInteraction(item.getItemId(), 0);
        return true;
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);

        if (null != mListener) {
            mListener.onFragmentInteraction(R.id.ViewItemDetails, falAdp
                    .getItem(position).getId());
        }
    }

    public interface OnFragmentInteractionListener {
        public void onFragmentInteraction(int mnuId, int Id);
    }

    private class getItemLists extends
            AsyncTask&lt;Void, String, ArrayList&lt;Item&gt;&gt; {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected void onProgressUpdate(String... values) {
            super.onProgressUpdate(values);
        }

        @Override
        protected ArrayList&lt;Item&gt; doInBackground(Void... params) {
            DAO dao = new DAO();
            ArrayList&lt;Item&gt; fal = dao.ItemsGetList(getActivity());
            return fal;
        }

        @Override
        protected void onPostExecute(ArrayList&lt;Item&gt; result) {
            super.onPostExecute(result);

            falAdp = new ItemsListAdapter(getActivity(),
                    R.layout.fragment_list_item_text_view, result);
            setListAdapter(falAdp);
        }
    }
}
All in all, this code is great. I don't see much in the way of problems. There are a few nit-picks:


    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString()
                + " must implement OnFragmentInteractionListener");
    }



The above code takes one ClassCastException and swaps it with another. Is it necessary? If it is necessary (because the current message does not give details on the current activity), then I recommend initializing the cause of the thrown CCE:

    } catch (ClassCastException e) {
        ClassCastException tothrow = new ClassCastException(activity.toString()
                + " must implement OnFragmentInteractionListener");
        tothrow.initCause(e);
        throw tothrow;
    }


it is a pain, but you benefit from getting all the information in the stack trace. Currently you are losing some.

The only other issue I see is the extension of the AsyncTask. You have re-implemented the methods:


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

    @Override
    protected void onProgressUpdate(String... values) {
        super.onProgressUpdate(values);
    }



This is completely unnecessary and you can delete that code. Just let the ancestor code be.