如何onclicklistener添加到一个按钮,一个ListView适配器内部适配器、按钮、onclicklistener、ListView

2023-09-04 11:27:39 作者:浅瞳

我的意思是我有一个包含所有的用户。每天从列表项列表中的列表视图是一个布局有一个按钮,显示一个AlertDialog来改变button.How的标签的值可以dinamically加上单击事件到按钮都是由ListView控件适配器生成。

i mean i have a listview that contain the list of all my users .every item from the list is a layout that have a button to show a AlertDialog to change the value of the label of the button.How can dinamically add on click event to that button that are generate by 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();
    }}

这是我的活动

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 are currently user

<?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.