Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/179.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Recyclerview仅显示光标中的前五项_Android_Android Recyclerview_Loader - Fatal编程技术网

Android Recyclerview仅显示光标中的前五项

Android Recyclerview仅显示光标中的前五项,android,android-recyclerview,loader,Android,Android Recyclerview,Loader,我试图用加载程序填充回收视图,但适配器将只绑定数据库中的前五项,然后对数据库中的其他每一项重复 为了清楚起见,它看起来是这样的: 项目1 项目2 项目3 项目4 项目5 项目1 项目2 但项目数仍然与数据库中的项目数匹配。我还测试了游标,它正确地打印了每个项目,所以我假设问题出在适配器上。这就是我正在使用的: class LibraryAdapter extends RecyclerView.Adapter<LibraryAdapter.LibraryViewHolder> {

我试图用
加载程序
填充
回收视图
,但适配器将只绑定数据库中的前五项,然后对数据库中的其他每一项重复

为了清楚起见,它看起来是这样的:

项目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;
}
}
class LibraryAdapter扩展了RecyclerView.Adapter{
私有上下文;
私有游标mCursor;
LibraryAdapter(上下文、光标){
this.mContext=上下文;
this.mCursor=游标;
setHasStableIds(true);
}
静态类LibraryViewHolder扩展了RecyclerView.ViewHolder实现了View.OnClickListener{
私有文本视图标题文本;
私有文本视图numText;
LibraryViewHolder(查看项目视图){
超级(项目视图);
titleText=(TextView)itemView.findViewById(R.id.titleText);
numText=(TextView)itemView.findViewById(R.id.numText);
setOnClickListener(这个);
}
@凌驾
公共void onClick(视图v){
....
}
}
@凌驾
public LibraryAdapter.LibraryViewHolder onCreateViewHolder(视图组父级,int-viewType){
View=LayoutFlater.from(parent.getContext()).flate(R.layout.list_项,parent,false);
返回新的LibraryViewHolder(视图);
}
@凌驾
BindViewHolder上的公共无效(LibraryAdapter.LibraryViewHolder,内部位置){
LibraryModel项目=获取数据(位置);
holder.titleText.setText(item.getTitle());
holder.numText.setText(item.getNum()+“items”);
}
@凌驾
public int getItemCount(){
返回(mCursor!=null)?mCursor.getCount():0;
}
专用游标交换游标(游标游标){
if(mCursor==光标){
返回null;
}
游标oldCursor=mCursor;
this.mCursor=游标;
如果(光标!=null){
this.notifyDataSetChanged();
}
返回光标;
}
void changeCursor(光标){
游标oldCursor=swapCursor(游标);
if(oldCursor!=null){
oldCursor.close();
}
}
私人图书馆模型getData(内部位置){
移动位置(位置);
String title=mCursor.getString(mCursor.getColumnIndex(DatabaseContract.LibraryEntry.COLUMN_title));
int num=mCursor.getInt(mCursor.getColumnIndex(DatabaseContract.LibraryEntry.COLUMN_NUMBER));
LibraryModel项=新的LibraryModel();
项目.设置标题(标题);
项目.setNum(num);
退货项目;
}
}
在片段上:

@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);
}
@Nullable
@凌驾
创建视图时的公共视图(LayoutFlater充气机、@Nullable ViewGroup容器、@Nullable Bundle savedInstanceState){
视图根=充气机。充气(R.layout.fragment\u所有轨道,容器,假);
libraryRecyclerview=(RecyclerView)root.findViewById(R.id.list);
RecyclerView.LayoutManager mLayoutManager=新的LinearLayoutManager(getContext());
libraryRecyclerview.setLayoutManager(MLLayoutManager);
mAdapter=newlibraryadapter(getContext(),null,allTracks);
libraryRecyclerview.setAdapter(mAdapter);
返回根;
}
@凌驾
ActivityCreated上的公共无效(@Nullable Bundle savedinStateCState){
super.onActivityCreated(savedInstanceState);
getLoaderManager().initLoader(1,null,this);
}
@凌驾
公共加载器onCreateLoader(int-id,Bundle-args){
Uri=DatabaseContract.LibraryEntry.CONTENT\u Uri;
字符串[]投影={
DatabaseContract.LibraryEntry.TABLE_NAME+““+DatabaseContract.LibraryEntry.\u ID,
DatabaseContract.LibraryEntry.COLUMN_标题,
DatabaseContract.LibraryEntry.COLUMN_NUM
};
返回新的游标装入器(getContext(),uri,projection,null,null,null);
}
@凌驾
public void onLoadFinished(加载器、光标数据){
mAdapter.changeCursor(数据);
}
@凌驾
公共void onLoaderReset(加载器){
mAdapter.changeCursor(空);
}

对于仍然跌跌撞撞地问这个问题的人:

我也有同样的问题,在我的回收器视图中重复了同样的5个条目

解决我的问题的方法就是简单地移除

adapter.setHasStableIds(true)

当然,这让我遇到了其他问题,但我设法用它显示了我的所有条目。

为什么要重新发明轮子?为什么不使用适配器?@pskink很好,很好。。我想我只是想理解为什么这个不能在
onBindViewHolder中添加一些
Log.d
调用then@pskink我在发帖前就这么做了,应该提到的。它将
标题
位置
的值打印到第5项,然后停止显示它停止了吗?我看到您的项目多于5个,您确定光标正确吗?您是否尝试过DatabaseUtils?