返回片段后ListView为空

时间:2014-01-17 02:02:13

标签: android listview android-listview android-fragments

我有一个FragmentTabHost,每个标签有5个标签和一个片段。其中一个片段包含ListView。如果我将一个Activity推送到堆栈并返回,则ListView保持不变。如果我更改标签并返回到我的ListView片段,则列表为空。我的列表仍然存在,并且适配器和数据源完全相同,但表格没有绘制。

这是我的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>

这是我的片段

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

}

2 个答案:

答案 0 :(得分:3)

我认为这可能是因为您使用onCreateFragment的{​​{1}}方法中填充了您的列表。请尝试在getDataonViewCreated填充您的列表。

答案 1 :(得分:0)

首先为列表视图定义适配器。其次控制您的响应类型。我看到字符串,但你可以处理jsonarray。最后设置你的适配器mylist.setadapter(adapter);