自定义列表视图与文本和图像

时间:2012-08-29 10:39:17

标签: android listview asynchronous

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

代码

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

}

1 个答案:

答案 0 :(得分:0)

尝试此example或使用此类

之类的任何适配器
  

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