【发布时间】:2016-05-24 19:59:16
【问题描述】:
我已经坚持这个练习太久了。
我已经获得了部分代码,而我必须编写其余部分。
首先,我有一个 VolleyManager 类,它可以帮助使用一些 Volley 工具(例如添加到请求队列)。然后是另一个帮助解析请求的 Json 的 Gson 类。
这是 UserListFragment 的代码,其中有两个按钮。一个用于填充列表,一个用于清除列表,以及这两个按钮下方的列表本身。
public class UserListFragment extends Fragment {
private static final String USERS_URL = "https://raw.githubusercontent.com/bengui/volleytest/master/json/users.json";
private static final String TAG = UserListFragment.class.getSimpleName();
private View view;
private ListAdapter listAdapter;
private Button requestButton;
private Button cleanButton;
private ListView listView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_user_list, null);
// UI Elements
requestButton = (Button) view.findViewById(R.id.request_btn);
listView = (ListView) view.findViewById(R.id.listview_users);
requestButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listAdapter = new ListAdapter(view.getContext());
listView.setAdapter(listAdapter);
}
});
cleanButton = (Button) view.findViewById(R.id.clean_btn);
cleanButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listView.setAdapter(null);
}
});
return view;
}
这是我为填充列表中的元素而制作的个性化适配器。
public class ListAdapter extends ArrayAdapter {
VolleyManager volleyManager;
List<User> list;
public ListAdapter(Context context){
super(context,0);
volleyManager = VolleyManager.getInstance(getActivity());
requestButton.setEnabled(false);
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(
USERS_URL,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
list = parseUserList(response.toString());
requestButton.setEnabled(true);
notifyDataSetChanged();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error){
Log.d(TAG, "JSON response error : " + error.getMessage());
requestButton.setEnabled(true);
}
}
);
volleyManager.addToRequestQueue(jsonArrayRequest);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View listItemView = convertView;
if (null == convertView) {
listItemView = layoutInflater.inflate(
R.layout.fragment_list_item,
parent,
false);
}
// Pick an element of the list and fill the UI textViews.
User user = list.get(position);
TextView nombre = (TextView) listItemView.findViewById(R.id.user_name);
TextView apellido = (TextView) listItemView.findViewById(R.id.user_last_name);
TextView edad = (TextView) listItemView.findViewById(R.id.user_age);
// Update views
nombre.setText(user.getName());
apellido.setText(user.getLastName());
edad.setText(user.getAge());
return listItemView;
}
}
/**
* Parses a users json array into a users list.
*
* @param jsonArray
*
* @return Users list
*/
private List<User> parseUserList(String jsonArray) {
Gson gson = new Gson();
// Declares the list type
Type listType = new TypeToken<List<User>>() {}.getType();
List<User> userList = gson.fromJson(jsonArray, listType);
return userList;
}
}
我不确定为什么这不起作用。也许我不完全了解适配器的工作原理,并且我在按钮单击侦听器上进行了错误调用?
一个多星期以来,我一直在努力解决这个问题。 取得了一些进展!但仍然无法成功。
欢迎任何意见。
编辑:忘记 .xmls
这是片段的布局。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_user_list"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/request_btn"
android:text="@string/request_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/clean_btn"
android:text="@string/clean_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<ListView
android:id="@+id/listview_users"
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:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/user_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/user_last_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/user_age"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
【问题讨论】:
-
到底是什么问题?不显示列表吗?
-
是的,显示屏上什么也不显示。但数据已被检索。
-
你检查 getView 在你的列表变量被填充后被调用了吗?
-
另外,在您的列表视图项目布局中,您的 LinearLayout 方向是否应该是水平的,因为子视图具有 width="match_parent"?
-
我认为 getView 没有被调用,但我不确定应该在哪里调用..!会继续研究。关于LinearLayout,你是对的,它们的宽度是错误的。也会解决这个问题,所以一切都会出现。谢谢大佬!
标签: java android json gson android-volley