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.
我正在使用Volley从网页中获取JSON数据,然后处理所述数据并将其添加到HashMap。之后,我创建了一个ListAdapter并将其提供给HashMap。所有这一切都有效,我没有错误但由于某种原因没有实际出现。
The ListView is nested in a LinearLayout fragment and the adapter uses a custom layout. The fragment renders fine.
ListView嵌套在LinearLayout片段中,适配器使用自定义布局。片段渲染得很好。
onCreateView in the MainActivity
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);
}
The fragment
<?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>
The list item layout
列表项布局
<?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 个解决方案
#1
0
try the following
尝试以下方法
-
make the oncreateView returns view object only after inflating layout
使oncreateView仅在膨胀布局后返回视图对象
-
put the http reguest using volley in onViewCreate Method not onCreateView method
在onViewCreate方法而不是onCreateView方法中使用volley使用http reguest
- call adapter.notifyDataSetChanged(); if you still have problems
call adapter.notifyDataSetChanged();如果你还有问题
#1
0
try the following
尝试以下方法
-
make the oncreateView returns view object only after inflating layout
使oncreateView仅在膨胀布局后返回视图对象
-
put the http reguest using volley in onViewCreate Method not onCreateView method
在onViewCreate方法而不是onCreateView方法中使用volley使用http reguest
- call adapter.notifyDataSetChanged(); if you still have problems
call adapter.notifyDataSetChanged();如果你还有问题