Reputation: 3340
I'm trying to display values of my ArrayList
in my ListView
. Values lists exist, I checked it with a debugger and Log.d
This is my class:
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;
}
}
My 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>
And 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>
And where is problem?
Edit:
I put my AsyncTaks
class in my SIPSettingsFragment
class
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;
}
And I execute this in SIPSettingsFragment
new DownloadJSON().execute();
But still I don't have data.
EDIT:
I modify:
@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();
}
But still nothing..
Upvotes: 1
Views: 53
Reputation: 365178
Your adapter has an empty data.
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
is initialized but empty.
If you are using an AsyncTask
to change the data, you have to use the notifyDatasetChanged()
method to notify the adapter about the update.
public class DownloadJSON extends AsyncTask<Void, Void, Void> {
@Override
protected void onPostExecute(Void result) {
//....
adapter.notifyDataSetChanged();
}
Upvotes: 2
Reputation: 10079
You fetching data using Async
class. I assume you need to update the list when its downloaded the JSON
data. So you need to update the adapter in 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();
}
Upvotes: 0