Unable to change ImageView visibility

I have a ListView using a custom adapter to populate a ListView.

row.xml

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <LinearLayout android:orientation="vertical" android:layout_width="0dip" android:layout_weight="1" android:layout_height="fill_parent"> <TextView android:id="@+id/title" android:layout_width="fill_parent" android:layout_height="wrap_content" android:singleLine="true" android:gravity="center_vertical" android:ellipsize="marquee" android:textSize="24dp" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/artist" android:singleLine="true" android:ellipsize="marquee" android:textSize="14dp" /> </LinearLayout> <ImageView android:id="@+id/currentplaying" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_marginLeft="1dip" android:src="@android:drawable/ic_media_play" android:contentDescription="@string/now_playing" android:visibility="gone" /> </LinearLayout> 

As you can see, the visibility of ImageView has disappeared. I want to make it visible to one particular line. Here is the code I tried, but this does not work ...

 View view = getListView().getAdapter().getView(0, null, null); ImageView iv = (ImageView)view.findViewById(R.id.currentplaying); iv.setVisibility(ImageView.VISIBLE); 

Thanks in advance.

 @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if(convertView==null) { holder = new ViewHolder(); convertView = inflater.inflate(R.layout.yourlayout, null); holder.imgViewLogo = (ImageView) convertView.findViewById(R.id.imgViewLogo); convertView.setTag(holder); } else { holder=(ViewHolder)convertView.getTag(); } if(position==0) { holder.imgViewLogo.setVisiblity(View.VISIBLE); } return convertView; } 

EDIT:

I have earned. I used this to trigger ListView activity.

 intent.putExtra("id", c.getInt(c.getColumnIndex(DatabaseHelper._ID))); startActivity(intent); 

In the ListView element,

 currentplayingid = getIntent().getExtras().getInt("id"); 

Then I added this to bindview()

 ImageView imgview = (ImageView)view.findViewById(R.id.currentplaying); int id = c.getInt(c.getColumnIndex(DatabaseHelper._ID)); if (id == SongsListActivity.this.currentplayingid) imgview.setVisibility(View.VISIBLE); else imgview.setVisibility(View.GONE); 
+8
android android-imageview
source share
10 answers

I have earned. I used this to start browsing the list.

 intent.putExtra("id", c.getInt(c.getColumnIndex(DatabaseHelper._ID))); startActivity(intent); 

In the activity list

 currentplayingid = getIntent().getExtras().getInt("id"); 

Then I added this to bindview ()

 ImageView imgview = (ImageView)view.findViewById(R.id.currentplaying); int id = c.getInt(c.getColumnIndex(DatabaseHelper._ID)); if ( id == SongsListActivity.this.currentplayingid ) imgview.setVisibility(View.VISIBLE); else imgview.setVisibility(View.GONE); 
+2
source share

perhaps you should do this in the getView() your adapter

EDIT:

 @Override public View getView(int position, View convertView, ViewGroup parent) { // codes... if (position == 0) { holder.imgViewLogo.setVisibility(ImageView.VISIBLE); } else { holder.imgViewLogo.setVisibility(ImageView.GONE); } // codes... } 
+1
source share

You should do it like this:

 iv.setVisibility(View.VISIBLE); 
0
source share

Try the following code as follows:

  private class ViewHolder { ImageView imgViewLogo; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub ViewHolder holder; if(convertView==null) { holder = new ViewHolder(); convertView = inflater.inflate(R.layout.yourlayout, null); holder.imgViewLogo = (ImageView) convertView.findViewById(R.id.imgViewLogo); convertView.setTag(holder); } else { holder=(ViewHolder)convertView.getTag(); } if(position==0) { holder.imgViewLogo.setVisiblity(View.VISIBLE); } return convertView; } 

It works for me ... It can help you.

0
source share

I ran into similar problems when several widgets appeared for some rows, but not for others. The problems were caused by recycling. I'm not quite sure this problem is here, but you should still handle it. The trick is to set the visibility for each row; and not just for the line you want to display / disappear.

So:

 if (position == 0) { iv.setVisibility(ImageView.VISIBLE); } else { iv.setVisibility(ImageView.GONE); } 

Otherwise, you assume that for positions other than 0, the visibility is GONE, but this may not be the case when view processing is used. By the way, I am doing this work in bindView. Not sure if this is technically correct.

0
source share

If it works for a while, maybe I can help you. It so happens that whenever you move the list, it recreates all the views again, in which case it never saves the last state of view. So what you need to do is save the state of each of your images in getView (), which you must set accordingly. I am posting one of my answers that may help you. Here is a little code for your help: I ​​will create a logical arraylist.

private ArrayList imageview_visible = null;

Then I will set the states of the whole image to false in my constructor:

 for (int i=0; i < no_of_elements.size(); i++) { imageview_visible.add(i, false); } 

In your getView write this code:

public View getView (int position, View convertView, parent group ViewGroup) {// WRITE YOUR CODE.

  if (imageview_visible.get(position) == true) { //SET YOUR IMAGE VIEW AS VISIBLE } else { // SET IMAGEVIEW AS GONE } 

}

Whenever you open or hide your view, just save it in imageview_visible.set (true or false), this will save the state of all your images and adjust each image accordingly.

0
source share

Use LayoutInflater to get view object

 LayoutInflater inflater = this.getLayoutInflater(); View rowView = inflater.inflate(R.layout.row, null, true); ImageView iv = (ImageView)view.findViewById(R.id.currentplaying); iv.setVisibility(ImageView.VISIBLE); 
0
source share

I have the same problem ... I solved with a non-standard solution, but worked for me ...

 v.setImageResource(R.color.transparent); 

import R from android

 import android.R; 
0
source share

Both iv.setVisibility(View.VISIBLE); and iv.setVisibility(ImageView.VISIBLE); are correct, but it is better to use View instead of ImageView because VISIBLE and GONE defined in the View class.

You change both visibility ( VISIBLE and GONE ) the most in this if . as:

 if(?) iv.setVisibility(View.VISIBLE); else iv.setVisibility(View.GONE); 
0
source share

You can hide or show views using setVisibility (int). use iv.setVisibility(View.VISIBLE);

0
source share

All Articles