我尝试使用recyclerview
填充loader
,但适配器只会绑定数据库中的前五项,然后对数据库中的每个其他项重复。
为清楚起见,它看起来像这样:
第1项
第2项
第3项
第4项
第5项
第1项
第2项
...
项目数仍然与数据库中的项目数相匹配。我还测试了光标并正确打印了每个项目,所以我假设问题是适配器。这就是我正在使用的:
class LibraryAdapter extends RecyclerView.Adapter<LibraryAdapter.LibraryViewHolder> {
private Context mContext;
private Cursor mCursor;
LibraryAdapter(Context context, Cursor cursor){
this.mContext = context;
this.mCursor = cursor;
setHasStableIds(true);
}
static class LibraryViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private TextView titleText;
private TextView numText;
LibraryViewHolder(View itemView) {
super(itemView);
titleText = (TextView) itemView.findViewById(R.id.titleText);
numText = (TextView) itemView.findViewById(R.id.numText);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
....
}
}
@Override
public LibraryAdapter.LibraryViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
return new LibraryViewHolder(view);
}
@Override
public void onBindViewHolder(LibraryAdapter.LibraryViewHolder holder, int position) {
LibraryModel item = getData(position);
holder.titleText.setText(item.getTitle());
holder.numText.setText(item.getNum() + " items");
}
@Override
public int getItemCount() {
return (mCursor != null) ? mCursor.getCount() : 0;
}
private Cursor swapCursor(Cursor cursor){
if(mCursor == cursor){
return null;
}
Cursor oldCursor = mCursor;
this.mCursor = cursor;
if(cursor != null){
this.notifyDataSetChanged();
}
return oldCursor;
}
void changeCursor(Cursor cursor){
Cursor oldCursor = swapCursor(cursor);
if (oldCursor != null){
oldCursor.close();
}
}
private LibraryModel getData(int position){
mCursor.moveToPosition(position);
String title = mCursor.getString(mCursor.getColumnIndex(DatabaseContract.LibraryEntry.COLUMN_TITLE));
int num = mCursor.getInt(mCursor.getColumnIndex(DatabaseContract.LibraryEntry.COLUMN_NUMBER));
LibraryModel item = new LibraryModel();
item.setTitle(title);
item.setNum(num);
return item;
}
}
在片段上:
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_alltracks, container, false);
libraryRecyclerview = (RecyclerView) root.findViewById(R.id.list);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getContext());
libraryRecyclerview.setLayoutManager(mLayoutManager);
mAdapter = new LibraryAdapter(getContext(), null, allTracks);
libraryRecyclerview.setAdapter(mAdapter);
return root;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getLoaderManager().initLoader(1, null, this);
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
Uri uri = DatabaseContract.LibraryEntry.CONTENT_URI;
String[] projection = {
DatabaseContract.LibraryEntry.TABLE_NAME + "." + DatabaseContract.LibraryEntry._ID,
DatabaseContract.LibraryEntry.COLUMN_TITLE,
DatabaseContract.LibraryEntry.COLUMN_NUM
};
return new CursorLoader(getContext(), uri, projection, null, null, null);
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
mAdapter.changeCursor(data);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
mAdapter.changeCursor(null);
}
答案 0 :(得分:1)
对于任何仍然绊倒这个问题的人:
我遇到了同样的问题,在回收站视图中重复了 5 个相同的条目。
解决我的问题的只是删除
adapter.setHasStableIds(true)
当然,这让我解决了其他问题,但我设法用这个显示了我的所有条目。