自定义列表视图使用文本和图像 [英] Custom Listview with Text and image

查看:151
本文介绍了自定义列表视图使用文本和图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了一个列表视图,列表视图显示图像和text.1st要下载图片和文字形式的网络服务,则必须显示,因为它需要更多的时间,所以我们在列表视图想到第一次绑定文本并使用的AsyncTask 并尽快图像下载图像将在activity.But的背景列表视图我不能这样做,我也做了一些编码,并将其下载所有的图像,然后结合既显示图片和文字(在这种情况下,我们需要前startDownload图像和2月1日两次绑定列表视图下载image.So后,如果任何有一些想法,请给我建议。

code

 公共类LoadImg扩展的AsyncTask<弦乐,太虚,位图> {
    上下文语境;
    串IMG;
    InputStream为= NULL;
    位图位图= NULL;    公共LoadImg(上下文的背景下,字符串IMG){
        // TODO自动生成构造函数存根
        this.context =背景;
        this.img = IMG;
    }    @覆盖
    保护位图doInBackground(字符串... PARAMS){
        // TODO自动生成方法存根
        位图的位图= downImg();
        System.out的
                .println(位图的价值=====================================
                        +位图);
        返回位图;
    }    私人位图downImg(){
        // TODO自动生成方法存根
        位图位图= NULL;
        如果(IMG == NULL){
            位= NULL;
        }其他{            网址URL = NULL;
            尝试{
                URL =新的URL(IMG);            }赶上(MalformedURLException的E){
                // TODO自动生成catch块
                e.printStackTrace();            }
            URLConnection的连接= NULL;
            尝试{                连接= url.openConnection();            }赶上(IOException异常E1){
                // TODO自动生成catch块
                e1.printStackTrace();
            }
            尝试{
                是= connection.getInputStream();
            }赶上(IOException异常五){
                // TODO自动生成catch块
                e.printStackTrace();
            }
            位= BitmapFactory.de codeStream(是);
            的System.out.println(电视图像===+位图);
        }        返回位图;
    }}


解决方案

试试这个的例如或使用任何适配器这样的


  

WeatherAdapter.java


 公共类WeatherAdapter扩展ArrayAdapter<天气及GT; {上下文语境;
INT layoutResourceId;
气象数据[] = NULL;公共WeatherAdapter(上下文的背景下,INT layoutResourceId,天气[]数据){
    超级(上下文,layoutResourceId,数据);
    this.layoutResourceId = layoutResourceId;
    this.context =背景;
    this.data =数据;
}@覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){
    查看排= convertView;
    WeatherHolder支架=无效;    如果(行== NULL)
    {
        LayoutInflater充气=((活动)上下文).getLayoutInflater();
        行= inflater.inflate(layoutResourceId,父母,假);        持有人=新WeatherHolder();
        holder.imgIcon =(ImageView的)row.findViewById(R.id.imgIcon);
        holder.txtTitle =(TextView中)row.findViewById(R.id.txtTitle);        row.setTag(保持器);
    }
    其他
    {
        支架=(WeatherHolder)row.getTag();
    }    天气气象数据= [位置]
    holder.txtTitle.setText(weather.title);
    holder.imgIcon.setImageResource(weather.icon);    返回行;
}静态类WeatherHolder
{
    ImageView的imgIcon;
    TextView的txtTitle;
}
}

I have developed a listview,listview displaying image and text.1st have to download image and text form web-service then have to display because it takes more time so we thought 1st bind text in listview and use AsyncTaskand as soon as image download image will be shown in the listview in the background of activity.But i'm unable to do that i have done some coding and it download all image and then bind both image and text(in this case we need to bind listview two times 1st before startDownload image and 2nd after download image.So if any has some idea please suggest me.

Code

public class LoadImg extends AsyncTask<String, Void, Bitmap> {
    Context context;
    String img;
    InputStream is = null;
    Bitmap bitmap = null;

    public LoadImg(Context context, String img) {
        // TODO Auto-generated constructor stub
        this.context = context;
        this.img = img;
    }

    @Override
    protected Bitmap doInBackground(String... params) {
        // TODO Auto-generated method stub
        Bitmap bitmap = downImg();
        System.out
                .println("Value of bitmap====================================="
                        + bitmap);
        return bitmap;
    }

    private Bitmap downImg() {
        // TODO Auto-generated method stub
        Bitmap bitmap = null;
        if (img == null) {
            bitmap = null;
        } else {

            URL url = null;
            try {
                url = new URL(img);

            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();

            }
            URLConnection connection = null;
            try {

                connection = url.openConnection();

            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            try {
                is = connection.getInputStream();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            bitmap = BitmapFactory.decodeStream(is);
            System.out.println("TV Image===" + bitmap);
        }

        return bitmap;
    }

}

解决方案

Try this example or use any adapter like this

WeatherAdapter.java

public class WeatherAdapter extends ArrayAdapter<Weather>{

Context context;
int layoutResourceId;   
Weather data[] = null;

public WeatherAdapter(Context context, int layoutResourceId, Weather[] data) {
    super(context, layoutResourceId, data);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
    this.data = data;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    WeatherHolder holder = null;

    if(row == null)
    {
        LayoutInflater inflater = ((Activity)context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);

        holder = new WeatherHolder();
        holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
        holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);

        row.setTag(holder);
    }
    else
    {
        holder = (WeatherHolder)row.getTag();
    }

    Weather weather = data[position];
    holder.txtTitle.setText(weather.title);
    holder.imgIcon.setImageResource(weather.icon);

    return row;
}

static class WeatherHolder
{
    ImageView imgIcon;
    TextView txtTitle;
}
}

这篇关于自定义列表视图使用文本和图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆