gpt4 book ai didi

java - 列表中按钮的 onclicklistener

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:43:08 27 4
gpt4 key购买 nike

我在 android studio 中有一个带有自定义 ListView 的项目。我的自定义 ListView 包含 2textbox 和一个开关和 imagebutton 但我无法找到如何在 listadapter 类中为它们设置 onclicklistener 或其他监听器

这是我的列表适配器:

public class listadapter extends ArrayAdapter {
Context context_;
int resource_;
ArrayList<reminders> objects_;
boolean bool;
DBAdapter db;

public listadapter(Context context, int resource, ArrayList<reminders> objects) {
super(context, resource, objects);
context_ = context;
resource_ = resource;
objects_ = objects;
}

public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context_.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View ViewRow = inflater.inflate(R.layout.reminders_list_layout, parent, false);
TextView nametxt, addresstxt;
Switch sw;
ImageView imgview;
nametxt = (TextView) ViewRow.findViewById(R.id.remindername);
addresstxt = (TextView) ViewRow.findViewById(R.id.reminderaddress);
sw = (Switch) ViewRow.findViewById(R.id.remindersw);
imgview = (ImageView) ViewRow.findViewById(R.id.imageView4);
nametxt.setText(objects_.get(position).name);
addresstxt.setText(objects_.get(position).address);
bool = (objects_.get(position).swbool != 0);
sw.setChecked(bool);
imgview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View parentRow = (View) v.getParent();
ListView listView = (ListView) parentRow.getParent();
final int position = listView.getPositionForView(parentRow);
Toast.makeText(context_,position+"",Toast.LENGTH_LONG).show();
}
});
return ViewRow;
}

但是当我测试我的项目并单击 imageview 时,它会强制关闭并且 logcat 中出现此错误

12-27 20:58:50.343 5658-5658/com.amir_p.yadambendaz E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.amir_p.yadambendaz, PID: 5658
java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.ListView at com.amir_p.yadambendaz.listadapter$1.onClick(listadapter.java:49)

我能做什么?

最佳答案

尝试改变

public View getView(int position, View convertView, ViewGroup parent) {
...
@Override
public void onClick(View v) {
View parentRow = (View) v.getParent();
ListView listView = (ListView) parentRow.getParent();
...
}

public View getView(int position, View convertView, final ViewGroup parent) {
...
@Override
public void onClick(View v) {
ListView listView = (ListView) parent;
...
}

关于java - 列表中按钮的 onclicklistener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34482952/

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