[英]No items on ListView using SimpleAdapter
我正在嘗試在ListView
顯示ArrayList
值。 值列表存在,我用調試器和Log.d
檢查了它
這是我的課:
public class SIPSettingsFragment extends ListFragment implements View.OnClickListener, AsyncResponse {
public SIPSettingsFragment() {
}
TextView username, password;
Button adduser,showUserForm, showUserListForm;
private ListView listView;
private ListAdapter adapter;
private static final String TAG_USERNAME = "usrname";
public ArrayList<HashMap<String, String>> usersList = new ArrayList<HashMap<String, String>>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_sipsettings, container, false);
/*
code
*/
Log.d("lab","Size: " + usersList.size()); //3
adapter = new SimpleAdapter(this.getActivity(),usersList, R.layout.sipuser_list_item,
new String[] { TAG_USERNAME }, new int[] { R.id.name});
listView = (ListView) rootView.findViewById(android.R.id.list);
listView.setAdapter(adapter);
return rootView;
}
}
我的fragment_sipsettings.xml
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/userListLayout"
android:layout_gravity="center_horizontal">
<include layout="@layout/sipuser_list_item"/>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@android:id/list" />
</LinearLayout>
和sipuser_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/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>
問題出在哪里呢?
編輯:我把我的AsyncTaks
類放在我的SIPSettingsFragment
類中
public class DownloadJSON extends AsyncTask<Void, Void, Void> {
protected Void doInBackground(Void... params) {
usersList = new ArrayList<>();
String urlName ="http://192.168.0.196:8080/openapi/localuser/list?{\"syskey\":\"s\"}";
JSONParser jParser = new JSONParser();
JSONArray json = jParser.getJSONFromUrl(urlName);
try {
for (int i = 0; i < json.length(); i++) {
JSONObject c = json.getJSONObject(i);
String username = c.getString(TAG_USERNAME);
// String description = c.getString(TAG_DESCRIPTION);
// String displayname = c.getString(TAG_DISPLAYNAME);
// String addr = c.getString(TAG_ADDR);
// String state = c.getString(TAG_STATE);
HashMap<String, String> map = new HashMap<>();
map.put(TAG_USERNAME,username);
// map.put(TAG_DESCRIPTION, description);
// map.put(TAG_DISPLAYNAME, displayname);
//// map.put(TAG_ADDR, addr);
// map.put(TAG_STATE, state);
usersList.add(map);
Log.d("lab", "Username: " + username);
}
}catch (JSONException e){
Log.d("lab", "JSON EX: " + e.toString());
}
return null;
}
然后在SIPSettingsFragment
執行此SIPSettingsFragment
new DownloadJSON().execute();
但是我仍然沒有數據。
編輯:
我修改:
@Override
protected void onPostExecute(Void args){
listView = (ListView) getActivity().findViewById(android.R.id.list);
adapter = new SimpleAdapter(
getActivity(),
usersList,
R.layout.sipuser_list_item,
new String[] { TAG_USERNAME },
new int[] { R.id.name}
);
listView.setAdapter(adapter);
((SimpleAdapter) adapter).notifyDataSetChanged();
}
但是還是沒有。
您的適配器的數據為空。
adapter = new SimpleAdapter(this.getActivity(), //context
usersList, //data
R.layout.sipuser_list_item, //resource
new String[] { TAG_USERNAME }, //from
new int[] { R.id.name}); //to
userlist
已初始化但為空 。
如果使用AsyncTask
更改數據,則必須使用notifyDatasetChanged()
方法將更新通知給適配器。
public class DownloadJSON extends AsyncTask<Void, Void, Void> {
@Override
protected void onPostExecute(Void result) {
//....
adapter.notifyDataSetChanged();
}
您使用Async
類獲取數據。 我假設您在下載JSON
數據時需要更新列表。 因此,您需要在onPostExexute
更新適配器
public class DownloadJSON extends AsyncTask<Void, Void, Void> {
protected Void doInBackground(Void... params) {
// Your Existing Code
}
protected void onPostExecute(Void result) {
// Refresh your list here and make sure you have data in your array
adapter.notifyDataSetChanged();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.