2012-10-25 25 views
5

有人能指出我在正確的方向來獲取這個SimpleAdapter裏面的ImageView嗎? TAG_IMAGE應該是Imageview。 我得到這個:類型mismatch:無法從ImageView轉換爲int。 是的,我是一個noob。我想在SimpleAdapter中使用Imageview嗎?

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 
       * */ 
       ListAdapter adapter = new SimpleAdapter(
         AllProductsActivity.this, productsList, 
         R.layout.list_item, new String[] { TAG_PID, 
           TAG_NAME, TAG_PRICE, TAG_RANDOM, TAG_IMAGE },        
         new int[] { R.id.pid, R.id.name, R.id.price, 
           R.id.random, R.id.image }); 
       // Imageview to show 
       // updating listview 
       setListAdapter(adapter); 
      } 
     }); 

回答

14

main.xml中與下面的代碼給定

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

    <TextView 
     android:id="@+id/textview" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
    /> 

    <ListView 
     android:id="@+id/listview" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
    /> 
</LinearLayout> 

listview_layout.xml並更新內容與下面碼給定

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" 
> 
    <ImageView 
     android:id="@+id/flag" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:contentDescription="@string/hello" 
     android:paddingTop="10dp" 
     android:paddingRight="10dp" 
     android:paddingBottom="10dp" 
    /> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
    > 
     <TextView 
      android:id="@+id/txt" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textSize="15dp" 
     /> 

     <TextView 
      android:id="@+id/cur" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textSize="10dp" 
     /> 
    </LinearLayout> 
</LinearLayout> 

MainActivity.java與下面碼

給定
package com.example.list; 

import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 

import android.app.Activity; 
import android.os.Bundle; 
import android.widget.ListView; 
import android.widget.SimpleAdapter; 

public class MainActivity extends Activity { 

    // Array of strings storing country names 
    String[] countries = new String[] { 
     "India", 
     "Pakistan", 
     "Sri Lanka", 
     "China", 
     "Bangladesh", 
     "Nepal", 
     "Afghanistan", 
     "North Korea", 
     "South Korea", 
     "Japan" 
    }; 

    // Array of integers points to images stored in /res/drawable-ldpi/ 
    int[] flags = new int[]{ 
     R.drawable.india, 
     R.drawable.pakistan, 
     R.drawable.srilanka, 
     R.drawable.china, 
     R.drawable.bangladesh, 
     R.drawable.nepal, 
     R.drawable.afghanistan, 
     R.drawable.nkorea, 
     R.drawable.skorea, 
     R.drawable.japan 
    }; 

    // Array of strings to store currencies 
    String[] currency = new String[]{ 
     "Indian Rupee", 
     "Pakistani Rupee", 
     "Sri Lankan Rupee", 
     "Renminbi", 
     "Bangladeshi Taka", 
     "Nepalese Rupee", 
     "Afghani", 
     "North Korean Won", 
     "South Korean Won", 
     "Japanese Yen" 
    }; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     // Each row in the list stores country name, currency and flag 
     List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>(); 

     for(int i=0;i<10;i++){ 
      HashMap<String, String> hm = new HashMap<String,String>(); 
      hm.put("txt", "Country : " + countries[i]); 
      hm.put("cur","Currency : " + currency[i]); 
      hm.put("flag", Integer.toString(flags[i])); 
      aList.add(hm); 
     } 

     // Keys used in Hashmap 
     String[] from = { "flag","txt","cur" }; 

     // Ids of views in listview_layout 
     int[] to = { R.id.flag,R.id.txt,R.id.cur}; 

     // Instantiating an adapter to store each items 
     // R.layout.listview_layout defines the layout of each item 
     SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList, R.layout.listview_layout, from, to); 

     // Getting a reference to listview of main.xml layout file 
     ListView listView = (ListView) findViewById(R.id.listview); 

     // Setting the adapter to the listView 
     listView.setAdapter(adapter); 
    } 
} 

運行應用程序

+0

這非常有幫助。謝謝。 – Jirapong

+4

我不明白適配器如何知道它應該將該標誌設置爲ImageView的src,並且它應該爲TextView設置文本。是因爲它看着目標視圖的類型並做了一個假設,例如如果它是一個ImageView,那麼相關的映射值必須是一個可以用作src的drawable? – RTF

+0

@Code這並不能解釋適配器如何知道R.id.flag引用了ImageView,而R.id.txt引用了TextView。看起來SimpleAdapter對引用的元素進行了一些內省,然後它根據元素類型神奇地知道要設置哪個屬性(text或src)。那是對的嗎? – lintmachine

相關問題