ListFragment不显示异步数据

时间:2016-08-29 00:52:14

标签: android android-fragments

我正在使用loopj将JSON数据异步加载到ListFragment中。我正在使用ListFragment附带的默认listview并在loopj的OnSuccess回调函数中调用我的方法FillListView。我已经查看过有关此问题的许多类似帖子,但无法解决我的问题。

我也尝试通过调用

从MainActivity加载ListFragment
MenuCatFragment.newInstance("a","c").setListAdapter(adapter);

但这也不行。

任何人都可以帮忙。

public class MenuCatFragment extends ListFragment {
     

//android studio auto-generated code ommitted    

public MenuCatFragment() {   }
      


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
    }

    myApp = (App)this.getActivity().getApplication();
    currentRestoSummary = myApp.getSelectedRestoSummary();
    restoid = currentRestoSummary.getRestoid();

try {
       LoadSelectedResto();
    } catch (Exception e) {
    }

}

private void LoadSelectedResto() throws JSONException {


    EatNowRestClient.get("resto", new RequestParams("id", restoid), new JsonHttpResponseHandler() {


        @Override
        public void onSuccess(int statusCode, Header[] headers, JSONObject response) {

            Gson gson = new Gson();
            Type listType = new TypeToken<Resto>() {}.getType();

            try {
                resto = gson.fromJson(response.toString(), listType);

                myApp.setSelectedResto(resto);

                FillListView();

                //progressDialog.dismiss();


            } catch (Exception e) {

                throw e;
            }
        }


        @Override
        public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
            super.onFailure(statusCode, headers, throwable, errorResponse);
            //progressDialog.dismiss();
            //Log.d("Failed: ", ""+statusCode);
            //Log.d("Error : ", "" + throwable);
        }

    });

private void FillListView() {

ListAdapter adapter;

adapter  = new customAdapterMenuCat(getActivity(),R.layout.custom_row_menucat,resto.getMenu_Categories());

setListAdapter(adapter);

    }


}

//adapter code

public class customAdapterMenuCat extends ArrayAdapter<MenuCategory> {

MenuCategory[] menucat;
Context c;

public customAdapterMenuCat(Context context, int resource, MenuCategory[] objects) {
    super(context, resource, objects);

    menucat = objects;
    c = context;
}

@Override
public int getCount() {
    return menucat.length;
}

@Override
public MenuCategory getItem(int position) {
    return menucat[position];
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View customview = View.inflate(c, R.layout.custom_row_menucat,null);

    TextView lblMenuCatNameResto = (TextView)customview.findViewById(R.id.lblMenuCat);

    lblMenuCatNameResto.setText(menucat[position].getName());

    return customview;


}

}

1 个答案:

答案 0 :(得分:0)

我找到了解决方案......问题是使用fragmenttabhost布局xml ...下面是工作的。

<android.support.v4.app.FragmentTabHost
     android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/tabResto">

    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"/>

        <FrameLayout
            android:id="@+id/realtabcontent"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>

    </LinearLayout>
</android.support.v4.app.FragmentTabHost>