如何在Android中为Listview获取HashMap值到CustomlistAdapter中

时间:2017-05-13 17:52:14

标签: java android listview hashmap

我尝试从MainActivity获取数据到我的Customlist Activity以在ListView中显示数据。不幸的是我无法找到我的错误。 这是我的MainActivity:

public class Locations extends Activity{
    private String TAG = Locations.class.getSimpleName();

    private ProgressDialog pDialog;


    // URL to get contacts JSON
    private static String url = "";

    ArrayList<HashMap<String, String>> contactList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        contactList = new ArrayList<>();

    }

    /**
     * Async task class to get json by making HTTP call
     */
    private class GetContacts extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // Showing progress dialog
            pDialog = new ProgressDialog(Locations.this);
            pDialog.setMessage("Please wait...");
            pDialog.setCancelable(false);
            pDialog.show();

        }

        @Override
        protected Void doInBackground(Void... arg0) {
            HttpHandler sh = new HttpHandler();

            // Making a request to url and getting response
            String jsonStr = sh.makeServiceCall(url);

            Log.e(TAG, "Response from url: " + jsonStr);

            if (jsonStr != null) {
                try {
                    JSONArray contacts = new JSONArray(jsonStr);

                    // Getting JSON Array node


                    // looping through All Contacts
                    for (int i = 0; i < contacts.length(); i++) {
                        JSONObject c = contacts.getJSONObject(i);

                        String id = c.getString("id");
                        String name = c.getString("name");
                        String imageurl = c.getString("imageurl");
                        // tmp hash map for single contact
                        HashMap<String, String> contact = new HashMap<>();

                        // adding each child node to HashMap key => value
                        contact.put("id", id);
                        contact.put("name", name);
                        contact.put("imageurl",imageurl);

                        // adding contact to contact list
                        contactList.add(contact);
                    }
                } catch (final JSONException e) {
                    Log.e(TAG, "Json parsing error: " + e.getMessage());
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getApplicationContext(),
                                    "Json parsing error: " + e.getMessage(),
                                    Toast.LENGTH_LONG)
                                    .show();
                        }
                    });

                }
            } else {
                Log.e(TAG, "Couldn't get json from server.");
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getApplicationContext(),
                                "Couldn't get json from server. Check LogCat for possible errors!",
                                Toast.LENGTH_LONG)
                                .show();
                    }
                });

            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            // Dismiss the progress dialog
            if (pDialog.isShowing())
                pDialog.dismiss();
            /**3
             * Updating parsed JSON data into ListView
             * */

            Customlist adapter = new
                    Customlist(Locations.this, contactList);
            list=(ListView)findViewById(R.id.lv);
            list.setAdapter(adapter);


        }

    }
}

这是我的CustomlistActivity:

public class Customlist extends ArrayAdapter<String> {
    private final Activity context;
    private final HashMap<String, String> contactlist;
    public Customlist(Activity context, ArrayList<HashMap<String, String>> contactlist) {
        super(context, R.layout.model, (List<String>) contactlist);
        this.context = context;
        this.contactlist= contactlist;


    }
    @Override
    public View getView(int position, View view, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        View rowView= inflater.inflate(R.layout.model, null, true);
        TextView txtTitle = (TextView) rowView.findViewById(R.id.namelist);

        ImageView imageView = (ImageView) rowView.findViewById(R.id.imagelist);


        return rowView;
    }
 }

1 个答案:

答案 0 :(得分:1)

1。当您传递HashMap时,请Customlist而不是ArrayAdapter<HashMap<String, String>>延长ArrayAdapter<String>

2。更新constructor,如下所示:

    public Customlist(Context context, ArrayList<HashMap<String, String>> contactlist) {
        super(context, R.layout.model, contactlist);
        this.context = context;
        this.contactlist= contactlist;
    }

3。在您的getView()方法中,从HashMap获取List并获取idname和{{1如下所示:

imageurl

以下是更新的// Contact HashMap<String, String> contact = contactlist.get(position); String id = contact.get("id"); String name = contact.get("name"); String imageurl = contact.get("imageurl"); 适配器:

Customlist

希望这会有所帮助〜