Dharmik Patel
Dharmik Patel

Reputation: 66

images in Android ListView

All

I am new to Android , If i ask a question in bad manner please inform me so i will take care of that next time.

My Question :

I create Listview , In that list views each row has three elements .

1) ImageView : which display (URL and i convert in Bitmap)
2) Name : Text
3) Address : Text

I successfully created that row but i face one small issue

++++> First when all rows are displayed at that time i am not able to see that Image of every Row . and when i scroll that list then once i can see that images.

help me if any one face this issue.

Code

BaseAdapter class :

@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 code

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

Thanks,
Dharmik

Upvotes: 0

Views: 109

Answers (3)

Gaurav Vashisth
Gaurav Vashisth

Reputation: 7727

Dharmik, you can use picasso http://square.github.io/picasso/ for loading the image.

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

Upvotes: 1

Zubair Ahmed
Zubair Ahmed

Reputation: 2897

you should set your Views data out side the body of if(convertView == null). For your help replace your getView() method with below one

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

Hope it will help you.

Upvotes: 0

lgw150
lgw150

Reputation: 178

please post your code,it may in your Adapter class,which in the getView() method,Please use View holder pattern to preventing duplication of row

 @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();
}
....................

Upvotes: 1

Related Questions