简体   繁体   English

使用SimpleAdapter的ListView上没有项目

[英]No items on ListView using SimpleAdapter

I'm trying to display values of my ArrayList in my ListView . 我正在尝试在ListView显示ArrayList值。 Values lists exist, I checked it with a debugger and Log.d 值列表存在,我用调试器和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 我的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 和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 编辑:我把我的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;
        }

And I execute this in SIPSettingsFragment new DownloadJSON().execute(); 然后在SIPSettingsFragment执行此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.. 但是还是没有。

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 . userlist已初始化但为空

If you are using an AsyncTask to change the data, you have to use the notifyDatasetChanged() method to notify the adapter about the update. 如果使用AsyncTask更改数据,则必须使用notifyDatasetChanged()方法将更新通知给适配器。

public class DownloadJSON extends AsyncTask<Void, Void, Void> {  

   @Override
   protected void onPostExecute(Void result) {
     //....
     adapter.notifyDataSetChanged();
 }

You fetching data using Async class. 您使用Async类获取数据。 I assume you need to update the list when its downloaded the JSON data. 我假设您在下载JSON数据时需要更新列表。 So you need to update the adapter in onPostExexute 因此,您需要在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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM