Android 带有文本和图像的自定义Listview

Android 带有文本和图像的自定义Listview,android,listview,asynchronous,Android,Listview,Asynchronous,我开发了一个列表视图,listview显示图像和文本。首先必须从web服务下载图像和文本,然后才能显示,因为这需要更多的时间,所以我们认为首先要在listview中绑定文本并使用异步任务,一旦图像下载图像将显示在活动背景的listview中。但我无法做到这一点,我已经做了一些工作编码并下载所有图像,然后绑定图像和文本(在这种情况下,我们需要绑定listview两次,第一次在开始下载图像之前,第二次在下载图像之后。如果有任何想法,请建议我 代码 public class LoadImg exten

我开发了一个列表视图,listview显示图像和文本。首先必须从web服务下载图像和文本,然后才能显示,因为这需要更多的时间,所以我们认为首先要在listview中绑定文本并使用
异步任务
,一旦图像下载图像将显示在活动背景的listview中。但我无法做到这一点,我已经做了一些工作编码并下载所有图像,然后绑定图像和文本(在这种情况下,我们需要绑定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;
    }

}
公共类LoadImg扩展异步任务{
语境;
字符串img;
InputStream=null;
位图=空;
公共加载img(上下文,字符串img){
//TODO自动生成的构造函数存根
this.context=上下文;
this.img=img;
}
@凌驾
受保护位图doInBackground(字符串…参数){
//TODO自动生成的方法存根
位图位图=向下img();
系统输出
.println(“位图的值=====================================================”
+位图);
返回位图;
}
私有位图downImg(){
//TODO自动生成的方法存根
位图=空;
如果(img==null){
位图=空;
}否则{
URL=null;
试一试{
url=新url(img);
}捕获(格式错误){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
URLConnection=null;
试一试{
connection=url.openConnection();
}捕获(IOE1异常){
//TODO自动生成的捕捉块
e1.printStackTrace();
}
试一试{
is=connection.getInputStream();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
位图=BitmapFactory.decodeStream(is);
System.out.println(“电视图像==”+位图);
}
返回位图;
}
}
试试这个或使用任何类似的适配器

WeatherAdapter.java

公共类WeatherAdapter扩展了ArrayAdapter{
语境;
国际布局资源;
天气数据[]=null;
公共WeatherAdapter(上下文上下文、内部布局资源ID、天气[]数据){
超级(上下文、布局资源ID、数据);
this.layoutResourceId=layoutResourceId;
this.context=上下文;
这个数据=数据;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图行=转换视图;
WeatherHolder=null;
if(行==null)
{
LayoutInflater充气器=((活动)上下文)。getLayoutInflater();
行=充气机。充气(layoutResourceId,父级,false);
支架=新的密封支架();
holder.imgIcon=(ImageView)row.findViewById(R.id.imgIcon);
holder.txtTitle=(TextView)row.findViewById(R.id.txtTitle);
row.setTag(支架);
}
其他的
{
holder=(WeatherHolder)row.getTag();
}
天气=数据[位置];
holder.txtTitle.setText(weather.title);
holder.imgIcon.setImageResource(weather.icon);
返回行;
}
静态类风挡
{
ImageView imgIcon;
TextView-txtTitle;
}
}

r在我的环境中,你肯定能做到这一点……。首先阅读我的问题,我必须在活动背景下从服务器下载图像,一旦图像下载必须绑定到listview中,请正确阅读我的问题。此链接将帮助你感谢Rajesh,Deepak为我编辑了你的答案,因此我不能投票支持此答案。你知道吗r答案很好,再次感谢您。@Dilip现在您有了解决方案了吗?@是的,但是stackoverflow.com/questions/541966/…不在编辑的代码上面有用谢谢..谢谢Andro,你能详细告诉我怎么做吗..我想用户已经在他的评论中提供了答案的链接。转到该链接,查看Fedor的答案.这是最好的策略,而且很容易实施。
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;
}
}