java - 无法在我的列表视图中添加删除功能
问题描述
所以我想添加一个选项来删除列表中的特定条目,所以我在列表的适配器中添加了一个图像视图。但是由于某种原因,当我现在尝试在我的应用程序中创建列表时,它崩溃了。这是错误行:
java.lang.ClassCastException: android.widget.LinearLayout 不能转换为 android.widget.ImageView
这是我的列表适配器的代码:
package com.example.taskmasterv3;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.ArrayList;
public class SubtaskAdapter extends ArrayAdapter<subtask> {
private final Context context;
private ArrayList<subtask> values;
public SubtaskAdapter(Context context, ArrayList<subtask> list) {
//since your are using custom view,pass zero and inflate the custom view by overriding getview
super(context, 0 , list);
this.context = context;
this.values = list;
}
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
//check if its null, if so inflate it, else simply reuse it
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.subtask_item, parent, false);
}
//use convertView to refer the childviews to populate it with data
TextView tvSubtaskName = convertView.findViewById(R.id.tvSubtaskName);
ImageView ivPri = convertView.findViewById(R.id.ivPri);
ImageView ivTime = convertView.findViewById(R.id.ivTime);
ImageView ivDelete = convertView.findViewById(R.id.ivDelete);
tvSubtaskName.setText(values.get(position).getSubtaskName());
if (values.get(position).isPriHigh()) {
ivPri.setImageResource(R.drawable.priority_high);
} else if (values.get(position).isPriMed()) {
ivPri.setImageResource(R.drawable.priority_med);
} else if (values.get(position).isPriLow()) {
ivPri.setImageResource(R.drawable.priority_low);
}
if (values.get(position).isTimeMore()) {
ivTime.setImageResource(R.drawable.time_symbol_more);
} else if (values.get(position).isTimeMed()) {
ivTime.setImageResource(R.drawable.time_symbol_med);
} else if (values.get(position).isTimeLess()) {
ivTime.setImageResource(R.drawable.time_symbol_less);
}
// Delete button for subtasks
ivDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
values.remove(position);
notifyDataSetChanged();
}
});
//return the view you inflated
return convertView;
}
//to keep adding the new subtasks try the following
public void addANewSubTask(subtask newSubTask){
ArrayList<subtask> newvalues = new ArrayList<>(this.values);
newvalues.add(newSubTask);
this.values = newvalues;
notifyDataSetChanged();
}
}
这是列表项的 xml 代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/ivDelete"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_margin="16dp"
android:background="@color/white"
android:clickable="true"
android:orientation="horizontal"
>
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:clickable="true"
app:srcCompat="@drawable/delete" />
<TextView
android:id="@+id/tvSubtaskName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="3"
android:fontFamily="@font/roboto"
android:gravity="center_horizontal"
android:text="subtask_name"
android:textColor="@color/black"
android:textSize="15sp" />
<ImageView
android:id="@+id/ivPri"
android:layout_width="0dp"
android:layout_height="32dp"
android:layout_gravity="center"
android:layout_weight="1"
app:srcCompat="@drawable/priority_high" />
<ImageView
android:id="@+id/ivTime"
android:layout_width="0dp"
android:layout_height="32dp"
android:layout_gravity="center"
android:layout_weight="1"
app:srcCompat="@drawable/time_symbol_more" />
</LinearLayout>
解决方案
您正在尝试将 a 引用LinearLayout
为ImageView
.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/ivDelete"
...
ImageView ivDelete = convertView.findViewById(R.id.ivDelete);
你看到这里的id了吗?
只需将最后一行替换为以下内容:
LinearLayout ivDelete = convertView.findViewById(R.id.ivDelete);
推荐阅读
- python - SyntaxError:(unicode 错误)“unicodeescape”编解码器无法使用 Selenium Python 解码位置字节
- python - 如何查看约束 IIS gurobi
- javascript - 为什么我会收到“ReferenceError:未定义测试”
- javascript - 验证函数的返回应该如何中断或继续另一个函数?
- sql - 检查字符串的前半部分是否为字符
- javascript - jQuery focusin / focusout - 以编程方式更新页面时触发 - 如何检测 *only* 人类关注的时间?
- wordpress - 适用于 SFTP 的 Amazon AWS EC2 Openlitespeed 权限
- math - 我们如何正确判断两个浮点变量是否相等?
- azure-devops - Azure DevOps:管道构建中的 Blazor 部署失败
- next.js - 使用 Next.js 进行机车滚动