Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot change the visiblity of an ImageView

I have a ListView using a custom cursoradapter to fill the ListView.

The 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 ImageView's visibility is gone. I want to make it visible for one particular row. Here is the code I tried but it is not working...

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 got it working. I used this to start the ListView activity.

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

In the ListView activity,

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

Then I added this in 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);
like image 441
Dipesh Avatar asked Apr 23 '12 03:04

Dipesh


2 Answers

I got it working. I used this to start the list view activity.

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

In the listview activity,

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

Then i added this in 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);
like image 62
Dipesh Avatar answered Oct 16 '22 04:10

Dipesh


perhaps you should do it in getView() of 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...
}
like image 22
C. Leung Avatar answered Oct 16 '22 02:10

C. Leung