2011-06-03 87 views
2

我有一堆資源圖像,我想爲ListView的每個元素顯示一個特定的圖像。目前我有一個row.xml文件,每個元素都顯示「圖標」圖像。我想使用我在row.xml中指定的相同格式,但將圖像更改爲我想要的每次。Android:如何在ListView中動態加載資源圖片?

我已經做了一些關於「延遲加載」的研究,但它似乎是過於複雜,並不適用於此。

回答

1

您應該重寫listview的適配器的getView方法,以便它將返回一個將使您的row.xml充氣的組件。然後可以在返回之前對該組件進行自定義。

下面是一個例子:

@Override 
public View getView(int position, View convertView, ViewGroup parent) 
{ 
    //where myview is your custom class loading your row.xml 
    if(!(convertView instanceof MyView)) 
     convertView = new MyView(); 

    MyView v = (MyView) convertView; 
    v.setImage(<your image depending on position>); 
    return convertView; 
}//met 

問候, 斯特凡

+0

謝謝,我把那個方法放在哪裏? – 2011-06-03 21:08:11

+0

瞭解如何定義類,繼承和重寫超類的方法。這不是「我把代碼放在哪裏」的問題,而是關於理解面向對象編程的問題。 Regards, Stéphane – Snicolas 2011-06-03 21:13:52

+1

哦哇我剛纔意識到,我已經有一個公共View getView方法重寫,這就是爲什麼我得到這麼多的錯誤。我只需要在它的底部添加2行,並且它完美地工作。 – 2011-06-03 21:29:03

1

好吧,擴大在上述答案,你做一個「適配器」類似於下面的代碼所示:

package com.example.consumer.adapter; 

import java.util.ArrayList; 

import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.TextView; 

import com.example.consumer.R; 
import com.example.consumer.model.AppModel; 
import com.example.consumer.model.Deal; 

public class DealListAdapter extends BaseAdapter { 

    private static ArrayList<Deal> dealArrayList = new ArrayList<Deal>(); 

    private LayoutInflater inflator; 
    private AppModel appModel; 

    public DealListAdapter(Context context, ArrayList<Deal> results) { 
     dealArrayList = results; 
     inflator = LayoutInflater.from(context); 
     appModel = (AppModel) context.getApplicationContext(); 
    } 

    public int getCount() { 
     if (dealArrayList == null) 
      return 0; 
     else 
      return dealArrayList.size(); 
    } 

    public Object getItem(int position) { 
     try { 
      return dealArrayList.get(position); 
     } catch (Exception e) { 
      e.printStackTrace(); 
      return null; 
     } 
    } 

    public long getItemId(int position) { 
     return position; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
     ViewHolder holder; 
     if (convertView == null) { 
      convertView = inflator.inflate(R.layout.list_item_deal, null); 
      holder = new ViewHolder(); 
      holder.name = (TextView) convertView.findViewById(R.id.deal_listview_item_one); 
      holder.distance = (TextView) convertView.findViewById(R.id.deal_listview_item_two); 

      convertView.setTag(holder); 
     } else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 

     holder.name.setText(dealArrayList.get(position).getMerchantName() + ": "+ dealArrayList.get(position).getTitle()); 
     holder.distance.setText(dealArrayList.get(position).getDistanceFormatted(appModel.getLocation())); 

     return convertView; 
    } 

    static class ViewHolder { 
     TextView name; 
     TextView distance; 
    } 
} 

請注意使用ViewHolder - 這將節省大量的「findViewById」調用。

要在列表中使用該適配器,您使用此代碼:

dealList = (ListView) findViewById(R.id.browse_list_view); 
    dealListAdapter = new DealListAdapter(this, dealList); 
    dealList.setAdapter(dealListAdapter); 

凡在這種情況下,dealList是優惠的ArrayList(你可以使用一些其他的自定義對象)

希望這有助於您的OO編程... :)

+1

我其實已經想通了,我已經執行了Stephane的解決方案,這導致了令人困惑的錯誤。謝謝你:) – 2011-06-03 21:37:36