ListView 内的 ImageButton onclick 列表器不起作用。应用点击后崩溃
Posted
技术标签:
【中文标题】ListView 内的 ImageButton onclick 列表器不起作用。应用点击后崩溃【英文标题】:ImageButton onclick listner inside ListView is not working. The App crashes after click 【发布时间】:2021-09-28 07:12:06 【问题描述】:我编写了以下代码从自定义 listView 内部进行调用,但每当我按下 Imagebutton 时,应用程序就会崩溃。我尝试了不同的文章和教程,但无法实现。有人可以看到我的代码并建议如何操作吗?
模型列表视图的 XML
<RelativeLayout
android:layout_
android:layout_
android:background="#EADEF1">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/photo"
android:layout_
android:layout_
android:layout_margin="5dp"
android:layout_gravity="center"
android:src="@drawable/logobsca">
</de.hdodenhof.circleimageview.CircleImageView>
<TextView
android:id="@+id/textName"
android:layout_
android:layout_
android:text="Name"
android:layout_toRightOf="@id/photo"
android:textSize="14sp"
android:textColor="#4527A0"
android:textStyle="bold"/>
<TextView
android:id="@+id/textDesignation"
android:layout_
android:layout_
android:layout_below="@id/textName"
android:layout_toRightOf="@id/photo"
android:text="Designation"
android:textSize="12sp"
android:textColor="#1F2020"
android:textStyle="normal"/>
<TextView
android:id="@+id/textPhone"
android:layout_below="@id/textDesignation"
android:layout_toRightOf="@id/photo"
android:layout_
android:layout_
android:text="Phone"
android:clickable="true"
android:textColor="#1F2020"
android:textSize="12sp"
android:textStyle="normal" />
<ImageButton
android:id="@+id/callButton"
android:layout_
android:layout_
android:layout_alignParentTop="true"
android:clickable="false"
android:layout_alignParentEnd="true"
android:layout_marginTop="9dp"
android:layout_marginEnd="13dp"
android:background="@drawable/roundcall"
android:src="@android:drawable/ic_menu_call" />
</RelativeLayout>
模型类活动
```public class ListMobileModel extends AppCompatActivity
String Name;
String Designation;
String Phone;
ImageButton CallButton;
int Photo;
public String getName()
return Name;
public void setName(String name)
Name = name;
public String getDesignation()
return Designation;
public void setDesignation(String designation)
Designation = designation;
public String getPhone()
return Phone;
public void setPhone(String phone)
Phone = phone;
public int getPhoto()
return Photo;
public void setPhoto(int photo)
Photo = photo;
public ListMobileModel(String name, String designation, String phone, int photo)
Name = name;
Designation = designation;
Phone = phone;
Photo = photo;
```
ListView 适配器
public class ListViewAdapter extends BaseAdapter
Activity context;
LayoutInflater inflater;
List<ListMobileModel> mobilelist;
ArrayList<ListMobileModel> arrayList;
public ListViewAdapter( Activity context, List<ListMobileModel> mobilelist)
context = context;
this.mobilelist=mobilelist;
inflater=LayoutInflater.from(context);
this.arrayList=new ArrayList<ListMobileModel>();
this.arrayList.addAll(mobilelist);
public class ViewHolder
TextView nameTV, designationTV, mobileTV;
ImageView photoTV;
ImageButton callButton;
@Override
public int getCount()
return mobilelist.size();
@Override
public Object getItem(int position)
return mobilelist.get(position);
@Override
public long getItemId(int position)
return position;
public View getView(int position, View view, ViewGroup parent)
ViewHolder holder;
if (view==null)
holder=new ViewHolder();
view=inflater.inflate(R.layout.activity_listmobile_model, null);
// locate the views in activity_listmobile_model.xml)
holder.nameTV=view.findViewById(R.id.textName);
holder.designationTV=view.findViewById(R.id.textDesignation);
holder.mobileTV=view.findViewById(R.id.textPhone);
holder.photoTV=view.findViewById(R.id.photo);
holder.callButton=view.findViewById(R.id.callButton);
view.setTag(holder);
else
holder = (ViewHolder) view.getTag();
//set the results into Textview
holder.nameTV.setText(mobilelist.get(position).getName());
holder.designationTV.setText(mobilelist.get(position).getDesignation());
holder.mobileTV.setText(mobilelist.get(position).getPhone());
holder.photoTV.setImageResource(mobilelist.get(position).getPhoto());
ImageButton callButton;
callButton=view.findViewById(R.id.callButton);
callButton.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
String cellno=mobilelist.get(position).getPhone();
Intent callIntent =new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" + cellno));
if(ActivityCompat.checkSelfPermission(context, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED)
return;
context.startActivity(callIntent);
);
return view;
//Filter
public void filter(String charText)
charText=charText.toLowerCase(Locale.getDefault());
mobilelist.clear();
if(charText.length()==0)
mobilelist.addAll(arrayList);
else
for(ListMobileModel row:arrayList)
if (row.getName().toLowerCase(Locale.getDefault()).contains(charText)|| (row.getDesignation().toLowerCase(Locale.getDefault()).contains(charText)))
mobilelist.add(row);
notifyDataSetChanged();
```
列表视图活动
```public class ListViewAdapter extends BaseAdapter
Activity context;
LayoutInflater inflater;
List<ListMobileModel> mobilelist;
ArrayList<ListMobileModel> arrayList;
public ListViewAdapter( Activity context, List<ListMobileModel> mobilelist)
context = context;
this.mobilelist=mobilelist;
inflater=LayoutInflater.from(context);
this.arrayList=new ArrayList<ListMobileModel>();
this.arrayList.addAll(mobilelist);
public class ViewHolder
TextView nameTV, designationTV, mobileTV;
ImageView photoTV;
ImageButton callButton;
@Override
public int getCount()
return mobilelist.size();
@Override
public Object getItem(int position)
return mobilelist.get(position);
@Override
public long getItemId(int position)
return position;
public View getView(int position, View view, ViewGroup parent)
ViewHolder holder;
if (view==null)
holder=new ViewHolder();
view=inflater.inflate(R.layout.activity_listmobile_model, null);
// locate the views in activity_listmobile_model.xml)
holder.nameTV=view.findViewById(R.id.textName);
holder.designationTV=view.findViewById(R.id.textDesignation);
holder.mobileTV=view.findViewById(R.id.textPhone);
holder.photoTV=view.findViewById(R.id.photo);
holder.callButton=view.findViewById(R.id.callButton);
view.setTag(holder);
else
holder = (ViewHolder) view.getTag();
//set the results into Textview
holder.nameTV.setText(mobilelist.get(position).getName());
holder.designationTV.setText(mobilelist.get(position).getDesignation());
holder.mobileTV.setText(mobilelist.get(position).getPhone());
holder.photoTV.setImageResource(mobilelist.get(position).getPhoto());
ImageButton callButton;
callButton=view.findViewById(R.id.callButton);
callButton.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
String cellno=mobilelist.get(position).getPhone();
Intent callIntent =new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" + cellno));
if(ActivityCompat.checkSelfPermission(context, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED)
return;
context.startActivity(callIntent);
);
return view;
//Filter
public void filter(String charText)
charText=charText.toLowerCase(Locale.getDefault());
mobilelist.clear();
if(charText.length()==0)
mobilelist.addAll(arrayList);
else
for(ListMobileModel row:arrayList)
if (row.getName().toLowerCase(Locale.getDefault()).contains(charText)|| (row.getDesignation().toLowerCase(Locale.getDefault()).contains(charText)))
mobilelist.add(row);
notifyDataSetChanged();
``` enter image description here``
【问题讨论】:
【参考方案1】:使用RecyclerView
代替ListView
RecyclerView
的Adapter和ListView
的Adapter也是一样的。
Click在这里了解更多详情。
【讨论】:
感谢哈桑先生的意见。我的问题是 ImageButton 单击而不是 ItemClick 侦听器。我的 ItemClick 监听器工作正常。以上是关于ListView 内的 ImageButton onclick 列表器不起作用。应用点击后崩溃的主要内容,如果未能解决你的问题,请参考以下文章
Android 在listview里itme中有imagebutton 怎么监听这个button?
ListView 不显示带有 BaseAdapter 的 ImageButton