How to put button on a Listview?

Asked

Viewed 1,621 times

2

I have a model that I use in my projects, of list with image, I want instead of the image on the side of the button, just button and title. Look at my Adapter:

Adapter

public class AdapterListView extends BaseAdapter {

  private LayoutInflater mInflater;
  private ArrayList<Categoria> itens;

  public AdapterListView(Context context, ArrayList<Categoria> itens) {
    //Itens que preencheram o listview
    this.itens = itens;
    //responsavel por pegar o Layout do item.
    mInflater = LayoutInflater.from(context);
  }

  /**
   * Retorna a quantidade de itens
   *
   * @return
   */
  public int getCount() {
    return itens.size();
  }

  /**
   * Retorna o item de acordo com a posicao dele na tela.
   *
   * @param position
   * @return
   */
  public Categoria getItem(int position) {
    return itens.get(position);
  }

  /**
   * Sem implementação
   *
   * @param position
  * @return
  */
  public long getItemId(int position) {
    return position;
  }

  public View getView(int position, View view, ViewGroup parent) {
    //Pega o item de acordo com a posção.
    Categoria item = itens.get(position);
    //infla o layout para podermos preencher os dados
    view = mInflater.inflate(R.layout.item_list, null);

    //atravez do layout pego pelo LayoutInflater, pegamos cada id relacionado
    //ao item e definimos as informações.
    ((TextView) view.findViewById(R.id.text)).setText(item.getTexto());
    ((ImageView) view.findViewById(R.id.imagemview)).setImageResource(item.getIconeRid());
  //  ((TextView) view.findViewById(R.id.subtitulo)).setText(item.getSubtitulo());

    return view;
  }
}

Layout

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:padding="5sp">

    <ImageView
        android:id="@+id/imagemview"
        android:layout_width="50dp"
        android:layout_height="50dp"
          android:layout_marginLeft="5sp"
        />

    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="20dp"
        android:layout_marginLeft="15sp"
        android:textStyle="bold"

        android:gravity="center_vertical"
        android:textColor="#FF000000" />    
  </LinearLayout>

I just tried to change ((ImageView) for ((Button) , but it didn’t work.

  • When you switched (Imageview) to (Button) you made in XML and JAVA ? <ImageView android:id="@+id/imagemview" .../> - ((ImageView) view.findViewById(R.id.imagemview))

  • that, I changed everything for boot, but see that in the imageView has a method setImageResource, I wanted to know how it will look in the boot

  • Have you tried using a ImageButton?

  • On the button you can use setText and the setOnClickListener to capture the event. Or use ImageButton as @sicachester said.

1 answer

2


If it’s to replace the ImageView for Button you will also have to replace the method setImageResource by one that the Button has as the setText.

//((ImageView) view.findViewById(R.id.imagemview)).setImageResource(item.getIconeRid());
((Button) view.findViewById(R.id.imagemview)).setText("meu botão");

and use the setOnClickListener to capture the event.

If you want to use the image as a button you can use the ImageButton and mater setImageResource or use an equivalent.

  • click implementation is usually done in Activity?

  • Yes, but it doesn’t have to be in Activity, you can do the implementation in this class.

  • but I’m going to use variables from another

  • So if I’m not mistaken, in your Activity you call a setAdapter passing the instance of its AdapterListView, if that’s what you can create your OnClickListener in Activity and create a method in AdapterListView for you to pass as variable before the setAdapter, so when he starts drawing (getView of AdapterListView) you already have the OnClickListener of Activity available.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.