如何在非活动和非片段类中使用CursorLoader?

时间:2017-03-16 06:03:14

标签: android sqlite android-cursoradapter

这可能是一个愚蠢的问题,因为对于这个游标加载器而言我是否有可能实现它是否有点困惑,我试图为整个项目制作公共游标加载器,到目前为止我已经初始化了每个游标加载器活动在哪里需要但是现在,我试图让它在普通的课堂但问题很困惑如何在非活动课程中初始化游标加载器请有人帮助我如何实现这个让我发布到目前为止我尝试过的内容< / p>

这是我实现了cursorloader的类:

 public class CursorLoader implements LoaderManager.LoaderCallbacks<Cursor> {
  private Context mcontext;
  private Cursor  mcursor;
  public CursorValue cursorValue;

    public void initCursor(Cursor cursor,Context context) {
        this.mcontext=context;
        this.mcursor=cursor;
        if(this.mcontext instanceof Activity){
            ((Activity)this.mcontext).getLoaderManager().initLoader(0, null,this); //here you can init loader
        } else {
            throw new ClassCastException("context should implement activity class");
        }
       }

    @Override
    public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
        return new android.content.CursorLoader(mcontext, IncidentDAO.DB_SF_Incident, null, null, null, null){

            ForceLoadContentObserver mObserver=new ForceLoadContentObserver();
            @Override
            public Cursor loadInBackground() {

           if (mcursor != null) {
                    mcursor.registerContentObserver(mObserver);
                    mcursor.setNotificationUri(getContext().getContentResolver(), getUri());

                }
                return mcursor;
            }
        };
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
            cursorValue.getValue(cursor);
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {

    }
}

在我的活动课中我试图像这样初始化:

 public class Incident extends AppCompatActivity implements CursorValue {
        private IncidentDAO incidentDAO;
        private RecyclerView recyclerView;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.incident_main);
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
            recyclerView = (RecyclerView) findViewById(R.id.incident_recyclerview);
            LinearLayoutManager layoutManager = new LinearLayoutManager(Incident.this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);

           CursorLoader cursorLoader=new CursorLoader();
            cursorLoader.cursorValue=Incident.this;
            Cursor c = incidentDAO.IncidentList();
            cursorLoader.initCursor(c,this);
        }
         @Override
        public void getValue(Cursor cursor) {
            final IncidentCursorAdapter incidentCursorAdapter = new IncidentCursorAdapter(Incident.this, cursor);
            recyclerView.setAdapter(incidentCursorAdapter);
            TextView textView = (TextView) findViewById(R.id.emptytext);
            if (incidentCursorAdapter.getItemCount() == 0 && incidentCursorAdapter != null) {
                textView.setText("No Data Available");
                textView.setVisibility(View.VISIBLE);
            } else {
                textView.setVisibility(View.INVISIBLE);
            }

            incidentCursorAdapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClicked(Cursor cursor) {
                    Intent intent = new Intent(Incident.this, IncidentView.class);
                    int is_localvendor = cursor.getInt(cursor.getColumnIndex(IncidentModel.Incident_LocalVendor));
                    int is_generalclaim = cursor.getInt(cursor.getColumnIndex(IncidentModel.Incident_IsGeneralClaim));
                    int is_partrequired = cursor.getInt(cursor.getColumnIndex(IncidentModel.Incident_IsPartRequired));
                    int is_installationcall = cursor.getInt(cursor.getColumnIndex(IncidentModel.Incident_IsInstallationCall));
                    int id = cursor.getInt(cursor.getColumnIndex(IncidentModel.Incident_RegistrationID));
                    Bundle bundle = new Bundle();
                    bundle.putInt("id", id);
                    bundle.putInt("generalclaim", is_generalclaim);
                    bundle.putInt("localvendor", is_localvendor);
                    bundle.putInt("partrequired", is_partrequired);
                    bundle.putInt("installationcall", is_installationcall);
                    intent.putExtras(bundle);
                    startActivity(intent);
                }
            });

        }

有点困惑我是以错误的方式做错还是有人告诉我如何初始化游标提前谢谢!!

崩溃报告:

 Caused by: java.lang.IllegalStateException: Observer android.content.Loader$ForceLoadContentObserver@42207168 is already registered.
                                                                                    at android.database.Observable.registerObserver(Observable.java:49)
                                                                                    at android.database.ContentObservable.registerObserver(ContentObservable.java:32)
                                                                                    at android.database.AbstractCursor.registerContentObserver(AbstractCursor.java:319)
                                                                                    at precisioninfomatics.servicefirst.HelperClass.CursorLoader$1.loadInBackground(CursorLoader.java:41)
                                                                                    at precisioninfomatics.servicefirst.HelperClass.CursorLoader$1.loadInBackground(CursorLoader.java:34)
                                                                                    at android.content.AsyncTaskLoader.onLoadInBackground(AsyncTaskLoader.java:312)
                                                                                    at android.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:69)
                                                                                    at android.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:57)
                                                                                    at android.os.AsyncTask$2.call(AsyncTask.java:288)
                                                                                    at java.util.concurrent.FutureTask.run(FutureTask.java:237)

这部分导致崩溃:

 return new android.content.CursorLoader(mcontext, IncidentDAO.DB_SF_Incident, null, null, null, null){

            ForceLoadContentObserver mObserver=new ForceLoadContentObserver();
            @Override
            public Cursor loadInBackground() {

                if (mcursor != null) {
                    mcursor.registerContentObserver(mObserver);
                    mcursor.setNotificationUri(getContext().getContentResolver(), getUri());

                }
                return mcursor;
            }
        };

mcursor.registercontentobserver导致崩溃如何处理!

1 个答案:

答案 0 :(得分:0)

试试这个:

public void initCursor(Cursor cursor,Context context) {
          this.mcontext=context;
         this.mcursor=cursor;

if(this.context instanceof Activity) {
((Activity)this.context).getLoaderManager() //here you can init loader 
      } else { 
             throw new ClassCastException("context should implement activity class");
      }
//....
} 

让我知道它是否有效。