0

I am trying to create a listview item with an imageview to the right of a textview.. lets say if the text was 3 lines i want the text of the first 2 lines to begin after the image view with some padding.. and the third line to be at the bottom of the image with no padding..

I used this code in my list adapter to do this..

    TextView commenterName = (TextView) row
            .findViewById(R.id.commenter_name);
    commentText = (TextView) row.findViewById(R.id.tv);
    ImageView commenterPhoto = (ImageView) row
            .findViewById(R.id.commenterPhoto);
    Display display = ((WindowManager) row.getContext()
            .getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    FlowTextHelper.tryFlowText(commentsArray.get(pos).getComment(),
            commenterPhoto, commentText, display, 0);
    commenterName.setText(commentsArray.get(pos).getCommenter());
    commentText.setTextColor(context.getResources().getColor(R.color.Black));
    commentText.setGravity(Gravity.RIGHT);

this working fine.. but when scrolling the list view the text loses its padding and get over the image and out of the screen..

UPDATE: This was the part causing the problem.. i put the else condition to set the default photo but the problem still exist..

String path = url.substring(url.lastIndexOf("/") + 1, url.length());
    sd = new File(Environment.getExternalStorageDirectory()
            + Constants.user_images_path + path);

    if (sd.exists()) {
        Bitmap thumbnail = null;
        try {
            //File filePath = context.getFileStreamPath(url);
            FileInputStream fi = new FileInputStream(sd);
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 4;
            thumbnail = BitmapFactory.decodeStream(fi, null, options);
            if(thumbnail != null)
                holder.commenterPhoto.setImageBitmap(thumbnail);
            else
                holder.commenterPhoto.setBackgroundResource(R.drawable.commenter_default);
        } catch (Exception ex) {
            Log.e("getThumbnail() on internal storage", ex.getMessage());
        }
    }

I don't know why this is happening.. Can you help me please?

Arwa
  • 161
  • 9

3 Answers3

1

OK.. I solved the problem by setting my imageView background in the condition of convertview in my getView() method before setting the tag of my holder.. just like this:

    View row = convertView; 

    if (row == null) {
        LayoutInflater infalInflater = (LayoutInflater) this.context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = infalInflater.inflate(resource, null);
        holder = new ViewHolder();
        holder.commentText = (TextView) row.findViewById (R.id.tv);
        holder.commenterPhoto = (ImageView) row.findViewById (R.id.commenterPhoto);
        holder.commenterName = (TextView) row.findViewById (R.id.commenter_name);
        holder.date = (TextView) row.findViewById (R.id.date);
        holder.show = (ImageButton) row.findViewById(R.id.show);

        String path = url.substring(url.lastIndexOf("/") + 1, url.length());
        sd = new File(Environment.getExternalStorageDirectory()
                + Constants.user_images_path + path);

        Bitmap thumbnail = null;

        if (sd.exists()) {
            try {

                //File filePath = context.getFileStreamPath(url);
                FileInputStream fi = new FileInputStream(sd);
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inSampleSize = 4;
                thumbnail = BitmapFactory.decodeStream(fi, null, options);
                if(thumbnail != null)
                    holder.commenterPhoto.setImageBitmap(thumbnail);
                else
                    holder.commenterPhoto.setBackgroundResource(R.drawable.commenter_default);

            } catch (Exception ex) {
                Log.e("getThumbnail() on internal storage", ex.getMessage());
            }
        }else{
            Toast.makeText(context, "No Image Found", Toast.LENGTH_LONG).show();
        }

        row.setTag(holder); 
    }
    else{
        holder = (ViewHolder) row.getTag(); 
    }
Arwa
  • 161
  • 9
0

Be careful with if...else blocks inside the getView() method in the adapter. If you set the background color in red of a LinearLayout inside an if statement you should put the "original" color in the else statement. This is caused by the ListView recycling views. You can see it better explained here

Community
  • 1
  • 1
jcasero
  • 46
  • 3
0

Try to add an "else" in the if(sd.exist()) block. I have supposed that this code is in getView().

jcasero
  • 46
  • 3