ListView没有显示没有错误的任何内容

时间:2017-08-04 11:45:31

标签: java android listview android-fragments

我正在使用Volley从网页中获取JSON数据,然后处理所述数据并将其添加到HashMap。之后,我创建了一个ListAdapter并将其提供给HashMap。 所有这些都有效,我没有错误,但由于某种原因没有实际出现。

ListView嵌套在LinearLayout片段中,适配器使用自定义布局。片段渲染得很好。

MainActivity中的onCreateView

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view =  inflater.inflate(R.layout.menu_betriebe, container, false);

        mitgliederList = new ArrayList<>();
        mitgliederListView = view.findViewById(R.id.menu_betriebe_mitglieder_list);

        String url = getString(R.string.api_base_url) + "removed";

        JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET,
                url, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {

                        JSONArray mitglieder;

                        try {
                            mitglieder = response.getJSONArray("data");

                            for (int i = 0; i < mitglieder.length(); i++) {

                                JSONObject mitgliedObject = mitglieder.getJSONObject(i);

                                String mitglied_name = mitgliedObject.getString("mitglied_name");

                                Log.d("WBV", mitglied_name);

                                HashMap<String, String> mitglied = new HashMap<>();
                                mitglied.put("mitglied_name", mitglied_name);

                                mitgliederList.add(mitglied);
                            }

                            ListAdapter adapter = new SimpleAdapter(
                                    getActivity(),
                                    mitgliederList,
                                    R.layout.betriebe_list_item,
                                    new String[]{"mitglied_name"},
                                    new int[]{R.id.betriebe_list_item_mitglied_name});

                            mitgliederListView.setAdapter(adapter);

                        } catch (JSONException e) {

                        }
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        VolleyLog.d("WBV", "Error: " + error.getMessage());
                    }
                }
        );

        // Adding request to request queue
        AppController.getInstance().addToRequestQueue(jsonObjReq, "json_obj_req");

        return inflater.inflate(R.layout.menu_betriebe, container, false);
    }

片段

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/menu_betriebe"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ListView
        android:id="@+id/menu_betriebe_mitglieder_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

列表项布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/betriebe_list_item_mitglied_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:textColor="@color/colorPrimaryDark"
        android:textSize="16sp"
        android:textStyle="bold"
        android:text="HalloTest"/>

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

尝试以下

  1. 使oncreateView仅在膨胀布局

  2. 后返回视图对象
  3. 在onViewCreate方法而不是onCreateView中使用volley使用http reguest 方法

  4. call adapter.notifyDataSetChanged();如果你还有问题