【发布时间】:2014-09-04 05:25:00
【问题描述】:
我正在获取 json 数据并将其放入 hashmap 和 hashmap 到一个数组列表中。一切都发生在扩展 ListFragment 的片段中
protected String doInBackground(String... urls) {
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("full_name", etUserSearch.getText().toString());
responseReceive = JsonPostClient.SendHttpPost(urls[0],
jsonObject);
} catch (JSONException e) {
e.printStackTrace();
}
try {
Success = responseReceive.getJSONArray("Success");
for (int i = 0; i < Success.length(); i++) {
JSONObject c = Success.getJSONObject(i);
String full_name = c.getString(TAG_FULL_NAME);
String user_name = c.getString(TAG_USER_NAME);
String user_id = c.getString(TAG_USER_ID);
HashMap<String, String> friends = new HashMap<String, String>();
// adding each child node to HashMap key => value
friends.put(TAG_FULL_NAME, full_name);
friends.put(TAG_USER_NAME, user_name);
friends.put(TAG_USER_ID, user_id);
// adding contact to contact list
FriendSearchList.add(friends);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
Log.d("hello", "");
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String result) {
super.onPostExecute(result);
if (pDialog.isShowing())
pDialog.dismiss();
printFriends();
};
我正在通过 printFriends() 方法检查数据是否有效,并且想在列表视图中仅显示“TAG_FULL_NAME”数据。我的 ListView 初始化
View rootView = inflater.inflate(R.layout.tab_add_friends, container,
false);
lv = (ListView) rootView.findViewById(R.id.listView);
ListView中检查数据和加载的方法
public void printFriends() {
int len = FriendSearchList.size();
for (int i = 0; i < len; i++) {
String f_name = FriendSearchList.get(i).get(TAG_FULL_NAME);
String u_name = FriendSearchList.get(i).get(TAG_USER_NAME);
String u_id = FriendSearchList.get(i).get(TAG_USER_ID);
Log.d("full_name", f_name);
Log.d("user_name", u_name);
Log.d("user_id", u_id);
}
ListAdapter adapter = new SimpleAdapter(getActivity(),
FriendSearchList, R.layout.tab_addfriend_list,
new String[] { TAG_FULL_NAME }, new int[] { R.id.full_name });
setListAdapter(adapter);
}
tab_add_friends xml 布局
<?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" >
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
tab_addfriend_list xml 布局
<?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"
android:padding="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp" >
<!-- Name Label -->
<TextView
android:id="@+id/full_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="2dip"
android:paddingTop="6dip"
android:textColor="#43bd00"
android:textSize="16sp"
android:textStyle="bold" />
</LinearLayout>
没有错误显示列表视图中没有显示任何内容。需要建议或线索来解决这个问题。
【问题讨论】:
-
看看这个:stackoverflow.com/questions/25375831/… 并使用 HashMap 而不是用户类。
-
为什么不使用自定义适配器?或创建 MyUser 类并覆盖 toString() 方法,只需输入“TAG_FULL_NAME”并将此类添加到 arraylist
-
@GiGa thnx 的建议。我认为它应该可以工作,但我也在尝试其他方式
-
我从 ListActivity 扩展时遇到了同样的问题,然后它对我有用。
标签: java android listview android-listview arraylist