我怎样在一个onclicklistener添加到ListView适配器内部的一个按钮? [英] How do I add an onclicklistener to a button inside a listview adapter?

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

问题描述

我有一个包含我所有用户的列表列表视图。从列表中每个项目的是,有一个按钮,以显示一个AlertDialog来改变按钮的标签的值的布局。我怎样才能动态地click事件添加到由ListView控件适配器生成按钮?

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()办法做到这一点。对于您将需要使用自定义适配器(如果你不这样做的话)。这将是更好,如果你能证明你的code的相关部分。

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

现在你可以简单地设置的正常 onClickListener getView()如下

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

让您的活动传递所需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(位置).setPerfil_tipoMoneda(项目); 的变化将被正确使用的按钮文本的文本,你应该简单地调用 perfiladapter.notifyDataSetChanged()的onClick 你的对话框(目前你是创建再次适配器它不是必需的)。

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.

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

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