[英]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.