I have the lines of code in onLoadFinished for my Loader Manager callback in a fragment. The problem with the code is that the favoriteMovies is needed to be displayed in the ArrayAdapter so fo what ever reason, it is zero while the log statement show that the cursor or data is not empty.
#Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
Log.i(TAG, "Loader Manager Loading Finished: " + data.getCount());
if (data != null && data.getCount() > 0) {
while (data.moveToNext()) {
favouriteMovies.add(
new Movie(
data.getString(2),
data.getString(3),
data.getString(4),
data.getInt(1),
data.getString(5),
data.getDouble(6),
data.getDouble(7)
));
}
setMovies(favouriteMovies);
hideLoadingProgress();
mAdapter.notifyDataSetChanged();
} else {
showEmptyView(EMPTY_VIEW_TYPE_LOCAL);
hideLoadingProgress();
}
}
How can i write this code so that it actually waits fro the while statement to finish before calling the setMovies() method. When i minimize my app to the background and restore it, the movies are diaplayed, but it does not work in the first time it is called.
After much trial i simple have to check the cursor to know if it is the last index and then call my other function as shown below.
#Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
Log.i(TAG, "Loader Manager Loading Finished: " + data.getCount());
favouriteMovies.clear();
if (data != null && data.getCount() > 0) {
while (data.moveToNext()) {
favouriteMovies.add(
new Movie(
data.getString(2),
data.getString(3),
data.getString(4),
data.getInt(1),
data.getString(5),
data.getDouble(6),
data.getDouble(7)
));
if (data.isLast()) {
setMovies(favouriteMovies);
hideLoadingProgress();
mAdapter.notifyDataSetChanged();
}
}
} else {
showEmptyView(EMPTY_VIEW_TYPE_LOCAL);
hideLoadingProgress();
}
}
Related
I am querying POJO which is NOT being Observed / Non-Live data from an IntentService that was started in a PreferenceFragment. However a second my application crashes and log displays:
java.lang.IllegalStateException: Cannot access database on the main thread since it may potentially lock the UI for a long period of time.
at android.arch.persistence.room.RoomDatabase.assertNotMainThread(RoomDatabase.java:204)
at android.arch.persistence.room.RoomDatabase.query(RoomDatabase.java:232)
at vault.dao.xxxDao_Impl.getAllNonLivePojoItems(xxxDao_Impl.java:231)
I want to know why is my program throwing this exception. as per https://stackoverflow.com/a/23935791/8623507
my database query[s] are inside an IntentService that runs In its own thread so i should be in the green. here is my code:
Inside IntentService
--------------------
// ERROR OCCURS HERE
List<POJO> pojoList = localRepo.getAllNonLivePojoItems(); // <= ERROR POINTS HERE
if (pojoList != null && pojoList.size() > 0) {
for (Pojo pojo : pojoList ){
// Do Long Running Task Here ....
}
Also I instantiate The Objects Being Used and call the above methods from those Objects Throughout the IntentService in OnHandleIntent like so:
#Override
protected void onHandleIntent(Intent intent) {
if (intent != null) {
final String action = intent.getAction();
LocalRepo localRepo = new LocalRepo(this.getApplication());
PojoHelper pojoHelper = new PojoHelper(this, localRepo);
if (LOGOUT.equals(action) && type != null) {
Log.d(TAG, "onHandleIntent: LOGOUT");
pojoHelper.logoutPojo();
}
else if(DELETE.equals(action) && type != null){
Log.d(TAG, "onHandleIntent: DELETE_POJO");
pojoHelper.deletePojo(true);
}
}
}
I assume you get callback from AsyncTask onPostExecute() method which runs on UI thread. It is prohibited to use database or network calls inside UI thread because it can block UI.
Execute your code where you access database inside new thread.
Example:
Executors.newSingleThreadExecutor().execute(()->{
//TODO access Database
});
One thing i failed to mention was that the method was being executed within an async's response callback method
PojoWarehouse.processPojoItems(new AsyncPojoCallback() {
#Override
public void done(Exception e) {
if (e == null) {
// ERROR OCCURS HERE
List<POJO> pojoList = localRepo.getAllNonLivePojoItems(); // <= ERROR POINTS HERE
if (pojoList != null && pojoList.size() > 0) {
for (Pojo pojo : pojoList ){
// Do Long Running Task Here ....
}
} else
Log.d(TAG, "done: Error Logging Out: " + e.getLocalizedMessage());
}
});
I cannot explain on a technical level why this fixed the issue, however suggestions are welcomed.
AM not so good in android Fragments. We need a proper solution to solve my problem. In mother activity I've fragment A,B,C. Every fragments comes after another . First A fragment is shown and after some user input we replace it with Fragment B and Fragment A is stacked. After B is done we replace it with C Fragment and B is stacked after A. I can go back to previous fragment to Edit the Data so we are saving the user inputs.
But when I go back to Fragments always I can get all data but only can update Fragment A UI view . I can't update any ui view in Fragment B and C .
Code snippests for different portions
abid hasan: getting current object for updating
City currentCity = cityManager.getCurrentCity();
updateViews(currentCity);
checking data for this object and updating views .
public static void updateViews(City currentCity){
Log.d(TAG , "updating views for city... "+currentCity.getCityName());
if (currentCity.getCityAccommodations().size() > 0 || currentCity.getCityLocations().size() > 0 || currentCity.getHotels().size() > 0) {
viewModifyLayoutHotels.setVisibility(View.VISIBLE);
deSelectAccommodationRadioButton.setChecked(false);
} else {
viewModifyLayoutHotels.setVisibility(View.GONE);
}
if (currentCity.cityFlights.size() > 0) {
viewModifyLayoutFlights.setVisibility(View.VISIBLE);
deSelectFlyRadioButton.setChecked(false);
} else {
viewModifyLayoutFlights.setVisibility(View.GONE);
}
if(currentCity.getFlightClass().equals("")){
viewModifyLayoutFlights.setVisibility(View.VISIBLE);
deSelectFlyRadioButton.setChecked(false);
}else {
viewModifyLayoutFlights.setVisibility(View.GONE);
}
if (currentCity.getActivities().size() > 0) {
viewModifyLayoutActivities.setVisibility(View.VISIBLE);
deSelectActivityRadioButton.setChecked(false);
} else {
viewModifyLayoutActivities.setVisibility(View.GONE);
}
}
on back press event from the successor fragment’s onDestroy() method and getting that object from manager and updating views
public static void updateViews(City currentCity){
Log.d(TAG , "updating views for city... "+currentCity.getCityName());
if (currentCity.getCityAccommodations().size() > 0 || currentCity.getCityLocations().size() > 0 || currentCity.getHotels().size() > 0) {
viewModifyLayoutHotels.setVisibility(View.VISIBLE);
deSelectAccommodationRadioButton.setChecked(false);
} else {
viewModifyLayoutHotels.setVisibility(View.GONE);
}
if (currentCity.cityFlights.size() > 0) {
viewModifyLayoutFlights.setVisibility(View.VISIBLE);
deSelectFlyRadioButton.setChecked(false);
} else {
viewModifyLayoutFlights.setVisibility(View.GONE);
}
if(currentCity.getFlightClass().equals("")){
viewModifyLayoutFlights.setVisibility(View.VISIBLE);
deSelectFlyRadioButton.setChecked(false);
}else {
viewModifyLayoutFlights.setVisibility(View.GONE);
}
if (currentCity.getActivities().size() > 0) {
viewModifyLayoutActivities.setVisibility(View.VISIBLE);
deSelectActivityRadioButton.setChecked(false);
} else {
viewModifyLayoutActivities.setVisibility(View.GONE);
}
}
before onDestroy() call I reset Those view to initial state from onStart() method
#Override
public void onStart() {
Log.d(MakeATripStepFourFragment.TAG , "calling onStart from "+TAG);
MakeATripFragmentFirstTime.destinationTextView.setText("Select experts for your trip");
resetCityPreferencesViews();
super.onStart();
}
public static void resetCityPreferencesViews() {
MakeATripStepFourFragment.deSelectFlyRadioButton.setChecked(true);
MakeATripStepFourFragment.selectFlyRadioButton.setChecked(false);
MakeATripStepFourFragment.deSelectAccommodationRadioButton.setChecked(true);
MakeATripStepFourFragment.selectAccommodationRadioButton.setChecked(false);
MakeATripStepFourFragment.deSelectActivityRadioButton.setChecked(true);
MakeATripStepFourFragment.selectActivityRadioButton.setChecked(false);
MakeATripStepFourFragment.viewModifyLayoutFlights.setVisibility(View.GONE);
MakeATripStepFourFragment.viewModifyLayoutHotels.setVisibility(View.GONE);
MakeATripStepFourFragment.viewModifyLayoutActivities.setVisibility(View.GONE);
}
for that city for those UI is not updating I use a LocalBroadcast call to Separate UI thread updating
if (!childFragment.onBackPressed()) {
// child Fragment was unable to handle the task
// It could happen when the child Fragment is last last leaf of a chain
// removing the child Fragment from stack
Log.d(MakeATripStepFourFragment.TAG , "this fragment.. "+childFragment.getClass().getSimpleName());
if(childFragment instanceof MakeATripStepFourFragment){
Log.d(MakeATripStepFourFragment.TAG ,"lets try it out");
if (cityManager.hasNextCity()) {
City currentCity = cityManager.getNextCity();
MakeATripFragmentFirstTime.destinationTextView.setText(currentCity.getCityName());
Log.d(MakeATripStepFourFragment.TAG, "updating radioButton with city.. "+currentCity.getCityName());
Intent intent = new Intent("update_radio-button");
intent.putExtra(MakeATripStepFourFragment.TAG,currentCity);
LocalBroadcastManager.getInstance(((MakeATripStepFourFragment) childFragment).getActivity()).sendBroadcast(intent);
}
}
childFragmentManager.popBackStackImmediate();
}
// either this Fragment or its child handled the task
// either way we are successful and done here
return true;
}
and in the broadcast receiver i call the updateViews() method
private BroadcastReceiver updatePreferences = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
City currentCity = (City) intent.getSerializableExtra(TAG);
Log.d(TAG , "recived call with city..."+currentCity.getCityName());
ExpertSelectorFragment.resetCityPreferencesViews();
updateViews(currentCity);
}
};
What maybe the solution I have to know . Please wanting perfect suggestion
Here on a button click looping through the city objects and adding fragment for them , if all city traversing completed then proceed to the next fragmet
case R.id.next_fragment_destination_button:
if (!cityManager.hasNextCity()) {
Log.d("status = ", "all city explored : " + cityManager.currentCityIndex);
cityManager.currentCityIndex = 0;
/*LocalBroadcastManager.getInstance(mActivity).unregisterReceiver(messageReceiver);
LocalBroadcastManager.getInstance(mActivity).unregisterReceiver(accomodationAllPreferenceReciver);
LocalBroadcastManager.getInstance(mActivity).unregisterReceiver(activitiesForCityPreferenceReciver);
LocalBroadcastManager.getInstance(mActivity).unregisterReceiver(updatePreferences);*/
setDataToGetExperts(cityManager.getAllCities().size());
} else {
Log.d("status = ", "going to next : " + cityManager.currentCityIndex);
Log.d(TAG, "current city size " + cityManager.getCurrentCity().toString());
cityManager.getNextCity();
//we have a next city
MakeATripStepFourFragment fragment = MakeATripStepFourFragment.newInstance(0);
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
// Store the Fragment in stack
transaction.addToBackStack(null);
transaction.replace(R.id.fragment_holder, fragment ,TAG);
transaction.commit();
}
break;
At the beginning of the chat app user see a list off groups (listview group) available and the user have the possibility to create a new group or click on some off the available groups and then start to write messages (listview messages). The functions CreateNewMessage and CreateNewGroup pushes information to firebase correctly
Above scenarios works finne problems arise when user navigates backwards (popBackStack()) from listview with messages to GroupFragment, here should user be presented a list off available groups but the listview is empty. The ReadGroupData() function is not reading the already created groups from firebase and inserts them in the group listview. How to make this happen?
GroupFragment:
public void ReadGroupData() {
Firebase firebaserootRef = new Firebase("https://000.firebaseio.com");
firebaserootRef.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot snapshot, String s) {
if (snapshot.getValue() != null) {
Group newGroup = new Group((String)snapshot.child("name").getValue(),
(String) snapshot.child("id").getValue());
if(!groupKeyValues.contains(newGroup.GetId())) {
groupKeyValues.add(newGroup.GetId());
AddToLstViewGroup(newGroup);
System.out.println("Read group data from firebase and
inserted in listView");
}
}
}
});
}
public void AddToLstViewGroup(Group newGroup) {
groupNameList.add(newGroup);
if(groupAdapter == null) {
groupAdapter = new GroupAdapter(getActivity(), groupNameList);
}
if (lstViewGroup == null) {
lstViewGroup = (ListView) getView().
findViewById(R.id.listView_group);
}
lstViewGroup.setOnItemClickListener(onItemClickListener);
lstViewGroup.setOnItemLongClickListener(onItemLongClickListener);
groupAdapter.notifyDataSetChanged();
lstViewGroup.setAdapter(groupAdapter);
}
ChatFragment:
public void ReadChatMessages(Firebase firebaseRootRef) {
firebaseRootRef.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot snapshot, String s) {
if (snapshot.child(GetGroupId()).child("messages").
getChildren() != null) {
for (DataSnapshot c :
snapshot.child(GetGroupId()).child("messages").getChildren()) {
String key = c.getKey();
Message newMessage = new Message();
newMessage.SetFrom((String) c.child("from").getValue());
newMessage.SetMsg((String)
c.child("message").getValue());
newMessage.SetTime((String) c.child("time").getValue());
newMessage.SetId((String) c.child("id").getValue());
if ((!msgKeyValues.contains(key)) ||
newMessage.GetFrom() != "") {
msgKeyValues.add(key);
AddToLstViewChat(newMessage);
//Automatic scrolls to last line in listView.
lstViewChat.setSelection(chatAdapter.getCount() -1);
}
}
}
}
public void AddToLstViewChat(Message newMessage) {
chatMsgList.add(newMessage);
if (chatAdapter == null) {
chatAdapter = new ChatAdapter(getActivity(), chatMsgList);
}
if(IsMsgFromMe(newMessage)) {
lstViewChat = (ListView)
getView().findViewById(R.id.listView_chat_message_me);
} else {
lstViewChat =
(ListView)getView().findViewById(R.id.listView_chat_message_others);
}
chatAdapter.notifyDataSetChanged();
lstViewChat.setAdapter(chatAdapter);
}
ChatActivity:
#Override
public void onBackPressed() {
if (getFragmentManager().getBackStackEntryCount() > 0) {
getFragmentManager().popBackStack();
} else {
finish();
}
}
For all the code click on the link: "http://pastebin.com/97nR68Rm"
SOLUTION!
Kato thank you for you patience and help. I have now found a solution for the problem. I'm calling ReadGroupData() and ReadChatMessages() at the end (before return) in my onCreateView methods. As Kato pointed out onCreate() is not getting called on popBackStack()
In my AddToLStViewGroup the if statement for lstViewGroup is deleted so now it always sets the listView otherwise it will throw an exception for not finding the correct view, To clarifying:
Deleted this line:
if (lstViewGroup == null) {
lstViewGroup = (ListView)getView().findViewById(R.id.listView_group);
}
And replaced with:
ListView lstViewGroup=(ListView)getView().findViewById(R.id.listView_group);
Kato thank you for you patience and help. I have now found a solution for the problem. I'm calling ReadGroupData() and ReadChatMessages() at the end (before return) in my onCreateView methods. As Kato pointed out onCreate() is not getting called on popBackStack()
In my AddToLStViewGroup the if statement for listViewGroup is deleted so now it always sets the listView otherwise it will throw an exception for not finding the correct view.
To clarify:
I deleted this line:
if (lstViewGroup == null) {
lstViewGroup = (ListView)getView().findViewById(R.id.listView_group);
}
And replaced it with:
ListView lstViewGroup =(ListView)getView().findViewById(R.id.listView_group);
(The original asker posted the answer as part of the question. I'm copying it here as a matter of housekeeping.)
While I have been unable to duplicate this bug, I still get a trickle of crash reports with this occurring. I thought adding a null check for my list adapter would fix it, but its still occurring. What am I missing?
Full stacktrace:
http://pastebin.com/Q6GwDU7Q
public void onLoaderReset(Loader<Cursor> loader) {
final int id = loader.getId();
switch (id) {
case LOADER_ID1:
if (mAdapter != null)
mAdapter.changeCursor(null); //Line 512 where stacktrace references
break;
case LOADER_ID2:
//Other code here
break;
default:
throw new InvalidParameterException("id=" + id);
}
}
mAdapter is initialized in onActivityCreated, but I realize while typing this I do not ever release it, maybe I should perform that in onDetach? mAdapter is attached to a ListView set up by a ListFragment. And I set the adapters cursor to null to clear the list I have. So yes, what am I overlooking?
Well after searching through the Android source, I see now that like GreyBearedGeek stated, I should allow the loader to handle the Cursor destruction.
As you can see in CursorLoader it will handle closing the old cursor if passing a new one:
/* Runs on the UI thread */
#Override
public void deliverResult(Cursor cursor) {
if (isReset()) {
// An async query came in while the loader is stopped
if (cursor != null) {
cursor.close();
}
return;
}
Cursor oldCursor = mCursor;
mCursor = cursor;
if (isStarted()) {
super.deliverResult(cursor);
}
if (oldCursor != null && oldCursor != cursor && !oldCursor.isClosed()) {
oldCursor.close();
}
}
As well as closing the cursor upon a reset.
#Override
protected void onReset() {
super.onReset();
// Ensure the loader is stopped
onStopLoading();
if (mCursor != null && !mCursor.isClosed()) {
mCursor.close();
}
mCursor = null;
}
So I will use swapCursor in the future as to not interfere with CursorLoaders handling, even though I've yet to see how this causes my NPE in the stacktrace above.
I have implemented the class found in this question:
CursorLoader usage without ContentProvider
It is a means of using the LoaderManager and CursorLoader without a content resolver. I am using it to load data from a SQLite database and display it in a ListFragment.
The problem I am seeing is that the database is leaking. Obviously this is because I am not closing the database when I am done.
I have now started to do this, but I am concerned as the database could be accessed at any time by background tasks scheduled with the AlarmManager. I am worried that I might close the database when another class needs it open.
My solution has been to count the opens/closes and only close the database when no one is using it. Like so:
public synchronized SQLiteDatabase openDataBase()
{
try
{
mDatabaseUsers++;
Log.d(TAG, "DatabaseUsers: " + mDatabaseUsers);
// If already open, return it.
if (mOpenDatabase != null && mOpenDatabase.isOpen())
return mOpenDatabase;
OpenHelper openHelper = new OpenHelper(mContext);
return openHelper.getWritableDatabase();
} catch (SQLException e)
{
Log.e("MessageDelay", "Error opening database: " + e.toString());
return null;
}
}
public synchronized void closeDatabase()
{
mDatabaseUsers--;
// If no one is using the database, close it.
if (mOpenDatabase != null && mDatabaseUsers == 0)
{
mOpenDatabase.close();
}
Log.d(TAG, "DatabaseUsers: " + mDatabaseUsers);
}
This appears to work, but it has meant adding an extra line of code all over my application. Furthermore I've had trouble with the LoaderManager not behaving as expected and it calls its reset function more than it does its load, so I've had to put this fix in:
return new SimpleCursorLoader(getActivity())
{
private int mDBOpens = 0;
#Override
public Cursor loadInBackground()
{
mDBOpens++;
return JSQLite.getSingleton(getActivity()).retrieveTextsSent(mMode == 1 ? true : false);
}
#Override
public void reset()
{
if (mDBOpens > 0)
{
JSQLite.getSingleton(getContext()).closeDatabase();
}
super.reset();
mDBOpens--;
}
};
It feels like this isn't the correct way of doing it. Is there another, cleaner means of closing/opening the database only when needed?
Thanks, Jason.