Reputation: 1260
I have five list views that are fragments in view pager tabs. When you click on the items in the list view it launches a new activity. But when I press the back-button to get back to the tabbed list view, the list view has doubled and if I open the activity and go back again it doubles again and it will keep doing that.I think the problem lies with this line of code
private List<Movie> movieList = new ArrayList<Movie>();
initiating on every return to the fragment however I am unsure on how to go about resolving it.
...
public class TabOneFragment extends ListFragment {
private static final String TAG = AccommodationFragment.class.getSimpleName();
// Movies json url
private static final String url = "http://api.mywebsite.com/json/json.php";
private ProgressDialog pDialog;
private List<Movie> movieList = new ArrayList<Movie>();<----i think this is the cause???
private ListView listView;
private CustomListAdapter adapter;
private View mheaderView;
//private TextView txtFragmentone;
public static TabOneFragment newInstance() {
Bundle bundle = new Bundle();
TabOneFragment fragment = new TabOneFragment();
fragment.setArguments(bundle);
return fragment;
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.accommodation_fragment, container, false);
rootView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT ));
listView = (ListView) getActivity().findViewById(android.R.id.list);
adapter = new CustomListAdapter(getActivity(), movieList);
setListAdapter(adapter);
mheaderView = inflater.inflate(R.layout.list_header, null);
pDialog = new ProgressDialog(getActivity());
// Showing progress dialog before making http request
pDialog.setMessage("Loading...");
pDialog.show();
// Creating volley request obj
JsonArrayRequest movieReq = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
hidePDialog();
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
Movie movie = new Movie();
movie.setTitle(obj.getString("title"));
movie.setThumbnailUrl(obj.getString("image"));
movie.seturl(obj.getString("url"));
/*movie.setRating(((Number) obj.get("rating"))
.doubleValue());*/
movie.setYear(obj.getString("releaseYear"));
// Genre is json array
JSONArray genreArry = obj.getJSONArray("genre");
ArrayList<String> genre = new ArrayList<String>();
for (int j = 0; j < genreArry.length(); j++) {
genre.add((String) genreArry.get(j));
}
movie.setGenre(genre);
// adding movie to movies array
movieList.add(movie);
} catch (JSONException e) {
e.printStackTrace();
}
}
// notifying list adapter about data changes
// so that it renders the list view with updated data
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hidePDialog();
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(movieReq);
return rootView;
}
@Override
public void onDestroy() {
super.onDestroy();
hidePDialog();
setListAdapter(null);
}
private void hidePDialog() {
if (pDialog != null) {
pDialog.dismiss();
pDialog = null;
}
}
...
Any help would be appreciated . Thanks.
Upvotes: 0
Views: 276
Reputation: 5451
The reason for doubling the data is your arraylist is not intialising everytime. you need to initialize it at oncreateview() method instead of globally initialised.
Upvotes: 0
Reputation: 5420
Can you check every time you click the back button from the activity, will the onCreate
method of this Fragment calls.
If that is the case, before you run the for loop inside the onResponse
method
movieList.clear();
If this is not resolving the issue, the the issue is in the createView method in the adapter.
Upvotes: 0
Reputation: 12685
you have to clear you list before adding new data if you are adding all data again.
and better practice is to add only additional data in list rather than adding all data set again to list
you can clear list before start adding data like this.
JsonArrayRequest movieReq = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
hidePDialog();
movieList.clear();
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
Upvotes: 1