gpt4 book ai didi

Android:使用PagedList时如何添加项目和刷新列表?

转载 作者:太空狗 更新时间:2023-10-29 14:37:58 28 4
gpt4 key购买 nike

我已经使用 PagedList 加载项目页面并将它们显示在 RecyclerView 中,它成功显示了数字列表(0,1,2,3....,99),列表总是可以向上滚动下来没有任何问题。当我尝试单击一个按钮来调用 appendItems() 以附加新数据时,它仍然可以。但是,当我滚动列表时应用程序崩溃了。

java.util.ConcurrentModificationException
at java.util.AbstractList$SubAbstractList.size(AbstractList.java:360)
at androidx.paging.PagedStorage.get(PagedStorage.java:153)
at androidx.paging.PagedList.get(PagedList.java:384)
at androidx.paging.AsyncPagedListDiffer.getItem(AsyncPagedListDiffer.java:206)
at androidx.paging.PagedListAdapter.getItem(PagedListAdapter.java:156)
at com.mysample.pagedlist.MyPagedListAdapter.onBindViewHolder(MyPagedListAdapter.java:38)
at com.mysample.pagedlist.MyPagedListAdapter.onBindViewHolder(MyPagedListAdapter.java:19)

看来我不知道附加更多数据和刷新显示的正确方法。谁能告诉我如何解决?

Api作为数据库和dao

public class Api {
private static List<String> list = null;

private static void initListIfNeeded() {
if (list == null) {
list = new ArrayList<>();
for (int i = 0; i < 100; i++) {
list.add("" + i);
}
}
}

public static List<String> pageItems(int page, int pageSize) {
initListIfNeeded();
int start = page * pageSize;
int end = (page + 1) * pageSize;
if (end > list.size()) {
end = list.size();
}
if (page < 0 || start >= list.size()) {
return new ArrayList<>();
} else {
return list.subList(start, end);
}
}

public static void appendItems() {
initListIfNeeded();
int size = list.size();
for (int i = 0; i < 10; i++) {
list.add("" + (size + i));
}
}
}

DataSource 用于处理数据的分页

public class MyDataSource extends PageKeyedDataSource<Integer, String> {

public MyDataSource() {
}

@Override
public void loadInitial(@NonNull LoadInitialParams<Integer> params, @NonNull LoadInitialCallback<Integer, String> callback) {
callback.onResult(Api.pageItems(0, params.requestedLoadSize), -1, 1);
}

@Override
public void loadBefore(@NonNull LoadParams<Integer> params, @NonNull LoadCallback<Integer, String> callback) {
callback.onResult(Api.pageItems(params.key, params.requestedLoadSize), params.key - 1);
}

@Override
public void loadAfter(@NonNull LoadParams<Integer> params, @NonNull LoadCallback<Integer, String> callback) {
callback.onResult(Api.pageItems(params.key, params.requestedLoadSize), params.key + 1);
}

}

对应的ViewModel类

public class MyViewModel extends ViewModel {

public LiveData<PagedList<String>> list;

public MyViewModel() {
list = new LivePagedListBuilder<>(new DataSource.Factory<Integer, String>() {
@Override
public DataSource<Integer, String> create() {
return new MyDataSource();
}
}, new PagedList.Config.Builder().setPageSize(20).setInitialLoadSizeHint(20).build()).build();
}

public void appendItems() {
Api.appendItems();
}
}

Activity

public class PagedListActivity extends AppCompatActivity {

private MyPagedListAdapter adapter;
private MyViewModel viewModel;
private RecyclerView recyclerView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_paged_list);
adapter = new MyPagedListAdapter(LayoutInflater.from(this));

recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
viewModel = ViewModelProviders.of(this).get(MyViewModel.class);
viewModel.list.observe(this, new Observer<PagedList<String>>() {
@DebugLog
@Override
public void onChanged(PagedList<String> strings) {
adapter.submitList(strings);
}
});
}

@DebugLog
public void onClick(View view) {
viewModel.appendItems();
}
}

适配器

public class MyPagedListAdapter extends PagedListAdapter<String, MyPagedListAdapter.VH> {

private LayoutInflater inflater;

protected MyPagedListAdapter(LayoutInflater inflater) {
super(DIFF_CALLBACK);
this.inflater = inflater;
}

@NonNull
@Override
public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.item_text, parent, false);
return new VH(view);
}

@Override
public void onBindViewHolder(@NonNull VH holder, int position) {
holder.setData(getItem(position));
}

public class VH extends RecyclerView.ViewHolder {

private final TextView txt;

public VH(@NonNull View itemView) {
super(itemView);
txt = itemView.findViewById(R.id.txt);
}

public void setData(String text) {
txt.setText(text);
}
}

private static DiffUtil.ItemCallback<String> DIFF_CALLBACK = new DiffUtil.ItemCallback<String>() {
@Override
public boolean areItemsTheSame(@NonNull String oldItem, @NonNull String newItem) {
return oldItem.equalsIgnoreCase(newItem);
}

@Override
public boolean areContentsTheSame(@NonNull String oldItem, @NonNull String newItem) {
return oldItem.equalsIgnoreCase(newItem);
}
};

}

最佳答案

来自这个堆栈跟踪:

java.util.ConcurrentModificationException
at java.util.AbstractList$SubAbstractList.size(AbstractList.java:360)

我们可以说

a) 当有人试图在列表迭代过程中修改列表时,应用程序崩溃
b) 这发生在 SubAbstractList 实例上(当您调用 list.subList(start, end);

应用程序崩溃,因为在修改原始列表之前创建的子列表不再有效,在您修改(将项目附加到)原始列表之后

而不是返回 list.subList(start, end);来自 Api#pageItems试试
返回 new ArrayList<>(list.subList(start, end));

要让 RecyclerView 显示您的更改,您应该调用 Adapter#notifyItemRangeInserted或 RW 适配器

关于Android:使用PagedList时如何添加项目和刷新列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52891690/

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