I have a FragmentTabHost with 5 tabs and a snippet in each of them. One of the fragments contains a ListView. If I popped the action onto the stack and come back, the ListView will remain the same. If I change the tabs and return to my ListView element, the list is empty. My list still exists, and the adapter and data source are exactly the same, but the table just doesn't draw.
This is my XML.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".Timeline" > <ImageView android:id="@+id/avatarProfile" android:layout_width="wrap_content" android:layout_height="wrap_content" android:scaleType="center" android:src="@drawable/background_pattern" /> <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:clickable="true" android:divider="@null" android:dividerHeight="0dp" > </ListView> </FrameLayout>
This is my fragment
public class Timeline extends Fragment { ListView list; Context context; int page = 1; JSONArray array; TimelineAdapter adapter; boolean shouldReload = true; public Timeline() { } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); context = this.getActivity(); page = 1; array = null; getData(page); } public void onResume(){ //((Activity) context).getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.yummmie_title_bar); super.onResume(); if(array != null){ adapter.notifyDataSetChanged(); } //context = this.getActivity(); //page = 1; //array = null; //getData(page); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_timeline, container, false); } public interface OnFragmentInteractionListener { // TODO: Update argument type and name public void onFragmentInteraction(Uri uri); } public void getData(int page){ RequestParams params = new RequestParams(); SharedPreferences pref = context.getSharedPreferences("YUMMMIE",Context.MODE_PRIVATE); params.put("idUser", pref.getString("ID_USER", null)); params.put("token", pref.getString("TOKEN", null)); params.put("pagina", Integer.toString(page)); params.put("version", "1"); MessageObject.post(params, table, "getstream"); } //Response handlers para login AsyncHttpResponseHandler table = new AsyncHttpResponseHandler(){ @Override public void onSuccess(String response){ try { JSONArray temp = new JSONArray(response); if(array == null){ array = temp; list = (ListView)((Activity) context).findViewById(R.id.listView1); adapter = new TimelineAdapter(context,array); list.setAdapter(adapter); list.setOnScrollListener(scrollListener); }else{ shouldReload = true; if(temp.length() == 0){ shouldReload = false; } array = concatArray(array,temp); adapter.jsonArray = array; adapter.notifyDataSetChanged(); } } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public void onFailure(Throwable e, String response) { // Response failed :( try { JSONObject jsonResponse = new JSONObject(response); JSONArray array = jsonResponse.getJSONArray("errors"); JSONObject error = array.getJSONObject(0); UIHelper.sendAlert(context, error.getString("message"), error.getString("name")); } catch (JSONException e1) {} } @Override public void onFinish() { } }; OnScrollListener scrollListener = new OnScrollListener(){ @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { if (visibleItemCount == totalItemCount) {}else { if ((firstVisibleItem + visibleItemCount) == totalItemCount && (shouldReload == true)) { shouldReload = false; page++; getData(page); } } } @Override public void onScrollStateChanged(AbsListView view, int scrollState) { } }; private JSONArray concatArray(JSONArray... arrs) throws JSONException { JSONArray result = new JSONArray(); for (JSONArray arr : arrs) { for (int i = 0; i < arr.length(); i++) { result.put(arr.get(i)); } } return result; } }
source share