patternjavaMinor
Using an AsyncTask to populate a ListView in a Fragment From a SQLite table
Viewed 0 times
sqlitepopulateusingfragmentfromasynctasktablelistview
Problem
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
```
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()
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()
Solution
All in all, this code is great. I don't see much in the way of problems. There are a few nit-picks:
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:
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:
This is completely unnecessary and you can delete that code. Just let the ancestor code be.
} 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.
Code Snippets
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnFragmentInteractionListener");
}} catch (ClassCastException e) {
ClassCastException tothrow = new ClassCastException(activity.toString()
+ " must implement OnFragmentInteractionListener");
tothrow.initCause(e);
throw tothrow;
}@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
}Context
StackExchange Code Review Q#44789, answer score: 8
Revisions (0)
No revisions yet.