gpt4 book ai didi

java - 无法清除数据或将数据添加到列表 - UnsupportedOperationException

转载 作者:行者123 更新时间:2023-12-02 10:40:46 27 4
gpt4 key购买 nike

我一直在开发搜索功能,该功能使我能够在 RecyclerView 中搜索数据。当我单击搜索图标时,我的应用程序崩溃了。经过一些调试后,我发现了问题,似乎无法清除信息或将信息添加到新列表。

AdapterClass.java

public class JobAdapter extends RecyclerView.Adapter<JobAdapter.ViewHolder> implements Filterable {

private OnJobClickListener mListener;
private List<Job> jobs;
private List<Job> jobsListFiltered;

private static final String LOG_TAG = JobAdapter.class.getName();


public interface OnJobClickListener {
void onJobClick(Job job);
}

public void setOnItemClickListener(OnJobClickListener listener) {
mListener = listener;
}

// Provide a reference to the views for each data item
// Provide access to all the views for a data item in a view holder
public class ViewHolder extends RecyclerView.ViewHolder {

public TextView jobTitle;
public TextView companyName;
public TextView datePosted;
public ImageView companyLogo;
public View layout;


public ViewHolder(View itemView) {
super(itemView);
layout = itemView;
jobTitle = layout.findViewById(R.id.textView_job_title);
companyName = layout.findViewById(R.id.textView_company_name);
datePosted = layout.findViewById(R.id.textView_date);
companyLogo = layout.findViewById(R.id.imageViewLogo);

itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mListener != null) {
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION) {
Job currentJob = jobs.get(position);
mListener.onJobClick(currentJob);
}
}
}
});
}
}


// Job constructor
public JobAdapter(List<Job> job, OnJobClickListener listener) {
jobs = job.subList(1, job.size());
this.mListener = listener;
jobsListFiltered = new ArrayList<>(job);
}

// Create new views (invoked by the layout manager)
@Override
public JobAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// create a new view
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View v = inflater.inflate(R.layout.job_item_row, parent, false);

// set the view's size, margins, paddings and layout parameters
ViewHolder vh = new ViewHolder(v);
return vh;
}

// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
// Get element from the dataset at this position
// Replace the contents of the view with that element
Job currentJob = jobs.get(position);

String mJobTitle = currentJob.getJobTitle();
holder.jobTitle.setText(mJobTitle);

String mCompanyName = currentJob.getCompanyName();
holder.companyName.setText(mCompanyName);

Context context = holder.datePosted.getContext();

Date mDatePosted = currentJob.getDatePosted();
String dateFormat = formatDayMonth(context, mDatePosted);
holder.datePosted.getContext();
holder.datePosted.setText(dateFormat);

String mCompanyLogo = currentJob.getCompanyLogo();
RequestOptions requestOptions = new RequestOptions()
.placeholder(R.drawable.ic_launcher_foreground)
.circleCrop();
Glide.with(context)
.load(mCompanyLogo)
.apply(requestOptions)
.into(holder.companyLogo);
}

@Nullable
public static String formatDayMonth (@NonNull Context context, @Nullable Date date){
if (date == null) {
return null;
}

SimpleDateFormat sdf = new SimpleDateFormat(
context.getString(R.string.format_date),
Locale.US);
return sdf.format(date);
}

public int getItemCount () {
return (jobs != null) ? jobs.size() : 0;
}

@Override
public Filter getFilter() {
return jobFilter;
}

private Filter jobFilter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
List<Job> filteredList = new LinkedList<>();

if (constraint == null || constraint.length() == 0) {
filteredList.addAll(jobsListFiltered);
} else {
String filterPattern = constraint.toString().toLowerCase().trim();

for (Job job : jobsListFiltered) {
if (job.getJobTitle().toLowerCase().contains(filterPattern)) {
filteredList.add(job);
}
}
}

FilterResults results = new FilterResults();
results.values = filteredList;

return results;
}

@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
jobs.clear();
jobs.addAll((LinkedList)results.values);
notifyDataSetChanged();
}
};

}

错误:

E/AndroidRuntime:致命异常:main
进程:包名,PID:17390
java.lang.UnsupportedOperationException
在 java.util.AbstractList.remove(AbstractList.java:161)
在 java.util.AbstractList$Itr.remove(AbstractList.java:374)
在 java.util.AbstractList.removeRange(AbstractList.java:571)
在 java.util.SubList.removeRange(AbstractList.java:668)
在 java.util.AbstractList.clear(AbstractList.java:234)
在包名.JobAdapter$1.publishResults(JobAdapter.java:178)
在 android.widget.Filter$ResultsHandler.handleMessage(Filter.java:282)
在 android.os.Handler.dispatchMessage(Handler.java:106)
在 android.os.Looper.loop(Looper.java:164)
在 android.app.ActivityThread.main(ActivityThread.java:6494)
在 java.lang.reflect.Method.invoke( native 方法)
在 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:440)
在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

到目前为止,我尝试了此 thread 中的建议但他们似乎不适合我的情况。任何帮助将不胜感激;)

最佳答案

请显示调用 JobAdapter 构造函数的代码。

在 JobAdapter 构造函数中,您使用 List.subList 创建由原始列表支持的子 ListView 。因此,您的子列表将继承支持列表上的任何属性(例如不变性)。

堆栈跟踪清楚地表明您正在尝试清除一个无法清除的列表,我只能假设这是因为它是不可变的。

要解决这个问题,您可以从 JobAdapter 构造函数中的子列表创建一个新的 ArrayList,如下所示:

jobs = new ArrayList<>(job.subList(1, job.size()));

关于java - 无法清除数据或将数据添加到列表 - UnsupportedOperationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52932695/

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