ListView不显示任何内容且没有错误 [英] ListView not displaying anything with no errors

查看:86
本文介绍了ListView不显示任何内容且没有错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Volley从网页上获取JSON数据,然后处理所述数据并将其添加到HashMap中.之后,我创建一个ListAdapter并将其提供给HashMap. 所有这些工作正常,但我没有收到任何错误,但由于某种原因,实际上没有任何显示.

I'm using Volley to grab JSON data from a web page to then process said data and add it to a HashMap. After, I create a ListAdapter and feed it the HashMap. All of this works, I get no errors but for some reason nothing actually shows up.

ListView嵌套在LinearLayout片段中,并且适配器使用自定义布局.片段呈现得很好.

The ListView is nested in a LinearLayout fragment and the adapter uses a custom layout. The fragment renders fine.

MainActivity中的onCreateView

onCreateView in the MainActivity

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. 仅在扩大布局后,使oncreateView返回视图对象

  1. make the oncreateView returns view object only after inflating layout

在onViewCreate方法而不是onCreateView中使用截击方式放置http请求 方法

put the http reguest using volley in onViewCreate Method not onCreateView method

这篇关于ListView不显示任何内容且没有错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆