3

我正在使用 SimpleAdapterListView在 JSON 解析数据的哈希图中显示 a 。一切都很完美。如何有效地在其中添加图像ListView(或向 中添加图像Map)?将图像添加到哈希映射不会显示图像,也不会引发错误或异常!

这是我的一段代码..

    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);
                    }
                });

            }

        }

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>

我该怎么做才能获得像 View in a Simple Adapter 这样的惰性列表?

4

1 回答 1

0

图像不是字符串,所以用字符串对象替换字符串,如下所示:

   ArrayList<HashMap<String, object>> inboxList = new ArrayList<HashMap<String, object>>();
    HashMap<String, Object> map = new HashMap<String, Object>();
于 2014-05-20T04:26:07.923 回答