开发者

Android LazyList set image issue

Actually the thing that I want to ask here is more like to get some suggestions which is the best way to do the thing that I want. I'm using Lazy Loading List in my application. I need to be able to set a different image to a list view item depending on that if user is subscribed to that image or not. If he is not, it will shows a default image. If he is, the real image. Here is the code which I am using :

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class LazyAdapter extends BaseAdapter {


    private Activity activity;
    private String[] data;
    private static LayoutInflater inflater=null;
    public ImageLoader imageLoader; 

    public LazyAdapter(Activity a, String[] d) {
        activity = a;
        data=d;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader=new ImageLoader(activity.getApplicationContext());
    }

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

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

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

    public static class ViewHolder{
        public TextView name,info;
        public ImageView image;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=开发者_Python百科convertView;
        ViewHolder holder;
        if(convertView==null){
            vi = inflater.inflate(R.layout.item, null);
            holder=new ViewHolder();
            holder.name=(TextView)vi.findViewById(R.id.name);
            holder.info=(TextView)vi.findViewById(R.id.info);
            holder.image=(ImageView)vi.findViewById(R.id.image);
            vi.setTag(holder);
            Log.v("",""+position);
        }
        else
            holder=(ViewHolder)vi.getTag();

        holder.name.setText("");
        holder.info.setText("");
        holder.image.setTag(data[position]);
        imageLoader.DisplayImage(data[position], activity, holder.image);
        return vi;
    }
}

So I want to set different text to name field, to info field, and different image depending on that if user is subscribed or not. The texts I'm getting from Sqlite database. So any ideas or suggestions which is the best way to do this?

I'll really appreciate any kind of help!Thanks!


if (userIsSubscribed) {
    holder.name.setText("subscribedUserName");
    holder.info.setText("subscribedUserInfo");
    holder.image.setImageResource(drawable.userSubscribedImage);
} else {
    holder.name.setText("unsubscribedUserName");
    holder.info.setText("unsubscribedUserInfo");
    holder.image.setImageResource(drawable.userUnSubscribedImage);
}


So you have your Data in your variable "data"

use your data in your getView method like you do with your Image

    holder.name.setText(data[position].getName());
    holder.info.setText(data[position].getInfo());
    holder.image.setTag(data[position]);
    imageLoader.DisplayImage(data[position], activity, holder.image);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜