Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ListView empty after returning to fragment

I got a FragmentTabHost with 5 tabs and a fragment in each one. One of the fragments contains a ListView. If I push an Activity to the stack and return, the ListView remains the same. If i change tabs and come back to my ListView fragment, the list is empty. My List still exists as it was and the adapter and datasource are exactly the same but the table just isn't drawing.

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;
        }

}
like image 990
Jorsh Avatar asked Jan 17 '14 02:01

Jorsh


1 Answers

I think this may be because you're populating your list in the onCreate method of your Fragment with getData. Try populating your list in onViewCreated or onResume instead.

like image 82
SK9 Avatar answered Oct 05 '22 11:10

SK9