Question

How can i show and hide some imagebuttons inside a row of a listview on listactivity? Ive some list of packages and want show buttons like install and uninstall. But when a package is installed only uninstall button is showed. If click uninstall, i want to hide uninstall button and show install button. i reference every row in a loop like this:

v = vi.getAdapter().getView(i, null, null);

and reference button like this

ImageButton ib = (ImageButton)v.findViewById(R.id.descargar);
            ib.setVisibility(View.VISIBLE);

nothing happened. Any suggestions?Thanks.

Was it helpful?

Solution

Inheriting the ArrayAdapter class and overwrite the getView function. The getView function always called, when row is displayed.

Create class for rows:

public class MyRow {
   public boolean installed;
}

Implement ArrayAdapter:

public class MyAdapter<MyRow> {

@Override
  public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(R.layout.rowlayout, parent, false);
    // get actual row item.
    MyRow item = (MyRow)getItem(position);
    ImageButton ib = (ImageButton)v.findViewById(R.id.install);
    ImageButton uib = (ImageButton)v.findViewById(R.id.uninstall);

     if (item.installed) {
         ib.setVisibility(View.VISIBLE);
         uib.setVisibility(View.GONE);
    } else {
         ib.setVisibility(View.GONE);
         uib.setVisibility(View.VISIBLE);
    }    

    return rowView;
  }
}

Check this tutorial on how to implement ListAdapter and how to recycle views. http://www.vogella.com/tutorials/AndroidListView/article.html

OTHER TIPS

I don't know what's wrong with your code, but try this:

View v  = getListView().getChildAt(i);

Then proceed to do as you did:

ImageButton ib = (ImageButton)v.findViewById(R.id.descargar);
        ib.setVisibility(View.VISIBLE);

This worked for me. Hope this help.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top