【问题标题】:How to get HashMap value into CustomlistAdapter for Listview in android如何将HashMap值获取到Android中Listview的CustomlistAdapter
【发布时间】:2017-05-13 17:52:14
【问题描述】:

我尝试将数据从我的 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;
    }
 }

【问题讨论】:

  • 目前状态如何?

标签: java android listview hashmap


【解决方案1】:

1.当您传递 HashMap 时,因此从 ArrayAdapter&lt;HashMap&lt;String, String&gt;&gt; 扩展 Customlist 而不是 ArrayAdapter&lt;String&gt;

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()方法中,从List得到HashMap,得到idnameimageurl如下:

// Contact
HashMap<String, String> contact = contactlist.get(position);

String id = contact.get("id");
String name = contact.get("name");
String imageurl = contact.get("imageurl");

这是更新后的Customlist 适配器:

public class Customlist extends ArrayAdapter<HashMap<String, String>> {
    private Context context;
    private ArrayList<HashMap<String, String> contactlist;

    public Customlist(Context context, ArrayList<HashMap<String, String>> contactlist) {
        super(context, R.layout.model, 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);


        // Contact
        HashMap<String, String> contact = contactlist.get(position);

        String id = contact.get("id");
        String name = contact.get("name");
        String imageurl = contact.get("imageurl");

        // Do something with value id, name and imageurl

        return rowView;
    }

}

希望对你有帮助~

【讨论】:

  • 非常欢迎 :).. 如果我的回答似乎有用,请投票。
猜你喜欢
  • 2012-02-25
  • 1970-01-01
  • 2011-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-03
相关资源
最近更新 更多