The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (new DialogInterface.OnClickListener(){})(View 类型中的方法 setOnClickListener(View.OnClickListener) 不适用于参数(新 DialogInterface.OnClickListener(){}))
问题描述
尝试将 onClickListener 添加到我的 listView 中的项目,但我收到一条错误消息:View 类型中的方法 setOnClickListener(View.OnClickListener) 不适用于参数(新 DialogInterface.OnClickListener(){})"
就行了:
Attempting to add an onClickListener to items in my listView and I'm getting an error stating: "The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (new DialogInterface.OnClickListener(){})"
on the line:
holder.imageView.setOnClickListener(new OnClickListener() {
本文作者提到以下几点:
The author of this article mentioned the following:
In your custom adapter class, you can try this code inside getView() method
[java]holder.imageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "Clicked on image", Toast.LENGTH_LONG).show();
}
});[/java]
似乎我可能需要修改我当前的实现 - 我只是不确定具体如何.
It seems as if I might need to modify my current implementation - I'm just not sure exactly how.
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
LayoutInflater inflater = context.getLayoutInflater();
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item2, null);
holder = new ViewHolder();
holder.txtSuccess = (TextView) convertView
.findViewById(R.id.success);
holder.txtCmd = (TextView) convertView.findViewById(R.id.cmd);
holder.txtPrice = (TextView) convertView.findViewById(R.id.price);
holder.imageView = (ImageView) convertView
.findViewById(R.id.thumbnail);
convertView.setTag(holder);
holder.imageView.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Toast.makeText(context, "Clicked on image",
Toast.LENGTH_LONG).show();
}
推荐答案
你可能有错误的 import
.检查您是否导入了 DialogInterface.OnClickListener
.您仍然可以通过这种方式显式强制参数:
you probably have the wrong import
. Check if you imported DialogInterface.OnClickListener
. Still you can explicitly force the parameter this way:
holder.imageView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(context, "Clicked on image",
Toast.LENGTH_LONG).show();
}
这篇关于View 类型中的方法 setOnClickListener(View.OnClickListener) 不适用于参数(新 DialogInterface.OnClickListener(){})的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:View 类型中的方法 setOnClickListener(View.OnClickListen


基础教程推荐
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 降序排序:Java Map 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01