Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/207.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_Database_Sqlite_Android Recyclerview_Android Cursorloader - Fatal编程技术网

Android 由于带有游标加载程序的recyclerview导致主活动为空

Android 由于带有游标加载程序的recyclerview导致主活动为空,android,database,sqlite,android-recyclerview,android-cursorloader,Android,Database,Sqlite,Android Recyclerview,Android Cursorloader,现在我正在尝试使用recyclerview和一个游标加载程序。根据我的研究,我在我的recyclerview适配器中包括了cursorloader。我不希望将我的SQLite数据库数据放入arraylist中。现在看起来我的代码是正确的,但当我加载应用程序时,我会看到一个空白屏幕。有人能帮我看一下代码中的错误吗 这是我的适配器: public class PrescriptionRecyclerAdapter extends RecyclerView.Adapter<Prescriptio

现在我正在尝试使用recyclerview和一个游标加载程序。根据我的研究,我在我的recyclerview适配器中包括了cursorloader。我不希望将我的SQLite数据库数据放入arraylist中。现在看起来我的代码是正确的,但当我加载应用程序时,我会看到一个空白屏幕。有人能帮我看一下代码中的错误吗

这是我的适配器:

public class PrescriptionRecyclerAdapter extends RecyclerView.Adapter<PrescriptionRecyclerAdapter.ViewHolder> {

private CursorAdapter mCursorAdapter;
private Context mContext;
private ViewHolder holder;
Cursor prescriptionCursor;




public PrescriptionRecyclerAdapter(Context context, Cursor c) {
    mContext = context;
    prescriptionCursor = c;
    mCursorAdapter = new CursorAdapter(mContext, c, 0) {




        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent) {
            // Inflate the view here

                    View v = LayoutInflater.from(context)
                    .inflate(R.layout.recycle_item, parent, false);
                return v;

        }




        @Override
        public void bindView(View view, Context context, Cursor cursor) {



            // Extract data from the current store row and column
            int nameColumnIndex = cursor.getColumnIndex(PrescriptionContract.PrescriptionEntry.COLUMN_PRESCRIPTION_NAME);
            int amountColumnIndex = cursor.getColumnIndex(PrescriptionContract.PrescriptionEntry.COLUMN_PRESCRIPTION_AMOUNT);
            int durationColumnIndex = cursor.getColumnIndex(PrescriptionContract.PrescriptionEntry.COLUMN_PRESCRIPTION_FREQUENCY_DURATION);
            final int columnIdIndex = cursor.getColumnIndex(PrescriptionContract.PrescriptionEntry._ID);

            //Read the store attritubes from the Cursor for the current stores
            String name = cursor.getString(nameColumnIndex);
            String amount = cursor.getString(amountColumnIndex);
            String duration = cursor.getString(durationColumnIndex);
            String col = cursor.getString(columnIdIndex);

            // Populate fields with extracted properties
            holder.prescriptionName.setText(name);
            holder.prescriptionAmount.setText(amount);
            holder.prescriptionDays.setText(duration);


        }
    };



}

public static class ViewHolder extends RecyclerView.ViewHolder {

    public TextView prescriptionName;
    public TextView prescriptionAmount;
    public TextView prescriptionDays;
    final public Button prescriptionButton;

    public ViewHolder(View itemView) {
        super(itemView);
        // Find fields to populate in inflated template
        prescriptionName = (TextView) itemView.findViewById(R.id.name);
        prescriptionAmount = (TextView) itemView.findViewById(R.id.amountlist);
        prescriptionDays = (TextView) itemView.findViewById(R.id.daysList);
        prescriptionButton = itemView.findViewById(R.id.scheduleButton);

    }
}

@Override
public int getItemCount() {
    return mCursorAdapter.getCount();
}

public Cursor swapCursor(Cursor cursor) {
    if (prescriptionCursor == cursor) {
        return null;
    }
    Cursor oldCursor = prescriptionCursor;
    this.prescriptionCursor = cursor;
    if (cursor != null) {
        this.notifyDataSetChanged();
    }
    return oldCursor;
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    // Passing the binding operation to cursor loader
    mCursorAdapter.getCursor().moveToPosition(position);
    mCursorAdapter.bindView(holder.itemView, mContext, mCursorAdapter.getCursor());
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    // Passing the inflater job to the cursor-adapter
    View v = mCursorAdapter.newView(mContext, mCursorAdapter.getCursor(), parent);
    holder = new ViewHolder(v);
    return holder;
}
}
公共类RecyclerAdapter扩展了RecyclerView.Adapter{
专用游标适配器mCursorAdapter;
私有上下文;
私人持票人;
光标指示符;
公共处方回收器适配器(上下文,光标c){
mContext=上下文;
处方库索=c;
mCursorAdapter=新游标适配器(mContext,c,0){
@凌驾
公共视图newView(上下文上下文、光标、视图组父对象){
//把这里的景色放大
视图v=LayoutFlater.from(上下文)
.充气(R.layout.U项目,父项,假);
返回v;
}
@凌驾
公共void bindView(视图、上下文上下文、光标){
//从当前存储行和列中提取数据
int-nameColumnIndex=cursor.getColumnIndex(PrescriptionContract.PrescriptionEntry.COLUMN\u PRESCRIPTION\u NAME);
int-amountColumnIndex=cursor.getColumnIndex(处方合同.PrescriptionEntry.COLUMN\u处方金额);
int-durationColumnIndex=cursor.getColumnIndex(处方合同.处方尝试.列\处方\频率\持续时间);
final int columnIdIndex=cursor.getColumnIndex(PrescriptionContract.PrescriptionEntry.\u ID);
//从光标中读取当前存储的存储属性
字符串名称=cursor.getString(nameColumnIndex);
字符串金额=cursor.getString(amountColumnIndex);
字符串持续时间=cursor.getString(durationColumnIndex);
String col=cursor.getString(columnIdIndex);
//使用提取的属性填充字段
持有者.处方名称.设置文本(名称);
持有者。处方数量。设置文本(金额);
持有人。规定日期。设定文本(持续时间);
}
};
}
公共静态类ViewHolder扩展了RecyclerView.ViewHolder{
公共文本视图处方名称;
公共文本视图;
公共文本查看规定日;
最终公共按钮处方按钮;
公共视图持有者(视图项视图){
超级(项目视图);
//查找要在模板中填充的字段
处方名称=(TextView)itemView.findViewById(R.id.name);
prescriptionAmount=(TextView)itemView.findViewById(R.id.amountlist);
prescriptionDays=(TextView)itemView.findViewById(R.id.daysList);
prescriptionButton=itemView.findViewById(R.id.scheduleButton);
}
}
@凌驾
public int getItemCount(){
返回mCursorAdapter.getCount();
}
公共游标交换游标(游标游标){
if(prescriptionCursor==游标){
返回null;
}
游标oldCursor=处方游标;
this.prescriptionCursor=光标;
如果(光标!=null){
this.notifyDataSetChanged();
}
返回光标;
}
@凌驾
公共无效onBindViewHolder(ViewHolder,int位置){
//将绑定操作传递给游标加载器
mCursorAdapter.getCursor().MoveTopoPosition(位置);
bindView(holder.itemView、mContext、mCursorAdapter.getCursor());
}
@凌驾
public ViewHolder onCreateViewHolder(视图组父级,int-viewType){
//将充气机作业传递给光标适配器
视图v=mCursorAdapter.newView(mContext,mCursorAdapter.getCursor(),父级);
支架=新的视图支架(v);
报税表持有人;
}
}
这是我的展示活动

 public class DisplayActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor>{
private static final int PRESCRIPTION_LOADER = 0;
PrescriptionRecyclerAdapter mCursorAdapter;
private RecyclerView.LayoutManager mLayoutManager;





@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(DisplayActivity.this, EditorActivity.class);
            startActivity(intent);
        }
    });

    RecyclerView prescriptionRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
    mLayoutManager = new LinearLayoutManager(getApplicationContext());
    prescriptionRecyclerView.setLayoutManager(mLayoutManager);


    mCursorAdapter = new PrescriptionRecyclerAdapter(this, null);
    prescriptionRecyclerView.setAdapter(mCursorAdapter);


    //Kick off the loader
    getLoaderManager().initLoader(PRESCRIPTION_LOADER,null,this);

}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_display, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // User clicked on a menu option in the app bar overflow menu
    switch (item.getItemId()) {
        // Respond to a click on the "Delete all entries" menu option
        case R.id.action_delete_all_entries:
            deleteAllPrescriptions();
            return true;

    }

    return super.onOptionsItemSelected(item);
}

/**
 * Helper method to delete all items in the database.
 */
private void deleteAllPrescriptions() {
    int rowsDeleted = getContentResolver().delete(PrescriptionEntry.CONTENT_URI, null, null);
    Log.v("CatalogActivity", rowsDeleted + " rows deleted from prescription database");
}

@Override
public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
    // Since the editor shows all store attributes, define a projection that contains
    // all columns from the store table
    String[] projection = {
            PrescriptionEntry._ID,
            PrescriptionEntry.COLUMN_PRESCRIPTION_NAME,
            PrescriptionEntry.COLUMN_PRESCRIPTION_AMOUNT,
            PrescriptionEntry.COLUMN_PRESCRIPTION_FREQUENCY_HOURS,
            PrescriptionEntry.COLUMN_PRESCRIPTION_FREQUENCY_TIMES,
            PrescriptionEntry.COLUMN_PRESCRIPTION_FREQUENCY_DURATION,
            PrescriptionEntry.COLUMN_PRESCRIPTION_REFILL,
            PrescriptionEntry.COLUMN_PRESCRIPTION_EXPIRATION,
            PrescriptionEntry.COLUMN_PRESCRIPTION_PHARMACIST_NAME,
            PrescriptionEntry.COLUMN_PRESCRIPTION_PHARMACIST_NUMBER,
            PrescriptionEntry.COLUMN_PRESCRIPTION_PHYSICIAN_NAME,
            PrescriptionEntry.COLUMN_PRESCRIPTION_PHYSICIAN_NUMBER};

    // This loader will execute the ContentProvider's query method on a background thread
    return new CursorLoader(this,   // Parent activity context
            PrescriptionEntry.CONTENT_URI,         // Query the content URI for the current store
            projection,             // Columns to include in the resulting Cursor
            null,                   // No selection clause
            null,                   // No selection arguments
            null);                  // Default sort order
}

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    mCursorAdapter.swapCursor(data);
}



@Override
public void onLoaderReset(Loader<Cursor> loader) {
    mCursorAdapter.swapCursor(null);
}



}
公共类DisplayActivity扩展AppCompative实现LoaderManager.LoaderCallbacks{
私有静态最终整数加载程序=0;
处方回收器适配器mCursorAdapter;
private RecyclerView.LayoutManager MLLayoutManager;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_显示);
Toolbar Toolbar=(Toolbar)findViewById(R.id.Toolbar);
设置支持操作栏(工具栏);
FloatingActionButton fab=(FloatingActionButton)findViewById(R.id.fab);
fab.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
意向意向=新意向(DisplayActivity.this、EditorActivity.class);
星触觉(意向);
}
});
RecyclerView处方RecyclerView=(RecyclerView)findViewById(R.id.RecyclerView);
mLayoutManager=newlinearlayoutmanager(getApplicationContext());
处方RecyclerView.setLayoutManager(MLLayoutManager);
mCursorAdapter=新处方回收器适配器(此为空);
处方RecyclerView.setAdapter(mCursorAdapter);
//启动装载机
getLoaderManager().initLoader(处方加载程序,null,this);
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(右菜单菜单菜单显示菜单);
返回true;
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
//用户单击了应用程序栏溢出菜单中的菜单选项
开关(item.getItemId()){
//对单击“删除所有条目”菜单选项作出响应
案例R.id.action\u删除\u所有\u条目:
删除所有处方();
返回true;
}
返回super.onOptionsItemSelected(项目);
}
/**
*方法删除数据库中的所有项。
*/
私人处方{
int rowsDeleted=getContentResolver().delete(PrescriptionEntry.CONTENT_URI,null,
public Cursor swapCursor(Cursor cursor) {
  if (prescriptionCursor == cursor) {
    return null;
  }

  Cursor oldCursor = prescriptionCursor;
  this.prescriptionCursor = cursor;

  if (cursor != null) {
    this.notifyDataSetChanged();
    // update your Cursor for CursorAdapter
    mCursorAdapter.swapCursor(prescriptionCursor);
  }

  return oldCursor;
}