gpt4 book ai didi

java - 如何知道在哪个项目中单击了按钮

转载 作者:行者123 更新时间:2023-12-01 22:20:54 25 4
gpt4 key购买 nike

到目前为止,我有一个带有自定义适配器的 ListView ,列表中的每个项目都有一个按钮。我真的很困惑;我正在尝试执行以下操作:

当用户单击列表中项目中的按钮(删除按钮)时,我想知道单击了哪个项目按钮,以便我知道要删除哪个项目 - 我该如何实现?我看过一些关于设置标签的内容,但我仍然很迷茫。

我还尝试从我的主要 Activity 的列表布局中访问该按钮,但无法引用它。

请给我详细说明如何做我想做的事情,谢谢。

添加适配器代码:

public class LocationAdapter extends BaseAdapter{
String [] n;
Context context;
String[] a;

private static LayoutInflater inflater=null;
public LocationAdapter(MainActivity mainActivity, String[] names, String[] addresses) {
// TODO Auto-generated constructor stub
n=names;
context=mainActivity;
a=addresses;
inflater = ( LayoutInflater )context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return n.length;
}

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

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

public class Holder
{
TextView name;
TextView address;
ImageButton ib;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Holder holder=new Holder();
View rowView;
rowView = inflater.inflate(R.layout.rowlayout2, null);
holder.name =(TextView) rowView.findViewById(R.id.EditTextName);
holder.address =(TextView) rowView.findViewById(R.id.EditTextAddress);
holder.ib = (ImageButton) rowView.findViewById(R.id.Delete);
holder.name.setText(n[position]);
holder.address.setText(a[position]);
holder.ib.setTag(convertView);
rowView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(context, "You Clicked "+n[position], Toast.LENGTH_LONG).show();
}

});
return rowView;
}

}

最佳答案

为您的删除按钮实现一个OnClickListener

当点击delete按钮时,删除数据源中position的行,然后调用notifyDataSetChanged()

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Holder holder=new Holder();
View rowView;
rowView = inflater.inflate(R.layout.rowlayout2, null);
holder.name =(TextView) rowView.findViewById(R.id.EditTextName);
holder.address =(TextView) rowView.findViewById(R.id.EditTextAddress);
holder.ib = (ImageButton) rowView.findViewById(R.id.Delete);
holder.name.setText(n[position]);
holder.address.setText(a[position]);
holder.ib.setTag(convertView);

//Add this for on-click of delete button
holder.ib.setOnClickListener(new OnClickListener(){
//Delete the row in your data source specified at position

});

rowView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(context, "You Clicked "+n[position], Toast.LENGTH_LONG).show();
}

});
return rowView;
}

关于java - 如何知道在哪个项目中单击了按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29813126/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com