输出:
我想在我的片段列表视图中显示我的Json数组。我创建了一个班级GetUser
:
package com.gmakerorganisation.glocator.Fragments;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import com.android.volley.RequestQueue;
import com.gmakerorganisation.glocator.Config;
import com.gmakerorganisation.glocator.GetAllUsers;
import com.gmakerorganisation.glocator.R;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
public class Sendrequest extends Fragment {
private RequestQueue requestQueue;
ArrayList<HashMap<String, String>> UserList;
private static final String TAG_ID = "id";
private static final String TAG_USERNAME = "username";
private static final String TAG_NAME = "name";
private static final String TAG_PHONE = "phone";
private static final String TAG_PROFILE = "profile";
GetAllUsers webreq = new GetAllUsers();
ListView listView;
public Sendrequest() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v=inflater.inflate(R.layout.fragment_sendrequest, container, false);
listView=(ListView)v.findViewById(R.id.list);
new GetStudents().execute();
return v;
}
private class GetStudents extends AsyncTask<Void, Void, Void> {
// Hashmap for ListView
ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... arg0) {
// Creating service handler class instance
GetAllUsers webreq = new GetAllUsers();
// Making a request to url and getting response
String jsonStr = webreq.makeWebServiceCall(Config.USERS_URL, GetAllUsers.GET);
Log.d("Response: ", "> " + jsonStr);
UserList = ParseJSON(jsonStr);
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
/**
* Updating parsed JSON data into ListView
li * */
ListAdapter adapter = new SimpleAdapter(
getActivity().getApplicationContext(), UserList,
R.layout.list_item, new String[]{TAG_NAME, TAG_PHONE}, new int[]{R.id.name,
R.id.mobile});
listView.setAdapter(adapter);
}
}
private ArrayList<HashMap<String, String>> ParseJSON(String json) {
if (json != null) {
try {
// Hashmap for ListView
ArrayList<HashMap<String, String>> studentList = new ArrayList<HashMap<String, String>>();
// Getting JSON Array node
JSONArray userrs = new JSONArray(json);
// looping through All Students
for (int i = 0 ; i < userrs.length(); i++) {
JSONObject c = userrs.getJSONObject(i);
String id = c.getString(TAG_ID);
String username = c.getString(TAG_USERNAME);
String name = c.getString(TAG_NAME);
String phone = c.getString(TAG_PHONE);
String profile = c.getString(TAG_PROFILE);
// tmp hashmap for single student
HashMap<String, String> user = new HashMap<String, String>();
// adding each child node to HashMap key => value
user.put(TAG_ID, id);
user.put(TAG_USERNAME, username);
user.put(TAG_NAME, name);
user.put(TAG_PHONE, phone);
user.put(TAG_PROFILE, profile);
// adding student to students list
studentList.add(user);
}
return studentList;
} catch (JSONException e) {
e.printStackTrace();
return null;
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
return null;
}
}
}
我在UserList中获取了正确的数据,所以我认为listview中存在问题。我的R.layout.list_item
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- Name Label -->
<TextView
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="2dip"
android:paddingTop="6dip"
android:text="Name"
android:textSize="16sp"
android:textStyle="bold" />
<!-- Mobile number label -->
<TextView
android:id="@+id/mobile"
android:layout_width="220dp"
android:layout_height="wrap_content"
android:gravity="left"
android:text="Phone: " />
</LinearLayout>
我的json是这样的:
[{"id":"1","username":"gmaker","name":"SHUBHAM SHARMA","phone":"9711979977","profile":"http:\/\/glocator.esy.es\/profilepicture\/1."},{"id":"4","username":"somnath","name":"","phone":"9582223881","profile":""},{"id":"7","username":"shitij","name":"","phone":"9650154839","profile":""}]
答案 0 :(得分:0)
更新列表时使用mAdapter.notifyDataSetChanged()
来更新适配器:
UserList = ParseJSON(jsonStr);
mAdapter.notifyDataSetChanged();
随着你的适配器初始化:
mAdapter = new SimpleAdapter(
getActivity().getApplicationContext(), UserList,
R.layout.list_item, new String[]{TAG_NAME, TAG_PHONE}, new int[]{R.id.name,
R.id.mobile});
listView.setAdapter(adapter);