How do I add an OnClickListener to a button inside a ListView adapter?
Asked Answered
R

3

19

I have a listview that contains the list of all my users. Every item from the list is a layout that has a button to show an AlertDialog to change the value of the label of the button. How can I dynamically add an on click event to that button that is generated by the listview Adapter?

This is my adapter:

public class PerfilAdapter extends BaseAdapter {
Context mContext;
LayoutInflater mLayoutInflater;
List<PerfilBean> listaPerfiles = new ArrayList<PerfilBean>();
public Settings01 set=new Settings01();
public PerfilAdapter(Context context,List<PerfilBean> lista) {
    mContext = context;
    mLayoutInflater = (LayoutInflater) mContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    listaPerfiles=lista;
}
@Override
public int getCount() {
    // TODO Auto-generated method stub
    return listaPerfiles.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return listaPerfiles.get(position);
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

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

    if (convertView == null) {
        itemView = (RelativeLayout) mLayoutInflater.inflate(
                R.layout.item_perfil, parent, false);
    } else {
        itemView = (RelativeLayout) convertView;
    }
    // obtengo los valores de la vista
    Button moneda = (Button) itemView.findViewById(R.id.Moneda);
    TextView titulo = (TextView) itemView.findViewById(R.id.Titulo);
    TextView nombredesc = (TextView) itemView.findViewById(R.id.txtNombre);
    TextView descripcion = (TextView) itemView.findViewById(R.id.txtDescripcion);
    String nombreM = Metodos.monedas[listaPerfiles.get(position).getPerfil_tipoMoneda()];
    moneda.setText(nombreM);
    titulo.setText(listaPerfiles.get(position).getPerfil_nombre());
    nombredesc.setText(listaPerfiles.get(position).getPerfil_nombreSec());
    descripcion.setText(listaPerfiles.get(position).getPerfil_texto());

    return itemView;

}
// metodo parahacer la vista de la celda


    public void actualizaDatosLista(List<PerfilBean> listaPerfilesM) {
        for(int i=0;i<listaPerfilesM.size();i++){
            Log.d("ITEM "+i,listaPerfilesM.get(i).getPerfil_nombreSec());
        }
        listaPerfiles = listaPerfilesM;
        notifyDataSetChanged();
    }}

and this is my Activity:

public class Settings01 extends Activity implements OnClickListener {

private List<PerfilBean> lst;
private PerfilDAO perfildao;
private PerfilAdapter perfiladapter;
private ListView lstPerfiles;

public void changeMoneda(final int position) {
    int x = 0;

    AlertDialog.Builder builder = new AlertDialog.Builder(Settings01.this);
    builder.setTitle("Seleccione Tipo de Distribuidor");
    builder.setItems(R.array.moneda, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int item) {
            lst.get(position).setPerfil_tipoMoneda(item);
            perfiladapter = new PerfilAdapter(getApplicationContext(), lst);
            lstPerfiles.setAdapter(perfiladapter);
            dialog.dismiss();
        }

    });
    builder.create();
    builder.show();

}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.settings01);
    lstPerfiles = (ListView) findViewById(R.id.lstSettings);
    perfildao = new PerfilDAOImplDB(Settings01.this);
    lst = new ArrayList<PerfilBean>();
    lst = perfildao.getAll();
    perfiladapter = new PerfilAdapter(getApplicationContext(), lst);
    Log.d("Info", "En Settings");
    lstPerfiles.setAdapter(perfiladapter);



}

@Override
public void onClick(View v) {
    Log.d("Info", "derp" + v.getId());

}}

This is the layout that my adapter is currently using:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
    android:id="@+id/Titulo"
    android:layout_width="wrap_content"
    android:layout_height="40dp"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="150dp"
    android:gravity="left|center_vertical"
    android:textColor="@color/Negro"
    android:text="derp" />

<TextView
    android:id="@+id/lblTipoMoneda"
    android:layout_width="120dp"
    android:layout_height="40dp"
    android:layout_toLeftOf="@+id/Moneda"
    android:gravity="left|center_vertical"
    android:text="Tipo de moneda: " />

<Button
    android:id="@+id/Moneda"
    android:layout_width="160dp"
    android:layout_height="40dp"
    android:layout_alignParentRight="true"
    android:layout_marginRight="150dp"
    android:gravity="left|center_vertical"
    android:background="@color/Blanco"
    android:textColor="@color/Negro"
    android:text="Peso argentino" />

<ImageView
    android:id="@+id/Separador"
    android:layout_width="match_parent"
    android:layout_height="2.5dp"
    android:layout_below="@+id/Moneda"
    android:layout_marginLeft="150dp"
    android:layout_marginRight="150dp"
    android:background="@color/Negro" />

<TextView
    android:id="@+id/Nombre"
    android:layout_width="wrap_content"
    android:layout_height="40dp"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/Separador"
    android:layout_marginLeft="150dp"
    android:layout_marginTop="10dp"
    android:clickable="true"
    android:gravity="left|center_vertical"
    android:onClick="changeMoneda"
    android:text="Nombre :" />

<EditText
    android:id="@+id/txtNombre"
    android:layout_width="200dp"
    android:layout_height="40dp"
    android:layout_below="@+id/Separador"
    android:layout_marginTop="10dp"
    android:layout_toRightOf="@+id/Nombre"
    android:background="@drawable/fondotxt"
    android:textColor="@color/Negro"
    android:inputType="text" />

<TextView
    android:id="@+id/lblTitulo"
    android:layout_width="360dp"
    android:layout_height="24dp"
    android:layout_below="@+id/txtNombre"
    android:layout_marginTop="10dp" />

<EditText
    android:id="@+id/txtDescripcion"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:layout_below="@+id/lblTitulo"
    android:layout_marginLeft="150dp"
    android:layout_marginRight="150dp"
    android:textColor="@color/Negro"
    android:gravity="left|center_vertical" />

<ImageView
    android:id="@+id/imgPicturefantes"
    android:layout_width="100dp"
    android:layout_height="150dp"
    android:layout_below="@+id/txtDescripcion"
    android:layout_toLeftOf="@+id/lblFotoAntes"
    android:src="@drawable/what" />

<ImageView
    android:id="@+id/imgPicturefdespues"
    android:layout_width="100dp"
    android:layout_height="150dp"
    android:layout_below="@+id/txtDescripcion"
    android:layout_marginLeft="50dp"
    android:layout_toRightOf="@+id/centerPoint"
    android:src="@drawable/what" />

<TextView
    android:id="@+id/lblFotoDespues"
    android:layout_width="120dp"
    android:layout_height="50dp"
    android:layout_below="@+id/txtDescripcion"
    android:layout_marginTop="50dp"
    android:layout_toRightOf="@+id/imgPicturefdespues"
    android:gravity="center"
    android:text="Foto despues: "
    android:textSize="18sp" />

<ImageButton
    android:id="@+id/btnDespuesF"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/btnAntesF"
    android:layout_toRightOf="@+id/imgPicturefdespues"
    android:background="@drawable/btnupload" />

<TextView
    android:id="@+id/centerPoint"
    android:layout_width="2dp"
    android:layout_height="2dp"
    android:layout_below="@+id/txtDescripcion"
    android:layout_centerHorizontal="true" />

<TextView
    android:id="@+id/lblFotoAntes"
    android:layout_width="100dp"
    android:layout_height="50dp"
    android:layout_below="@+id/txtDescripcion"
    android:layout_marginRight="50dp"
    android:layout_marginTop="50dp"
    android:layout_toLeftOf="@+id/centerPoint"
    android:gravity="center"
    android:text="Foto antes: "
    android:textSize="18sp" />

<ImageButton
    android:id="@+id/btnAntesF"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/lblFotoAntes"
    android:layout_marginRight="75dp"
    android:layout_toLeftOf="@+id/centerPoint"
    android:background="@drawable/btnupload" />

Repartee answered 21/11, 2013 at 21:32 Comment(1)
Look here https://mcmap.net/q/666250/-button-clicklistener-inside-listviewAmphibolous
L
33

You can do so in the getView() method of your adapter. For that you will need to use a custom adapter (if you are not doing that already). It will be better if you can show the relevant portions of your code.

EDIT: The dialog will be shown by your activity. So you can create an interface for listening to this button click event.

public interface BtnClickListener {
    public abstract void onBtnClick(int position);
}

Let your custom adapter receive it as input.

private BtnClickListener mClickListener = null;
public PerfilAdapter(Context context, List<PerfilBean> lista, BtnClickListener listener) {
    mContext = context;
    mLayoutInflater = (LayoutInflater) mContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    listaPerfiles=lista;
    mClickListener = listener;
}

Now you can simply set the normal onClickListener in getView() as below

Button moneda = (Button) itemView.findViewById(R.id.Moneda);
moneda.setTag(position); //For passing the list item index
moneda.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if(mClickListener != null)
            mClickListener.onBtnClick((Integer) v.getTag());                
    }
});

Let your activity pass the required BtnClickListener as part of adapter creation.

perfiladapter = new PerfilAdapter(getApplicationContext(), lst, new BtnClickListener() {

    @Override
    public void onBtnClick(int position) {
        // TODO Auto-generated method stub
        // Call your function which creates and shows the dialog here
        changeMoneda(position);
    }

});

Assuming that lst.get(position).setPerfil_tipoMoneda(item); changes the text which will be used as button text correctly, you should simply call perfiladapter.notifyDataSetChanged() in the onClick of your dialog (Currently you are creating the adapter again which is not required).

public void onClick(DialogInterface dialog, int item) {
    lst.get(position).setPerfil_tipoMoneda(item);
    perfiladapter.notifyDataSetChanged();
    dialog.dismiss();
}

Hope it will work as you expect it to.

Lynellelynett answered 21/11, 2013 at 21:41 Comment(3)
i have an custom adapter but i need the adapter to have the Button Moneda have to appear a alerdialog to show option after the user select the label of the button have to change that depends of the selectionRepartee
Thanks a lot man... You have explained it very well. I was also stuck with the similar issue, but you saved me..Unset
How do you call notifyDataSetChanged from The MainActivity ?Dejong
K
2

In my case i had to add this attribute in the listView :

<ListView
...
android:clickable="true"
...
</ListView>

And in the adapter just add on click listener in the button view.

wrapper.getButtonHi().setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            DebugUtils.logDebug("Clickeado :: "+ mContact.getUserId());
        }
});

Its important to set final the variables:

public View getRowView(final int position, View convertView, ViewGroup parent) {
    final BrowseContactItemWrapper wrapper;
    final UserModel mContact = lstContact.get(position);
    .....
}
Kelcey answered 30/9, 2014 at 17:19 Comment(0)
D
1

Just a small tweak to refresh the renderer from outside.

final FinalMenuListAdapter adapter = this;

viewHolder.deleteItem.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
       if(mClickListener != null)
         mClickListener.onBtnClick((MenuItemObject)v.getTag(),adapter);
   }
 });
 final MenuItemObject menuItemObject = getItem(position);

 viewHolder.deleteItem.setTag(menuItemObject);
Dejong answered 14/11, 2016 at 14:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.