这可能是一个愚蠢的问题,因为这个游标加载器的新增功能有点困惑我的问题是否可以实现它,我试图为整个项目制作通用的游标加载器,到目前为止我已经在每个活动中初始化了单独的游标加载器哪里需要,但现在,我试图使它在普通的类,但问题是困惑如何初始化非活动类中的cursorloader请有人可以帮助我如何实现这个让我发布我迄今尝试过的东西如何在非活动和非片段类中使用CursorLoader?
这是我实现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 [email protected] 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造成死机怎么处理!
你有错误吗? –
无错无显示我们需要初始化光标加载器,但我无法初始化它如何做到这一点! –
您是否尝试将上下文转换为活动类? –