简体   繁体   English

使用哈希映射 - 使用SimpleAdapter在ListView中显示图像 - Android

[英]Displaying image in a ListView with a SimpleAdapter using Hash Map - Android

I'm using a SimpleAdapter to display a ListView in a Hash map of JSON parsed data. 我正在使用SimpleAdapter在JSON解析数据的哈希映射中显示ListView Everything works perfectly. 一切都很完美。 How do I effectively add an image in this ListView (or add an image to the Map ) ? 如何在此ListView有效添加图像(或将图像添加到Map )? Adding image to the hash map does not display the image nor does it throw an error or exception! 将图像添加到哈希映射不会显示图像,也不会引发错误或异常!

Here is my piece of code.. 这是我的代码..

    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.event_list);
            // Hashmap for ListView
            inboxList = new ArrayList<HashMap<String, String>>();

            // Loading INBOX in Background Thread
            new LoadInbox().execute();
        }

        /**
         * Background Async Task to Load all INBOX messages by making HTTP Request
         * */
        class LoadInbox extends AsyncTask<String, String, String> {

            /**
             * Before starting background thread Show Progress Dialog
             * */
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                pDialog = new ProgressDialog(AllEvents.this);
                pDialog.setMessage("Loading Events ...");
                pDialog.setIndeterminate(false);
                pDialog.setCancelable(false);
                pDialog.show();
            }

            /**
             * getting Inbox JSON
             * */
            protected String doInBackground(String... args) {
                // Building Parameters
                List<NameValuePair> params = new ArrayList<NameValuePair>();

                // getting JSON string from URL
                JSONObject json = jsonParser.makeHttpRequest(INBOX_URL, "GET",
                        params);

                // Check your log cat for JSON reponse
                Log.d("Inbox JSON: ", json.toString());

                try {
                    inbox = json.getJSONArray(TAG_MESSAGES);
                    // looping through All messages
                    for (int i = 0; i < inbox.length(); i++) {
                        JSONObject c = inbox.getJSONObject(i);

                        // Storing each json item in variable

                        String from = c.getString(TAG_FROM);
                        String subject = c.getString(TAG_SUBJECT);
                        String date = c.getString(TAG_DATE);

                        // creating new HashMap
                        HashMap<String, String> map = new HashMap<String, String>();

                        picture = String.valueOf(R.drawable.ic_launcher);
                        // adding each child node to HashMap key => value

                        map.put(TAG_PIC, picture);
                        map.put(TAG_FROM, from);
                        map.put(TAG_SUBJECT, subject);
                        map.put(TAG_DATE, date);





                        // adding HashList to ArrayList
                        inboxList.add(map);
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }

                return null;
            }

            /**
             * After completing background task Dismiss the progress dialog
             * **/
            protected void onPostExecute(String file_url) {
                // dismiss the dialog after getting all products
                pDialog.dismiss();
                // updating UI from Background Thread
                runOnUiThread(new Runnable() {
                    public void run() {
                        /**
                         * Updating parsed JSON data into ListView
                         * */
                        SimpleAdapter adapter = new SimpleAdapter(
                                AllEvents.this, inboxList,
                                R.layout.event_list_item, new String[] { TAG_PIC, TAG_FROM, TAG_SUBJECT, TAG_DATE, },
                                new int[] {  R.id.imageView1,R.id.from, R.id.subject, R.id.date });
                        // updating listview



                        setListAdapter(adapter);
                    }
                });

            }

        }

and layout 'event_list_item.xml' used by the SimpleAdapter is as follows: 和SimpleAdapter使用的布局'event_list_item.xml'如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <!-- From Label -->

    <ImageView
        android:id="@+id/imageView1"
         android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="hello"
            android:paddingTop="10dp"
            android:paddingRight="10dp"
            android:paddingBottom="10dp"   
            android:layout_toLeftOf="@+id/subject"
         />


    <TextView
        android:id="@+id/from"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingTop="8dip"
        android:paddingLeft="8dip"
        android:paddingBottom="4dip"
        android:textSize="20sp"
        android:textStyle="bold" />

    <!-- Mail Subject -->
    <TextView android:id="@+id/subject"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:paddingLeft="8dip"
        android:paddingBottom="6dip"
        android:textSize="15sp"
        android:layout_below="@id/from"/>

    <!-- Mail date -->
    <TextView android:id="@+id/date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:padding="8dip"/>



</RelativeLayout>

What do I do to get a lazy-list like View in a Simple Adapter? 我该怎么做才能获得像简单适配器中的View这样的惰性列表?

An image is not a string, so replace string by String Object like that : 图像不是字符串,因此用String对象替换字符串,如下所示:

   ArrayList<HashMap<String, object>> inboxList = new ArrayList<HashMap<String, object>>();
    HashMap<String, Object> map = new HashMap<String, Object>();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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