2011-06-03 45 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