How to inflate a button inside a ListView Item?

0

I need to put a button inside an item in a ListView to look like the image below:

But I'm doing everything dynamically, I get a vector with data from the database and I fill in the ListView rows. Does anyone know how to insert a button for each row in the listView? Thank you in advance!

    
asked by anonymous 31.08.2015 / 17:57

2 answers

3

To do everything dynamically and using a custom list you need to create an xml only for one item in this listView that will be used in all other fields in the list. The second step required to create this list is to create a variable of type ArrayList<suaclasse> with the data class you created to dynamically populate each line in your list.

To create your specific xml for each line try something like:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">

<TextView
    android:id="@+id/tv_texto"
    android:layout_width="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_height="wrap_content" />
<Button
    android:id="@+id/bt_botao"
    android:layout_width="wrap_content"
    android:onClick="CliqueBotao"
    android:layout_alignParentEnd="true"
    android:layout_height="wrap_content" />
</RelativeLayout>

After this in your main class you need to create a class that extends the BaseAdapter class so you can inflate this layout. And for that the best practice to be used is the design pattern MyViewHolder where you can take a look at this link to know more of this: link

Now let's create your class in order to insert text in both TextView and Button:

class SuaClasse{
    public String TvTitulo, BtTitulo;
    SuaClasse(String tv_titulo, String bt_titulo){
        this.TvTitulo = tv_titulo;
        this.BtTitulo = bt_titulo;
    }
}

Now for us to manipulate the data using the created xml we have to create a class to find the xml widgets for each line:

class MinhaViewHolder{
    TextView tv_texto;
    Button bt_botao;
    MinhaViewHolder(View v){
        tv_texto = (TextView) v.findViewById(R.id.tv_texto);
        bt_botao = (Button) v.findViewById(R.id.bt_botao);
    }
}

Now we should create the class that extends the BaseAdapter as follows:

class MeuAdapter extends BaseAdapter{
    Context c;
    MeuAdapter(Context context){
        this.c = context;
    }

    @Override
    public int getCount() {
        return lista.size();//retorna o tamanho da lista
    }

    @Override
    public Object getItem(int position) {
        return lista.get(position);//retorna um item da lista
    }

    @Override
    public long getItemId(int position) {
        return position;//retorna a posição de um item
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View row = convertView;
        MinhaViewHolder holder = null;
        if(row == null){
            LayoutInflater inflater = (LayoutInflater) c
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.single_item, parent, false);
            holder = new MinhaViewHolder(row);
            row.setTag(holder);
        }else{
            holder = (MinhaViewHolder) row.getTag();
        }

        holder.tv_texto.setText(lista.get(position).TvTitulo);
        holder.bt_botao.setText(lista.get(position).BtTitulo);

        return row;
    }
}

Now to add the items in this list you need to initialize the list and then add items to it:

lista = new ArrayList<SuaClasse>();
lista.add(new SuaClasse("texto 1", "clique 1"));
lista.add(new SuaClasse("texto 2", "clique 2"));

And then in your onCreate method say that the adapter of your list will be the adapter that was created to make this list:

adapter = new MeuAdapter(getApplicationContext());
lv_lista.setAdapter(adapter);

In your Activity you should also have the ClickBotao function so that clicking the button performs some action:

public void CliqueBotao(View v){
    //Seu código aqui
}

You can always add items to your list if you need a new item in the list after the list is ready, you can simply add the new item and call the notifyDataSetChanged method of your adapter variable as follows: / p>

lista.add(new SuaClasse("texto 3", "clique 3"));
adapter.notifyDataSetChanged();

I hope this helps you.

    
31.08.2015 / 21:36
-1

I hope this helps you,

link

Thank you

    
31.08.2015 / 20:13