如何更改动态列表的图像?

时间:2011-05-16 11:56:51

标签: android

我有动态列表视图,其中包含1个imageview和1个textview。 我的问题是我必须在onclick中更改特定位置的图像 这是我的代码

public class CoverLetterAdaptor extends BaseAdapter
{
    String[] coverLetterItems;
    Context context;    
    private LayoutInflater mInflater;   
    int ht,wt;

public CoverLetterAdaptor(Context context,int ht,int wt, String[] coverLetterTitle)
{
    this.context = context;
    this.coverLetterItems = coverLetterTitle ;          
    this.ht = ht;
    this.wt = wt;
    mInflater = LayoutInflater.from(context);
}

public int getCount() {
    return coverLetterItems.length;
}

public Object getItem(int position) {
    return position;
}

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

public View getView( final int position, View convertView, ViewGroup parent) 
{
    ViewHolder holder;
    if (convertView == null)
    {
        convertView = mInflater.inflate(R.layout.coverletteradaptor, null);     
        holder = new ViewHolder();

        holder.coverLetterTxt = (TextView) convertView.findViewById(R.id.coverLetterAdaptorTxt);
        holder.bgImageCCAdaptor = (ImageView)convertView.findViewById(R.id.bgimageCoverLetter);
        holder.bgimageCoverLetter = (ImageView)convertView.findViewById(R.id.bgimageCoverLetterr);
        holder.bgimageCoverLetter.setLayoutParams(new RelativeLayout.LayoutParams(wt, ht/7));       
        convertView.setTag(holder);
     }
    else                
        holder = (ViewHolder) convertView.getTag();
    holder.coverLetterTxt.setText(coverLetterItems[position]);

    return convertView;
} 

static class ViewHolder
{
    TextView coverLetterTxt;        
    ImageView bgImageCCAdaptor;
    ImageView bgimageCoverLetter;
}

}

请帮助我 感谢

1 个答案:

答案 0 :(得分:0)

您可以实施OnItemclicklistener,然后对Itemclicks做出反应。然后使用此方法更改位图的来源。

现在使用上述方法。这是代码。

 @Override
        public void onItemClick(AdapterView<?> adaptview, View clickedview, int position,
                long id) {

            //TEST SO 4533684
            View subview = clickedview.findViewById(R.id.bgimageCoverLetter);
            ((ImageView) subview).setImageResource(R.drawable.icon);
            //END TEST 
        }