Reputation: 1
I've searched exhaustively for the right way to do this, and despite it often being said that you should not load data on the UI thread, nobody ever posts code!
So, this is what I am doing, and it's not quite working:
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/...
linearLayoutManager = new LinearLayoutManager(getActivity());
list = new ArrayList<>();
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
new Thread(new Runnable() {
@Override
public void run() {
reloadData();
}
}).start();
adapter = new MyAdapter(args, list, etc);
return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void reloadData() {
list.clear();
list.addAll(fetchDataFromSQLDatabase(args));
}
The RecyclerView stuff is handled in the parent class' onCreateView() (binding views, attaching adapter to the recyclerview, etc).
But is this the right way to load in the data? The list always shows up blank, until I rotate the screen or do something that interacts with the list, etc.
Is there a simple way I can modify what I have above so it works? I am not looking to overhaul the whole thing; I just want it to work. I'm using GreenDao for my database access so I can't change everything around too much. I just want to reorganize the code above so it works. I'm repeating myself a lot here because every time I ask this question people ignore it. I'm not looking to drastically overhaul this. I'm not looking to drastically overhaul this. I'm not looking to drastically overhaul this. I just want it to work.
Upvotes: 0
Views: 547
Reputation: 349
For doing db related operation you should use load manager loader call back.
public class SampleActivity extends Activity implements LoaderManager.LoaderCallbacks<D> {
public Loader<D> onCreateLoader(int id, Bundle args) { ... }
public void onLoadFinished(Loader<D> loader, D data) { ... }
public void onLoaderReset(Loader<D> loader) { ... }
}
onCreateLoader() get called when you call getLoadmanager().initLoader(). This method used to initialize the loader(cusrsorLoader). This may be any kind of db operation(query etc).
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
// Create a new CursorLoader with the following query parameters.
return new CursorLoader(SampleListActivity.this, CONTENT_URI,
PROJECTION, null, null, null);
}
onLoadFinished() get called when the load manager finishes its load.
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
// A switch-case is useful when dealing with multiple Loaders/IDs
switch (loader.getId()) {
case LOADER_ID:
// The asynchronous load is complete and the data
// is now available for use. Only now can we associate
// the queried Cursor with the SimpleCursorAdapter.
// call the recycler view adapter to show the data
// pass the loader object to adapter and the set the adapter to
// recycler view
break;
}
// Your recycler view now displays the queried data.
}
Upvotes: 1