如何将 OnClickListener 添加到 ListView 适配器内的按钮? [英] How do I add an OnClickListener to a button inside a ListView adapter?

查看:17
本文介绍了如何将 OnClickListener 添加到 ListView 适配器内的按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含我所有用户列表的列表视图.列表中的每一项都是一个布局,它有一个按钮来显示 AlertDialog 以更改按钮标签的值.如何向由列表视图适配器生成的按钮动态添加点击事件?

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?

这是我的适配器:

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

这是我的活动:

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" />

推荐答案

您可以在适配器的 getView() 方法中执行此操作.为此,您将需要使用自定义适配器(如果您还没有这样做).如果您可以显示代码的相关部分会更好.

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.

您的活动将显示该对话框.所以你可以创建一个接口来监听这个按钮点击事件.

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

现在你可以简单地在 getView() 中设置普通的 onClickListener 如下

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

让您的 Activity 作为适配器创建的一部分传递所需的 BtnClickListener.

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

});

假设 lst.get(position).setPerfil_tipoMoneda(item); 更改了将正确用作按钮文本的文本,您应该简单地调用 perfiladapter.notifyDataSetChanged()code> 在您的 dialogonClick 中(目前您正在重新创建不需要的适配器).

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

希望它能如你所愿.

这篇关于如何将 OnClickListener 添加到 ListView 适配器内的按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆