0

我开发了一个列表视图,列表视图显示图像和文本。第一次必须从网络服务下载图像和文本,然后必须显示,因为它需要更多时间,所以我们认为第一次在列表视图中绑定文本并使用AsyncTask,一旦图像下载图像将显示在活动背景的列表视图中。但我无法做到这一点,我已经完成了一些编码,它下载所有图像,然后绑定图像和文本(在这种情况下,我们需要在 startDownload 之前第一次绑定列表视图两次图像和下载图像后的第二个。所以如果有任何想法请建议我。

代码

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;
    }

}
4

1 回答 1

1

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

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;
}
}
于 2012-08-29T10:46:37.237 回答