0

I am trying to get News from one site. Some articles have images. Here is my problem: some article doesn't have images so i want remove that Imageview for that row and title textview should acquire that space. I tried to implement it in BaseAdapter but setvisibility(View.Gone) is not working i guess.

Here is my layout file:

<!-- Thumbnail Image -->
<com.android.volley.toolbox.NetworkImageView
    android:id="@+id/thumbnail"
    android:layout_width="80dp"
    android:layout_height="80dp"
    android:layout_alignParentLeft="true"
    android:layout_marginRight="8dp" />

<!-- News Title -->
<TextView
    android:id="@+id/title"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textSize="@dimen/title"
    android:ellipsize="end"
    android:textStyle="bold"
    android:layout_alignTop="@+id/thumbnail"
    android:layout_toRightOf="@+id/thumbnail"/>

Here is my implementation in base adapter:

  NetworkImageView thumbNail = (NetworkImageView) convertView
            .findViewById(R.id.thumbnail);
    TextView title = (TextView) convertView.findViewById(R.id.title);

    // getting movie data for the row
    Movie m = movieItems.get(position);

    if(m.getThumbnailUrl() == null)
    {

     thumbNail.setVisibility(View.GONE); //Not working

    }
    else {
        thumbNail.setImageUrl(m.getThumbnailUrl(), imageLoader);
    }

And current output is: I want that blank space of Imageview to be used by textview.

Example

When I try with ViewHolder for smooth transition i am unable to implement this logic:

if (position==-1)
    {
        viewHolder.thumbnail.setVisibility(View.GONE);
    }
    else
    {
        viewHolder.thumbnail.setImageUrl(news.getThumbnailUrl(), imageLoader);
    }
Roon13
  • 387
  • 2
  • 23

5 Answers5

1

You can check whether URL for image is valid or not then you can use Visibility gone concept.Use something like below code:

if(!URLUtil.isValidUrl(m.getThumbnailUrl())){
    thumbNail.setVisibility(View.GONE);
}else {
        thumbNail.setImageUrl(m.getThumbnailUrl(), imageLoader);
        thumbNail.setVisibility(View.VISIBLE);
    }

Hope this help you.

Santosh Kathait
  • 1,444
  • 1
  • 11
  • 22
1

try this:

    if(m.getThumbnailUrl() == null)
    {
     thumbNail.setVisibility(View.GONE);
     LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,  LinearLayout.LayoutParams.WRAP_CONTENT);
lp.addrule(LinearLayout.RIGHT_OF,0);
         lp.setMargins(-150,0,0,0); //set your margins for your textview as your want
         title.setLayoutParams(lp);
         Your_Linear_Layout_Name.addView(title, lp);
        //or RelativeLayoutName.addView(title, lp);
        }

        .....
Hawraa Khalil
  • 261
  • 2
  • 12
1

The m.getThumbnailUrl() return a String, right?

Did you try this?

if(m.getThumbnailUrl() == null || TextUtils.isEmpty(m.getThumbnailUrl()))

Try to debug and check if hit this line:

thumbNail.setVisibility(View.GONE);
FelipeJM
  • 136
  • 1
  • 4
1

When you don't have an image for article set LayoutParameters for that particular TextView programmatically.

if(m.getThumbnailUrl() == null)
{

 thumbNail.setVisibility(View.GONE);
 RelativeLayout.LayoutParams layoutParams= (RelativeLayout.LayoutParams) title.getLayoutParams();
 layoutParams.addRule(RelativeLayout.RIGHT_OF,0);
 layoutParams.addRule(RelativeLayout.ALIGN_TOP,0);
 layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,RelativeLayout.TRUE);
 title.setLayoutParams(layoutParams);
}
Praveena
  • 6,340
  • 2
  • 40
  • 53
1

It's because of how the ListView works. You can try something like this:

if(m.getThumbnailUrl() == null)
    {

     thumbNail.setVisibility(View.GONE);
     LayoutParams layoutParams = title.getLayoutParams();
     layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
     layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
     title.setLayoutParams(layoutParams); 
    }

    else {

        thumbanil.setVisibility(View.VISIBLE);
        thumbNail.setImageUrl(m.getThumbnailUrl(), imageLoader);
        LayoutParams layoutParams = title.getLayoutParams();
        layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
        layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        title.setLayoutParams(layoutParams); 

    }
Florin T.
  • 85
  • 1
  • 9
  • maybe m.getThumnailUrl() isn't returning null. – Florin T. May 21 '15 at 13:29
  • it may return something like " " . Not null and not a valid link. do you get some errors in the logs from Volley? – Florin T. May 21 '15 at 13:35
  • I believe that some of the links might be broken, or not converted to valid URI. you can try something from here: http://stackoverflow.com/questions/573184/java-convert-string-to-valid-uri-object ; after checking for null. – Florin T. May 21 '15 at 13:40
  • No one is using it now days. – Roon13 May 21 '15 at 13:49