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

馋奶兔 提交于 2019-11-27 13:12:56

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.

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);
    .....
}

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);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!