Android ListView中的图像

时间:2014-08-12 07:33:24

标签: android listview

所有

我是Android的新手,如果我以不好的方式提出问题,请通知我,以便下次我会处理。

我的问题:

我创建Listview,在该列表视图中,每行有三个元素。

1)ImageView:哪个显示(URL和i在Bitmap中转换)
2)姓名:文字
3)地址:文字

我成功创建了这一行,但我面临一个小问题

+ + ++>首先,当那时显示所有行时,我无法看到每一行的图像。当我滚动该列表然后我可以看到那些图像。

如果任何人面对这个问题,请帮助我。

BaseAdapter类:

@Override
  public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
holder = null;

LayoutInflater mInflater = (LayoutInflater) context
        .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
    convertView = mInflater.inflate(
            R.layout.mention_timeline_listitems, null);
    holder = new ViewHolder();
    holder.txtTitle = (TextView) convertView.findViewById(R.id.title);
    holder.imageView = (ImageView) convertView.findViewById(R.id.icon);

    convertView.setTag(holder);

    rowItem = (mentionRowItems) getItem(position);

    holder.txtTitle.setText(rowItem.getTitle());

    Thread thread = new Thread(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            try {
                holder.imageView.setImageBitmap(common
                        .imageLoad(rowItem.getImageId()));
            } catch (Exception ex) {
                Log.d("Error------->", ex + "");
            }
        }
    });

    thread.start();
} else {
    holder = (ViewHolder) convertView.getTag();
}

return convertView;

}

rowFile代码

    for(loop){ // 4 times
    rowItems.add(new mentionRowItems(profile_bg_url, name,
                            screen_name));

Example .. profile_bg_url = "https://dl.dropboxusercontent.com/u/72783403/AtoZ/1.png";
Example .. screen_name = "Admin"

    }

                adapter = new mentionBaseAdapter(getActivity(), rowItems);
listView.setAdapter(adapter);

谢谢,
Dharmik

3 个答案:

答案 0 :(得分:1)

Dharmik,您可以使用毕加索http://square.github.io/picasso/来加载图片。

@Override
public View getView(int position, View convertView, ViewGroup parent) {


    LayoutInflater mInflater = (LayoutInflater) context
        .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
        convertView = mInflater.inflate(
            R.layout.mention_timeline_listitems, parent, false);
        holder = new ViewHolder();
        holder.txtTitle = (TextView) convertView.findViewById(R.id.title);
        holder.imageView = (ImageView) convertView.findViewById(R.id.icon);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    rowItem = (mentionRowItems) getItem(position);
    holder.txtTitle.setText(rowItem.getTitle());

    String imageUrl =  rowItem.profile_bg_url.; // image url
    Picasso.with(context).load(imageUrl).into(holder.imageView);
}

答案 1 :(得分:1)

请发布您的代码,它可能在您的Adapter类中,在getView()方法中,请使用View holder模式来防止重复行

 @Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null || !(convertView.getTag() instanceof ViewHolder)) {
  convertView =
      LayoutInflater.from(context).inflate(R.layout.list_item_conversation, parent, false);
  holder = new ViewHolder();
  holder.content = (TextView) convertView.findViewById(R.id.conversation_content);
  holder.masterPhoto =
      (MasterAvatarView) convertView.findViewById(R.id.conversation_photo_master);
  holder.masterPhoto.setDefaultImageResId(R.drawable.default_avatar_small);
  holder.userPhoto = (MasterAvatarView) convertView.findViewById(R.id.conversation_photo_user);
  holder.userPhoto.setDefaultImageResId(R.drawable.default_avatar_small);
  holder.itemLayout = (LinearLayout) convertView.findViewById(R.id.conversation_item);
  holder.time = (TextView) convertView.findViewById(R.id.conversation_time);
  convertView.setTag(holder);
} else {
  holder = (ViewHolder) convertView.getTag();
}
....................

答案 2 :(得分:0)

您应将Views数据设置在if(convertView == null)的正文旁边。为了您的帮助,请将getView()方法替换为下面的

 @Override
      public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    holder = null;

    LayoutInflater mInflater = (LayoutInflater) context
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        convertView = mInflater.inflate(
                R.layout.mention_timeline_listitems, null);
        holder = new ViewHolder();
        holder.txtTitle = (TextView) convertView.findViewById(R.id.title);
        holder.imageView = (ImageView) convertView.findViewById(R.id.icon);

        convertView.setTag(holder);


    } else {
        holder = (ViewHolder) convertView.getTag();
    }

rowItem = (mentionRowItems) getItem(position);

        holder.txtTitle.setText(rowItem.getTitle());

        Thread thread = new Thread(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                try {
                    holder.imageView.setImageBitmap(common
                            .imageLoad(rowItem.getImageId()));
                } catch (Exception ex) {
                    Log.d("Error------->", ex + "");
                }
            }
        });

        thread.start();

    return convertView;
    }

希望它会对你有所帮助。