I have an adapter class that handles displaying a list of thumbnail images. When the user clicks on a thumbnail, it retrieves an image from a URL and takes about a second to do so and displays it in a dialog fragment. Because of the delay, I want there to be a toast that says "fetching image". However, the toast does not appear until the dialog fragment displays, which is pointless.
I have tried moving the toast before and after the call to make a dialog fragment and still the same result. I have tried using an AsyncTask to synchronize the toast first and then the dialog fragment but still the same result.
Adapter class:
holder.viewThumbnail.setOnClickListener(v ->
{
FetchImage fetchImage = new FetchImage(MainActivity.mainActivity, rootView, position);
fetchImage.execute();
});
FetchImage class:
protected final Void doInBackground(WeakReference<Activity>... params)
{
if(MainActivity.animalList.get(position).getImage() == null)
{
MainActivity mainActivity = weakReferenceActivity.get();
if(mainActivity != null)
{
new Handler(Looper.getMainLooper()).post(() -> Toast.makeText(mainActivity, mainActivity.getString(R.string.fetching_image), Toast.LENGTH_SHORT).show());
}
}
return null;
}
protected void onPostExecute(Void result)
{
super.onPostExecute(result);
MainActivity mainActivity = weakReferenceActivity.get();
if(mainActivity != null)
{
mainActivity.dialogShow(view, C.NO, C.DIALOG_IMAGE, "", "", position);
}
}
I also simply tried doing this in the adapter class without the AsyncTask route:
Adapter class:
holder.viewThumbnail.setOnClickListener(v ->
{
if(MainActivity.animalList.get(position).getImage() == null)
interfaceCommon.makeToast("fetching data", 1);
interfaceCommon.dialogShow(rootView, C.NO, C.DIALOG_IMAGE, "", "", position);
});
The interfaceCommon is simply a common interface to call methods in the Main Activity. I have one activity for this app and multiple fragments.
Related
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;
When a button is pressed, a dialog appears asking user for message, with the option of attaching an image (from url). The problem I'm having is once the recyclerview is filled with enough items to scroll, when the user scrolls quickly for some reason random images start popping up in seemingly random list items.
I know the problem has to come from when the image is actually placed into the imageview, since I can tell the link is added to the firebase db just fine.
When the image link is submitted, it's sent to /posts/$uid/$post-id in a HashMap. Kind of like this:
final Map<String, String> postMap = new HashMap<String, String>();
imagebutton.setOnClickListener((view) -> {
AlertDialog.Builder = new...
LayoutInflater in = ...
View dialogLayout = ...inflate(r.layout...., null);
build.setView(dialogLayout);
EditText imgText = ...
Button submit = ...
AlertDialog a = build.create();
submit.setOnClickListener((View) -> {
...
postMap.put("imgLink", imgText.getText().toString());
a.dismiss();
...
urlDialog.show();
Then a few more items are added to the map and pushed to firebase.
Firebase postRef = ref.child("posts").child(auth.getUid());
postMap.put("author", ...);
postMap.put("content", ...);
postRef.push().setValue(postMap);
But like I said, I'm almost 100% sure the problem is not in posting the information, just populating the recview
Here's my code for the list itself:
RecyclerView feed = (RecyclerView)findViewById(R.id.recycler);
if (ref.getAuth() != null) {
FirebaseRecyclerAdapter<TextPost, PostViewHolder> adapter = new FirebaseRecyclerAdapter<TextPost, PostViewHolder>(TextPost.class, R.layout.list_item, PostViewHolder.class, ref.child("posts").child(uid)) {
#Override
protected void populateViewHolder(final PostViewHolder postViewHolder, final TextPost textPost, int i) {
postViewHolder.content.setText(textPost.getContent());
postViewHolder.author.setText(textPost.getAuthor());
postViewHolder.score.setText(textPost.getScore());
postViewHolder.time.setText(textPost.getTime());
if (textPost.getImgLink() != null && !textPost.getImgLink().equals("")) {
Log.i(TAG, "Setting image");
new Thread(new Runnable() {
#Override
public void run() {
try {
final Bitmap pic = bitmapFromUrl(textPost.getImgLink());
postViewHolder.img.post(new Runnable() {
#Override
public void run() {
postViewHolder.img.setImageBitmap(pic);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
...
feed.setAdapter(adapter);
I just started learning how to work with worker threads for network activities off the main UI thread so I assume I messed that up? I've gone through the logic over and over in my head and i can't seem to figure out what's going wrong here.
EDIT: I tried using AsyncTask instead of Threads and the problem persists. sos
All I had to do was set the ImageView drawable to null before populating the ImageView.
Like this:
#Override
protected void populateViewHolder(final ViewHolder v, final Object o, int i) {
//populate views
v.content.setText("...");
//Set imageview to null
v.imageview.setImageDrawable(null);*
if (o.getImageLink() != null && !o.getImageLink.equals("")) {
// Start AsyncTask to get image from link and populate imageview
new DownloadImageTask().execute(o.getImgLink(), v.imageview);
}
}
I have a recyclerview which works as expected. I have a button in the layout that fills the list. The button is supposed to make a async call, and on result, I change the button's look. All this happens fine.
But when I click on the button and scroll down the list fast, the async call's result updates the new view's button(the view that is in place of the old one). How do I handle this? Can I have a handle on when a particular view gets reused?
Update :
Code piece of the adapter class that does the async call and the updation of ui.
#Override
public void onBindViewHolder(CommentsViewHolder holder, int position) {
try {
Comments comment = comments.get(position);
holder.bindView(comment,position);
}
catch(Exception ex){ex.printStackTrace();}
}
#Override
public int getItemCount() {
if(comments==null)
{return 0;}
return comments.size();
//return comments.length();
}
public class CommentsViewHolder extends RecyclerView.ViewHolder {
TextView score ;
TextView commentText;
TextView commentTime;
TextView avatarId;
ImageButton minusOne;
ImageButton plusOne;
ParseObject model;
public CommentsViewHolder(View itemView) {
super(itemView);
//itemView.setBackgroundColor(Color.DKGRAY);
minusOne =(ImageButton)itemView.findViewById(R.id.decScore);
plusOne =(ImageButton)itemView.findViewById(R.id.incScore);
commentText = (TextView)itemView.findViewById(R.id.comment);
score = (TextView)itemView.findViewById(R.id.commentScore);
commentTime =(TextView)itemView.findViewById(R.id.commentTime);
avatarId = (TextView)itemView.findViewById(R.id.ivUserAvatar);
}
public void bindView(Comments comment, int position) {
commentText.setText(comment.getCommentText());
score.setText(Integer.toString(comment.getScore()));
String timeText = DateUtils.getRelativeTimeSpanString( comment.getCreatedAt().getTime(), System.currentTimeMillis(), DateUtils.SECOND_IN_MILLIS).toString();
timeText = timeText.replace("hours","hrs");
timeText = timeText.replace("seconds","secs");
timeText = timeText.replace("minutes","mins");
commentTime.setText(timeText);
int commentHandler = comment.getCommenterHandle();
String commenterNumber = "";
if(commentHandler==0)
{
commenterNumber = "OP";
}
else{
commenterNumber = "#"+commentHandler;
}
avatarId.setText( commenterNumber);
model = comment;
String choice = "none";
minusOne.setEnabled(true);
plusOne.setEnabled(true);
minusOne.setVisibility(View.VISIBLE);
plusOne.setVisibility(View.VISIBLE);
for (ParseObject choiceIter : choices) {
if ((choiceIter.getParseObject("comment").getObjectId()).equals(comment.getObjectId())) {
choice = choiceIter.getString("userChoice");
break;
}
}
Log.i("debug",comment.getCommentText()+" "+comment.getScore()+" "+choice);
switch (choice) {
case "plusOne":
Log.i("darkplus","setting darkplus");
plusOne.setImageResource(R.drawable.ic_add_circle_black_18dp);
plusOne.setOnClickListener(reversePlusOneOnClickListener);
//minusOne.setOnClickListener(minusOneOnClickListener);
minusOne.setVisibility(View.GONE);
break;
case "minusOne":
Log.i("darkminus","setting darkminus");
minusOne.setImageResource(R.drawable.ic_remove_circle_black_18dp);
minusOne.setOnClickListener(reverseMinusOneOnClickListener);
//plusOne.setOnClickListener(plusOneOnClickListener);
plusOne.setVisibility(View.GONE);
break;
case "none":
Log.i("darkregular","setting regular");
minusOne.setImageResource(R.drawable.ic_remove_black_18dp);
plusOne.setImageResource(R.drawable.ic_add_black_18dp);
plusOne.setOnClickListener(plusOneOnClickListener);
minusOne.setOnClickListener(minusOneOnClickListener);
break;
}
}
View.OnClickListener reversePlusOneOnClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!FourUtils.isConnected(v.getContext())) {
return;
}
minusOne.setEnabled(false);
plusOne.setEnabled(false);
model.increment("plusOne", -1);
model.increment("score", -1);
model.saveEventually(new SaveCallback() {
#Override
public void done(ParseException e) {
if (e == null) {
ParseQuery<ParseObject> query = ParseQuery.getQuery("CommentChoice");
query.whereEqualTo("user", ParseUser.getCurrentUser());
query.whereEqualTo("comment", model);
query.fromPin(Four.COMMENT_CHOICE);
query.getFirstInBackground(new GetCallback<ParseObject>() {
#Override
public void done(ParseObject parseObject, ParseException e) {
if (e == null) {
if (parseObject == null) {
parseObject = ParseObject.create("CommentChoice");
parseObject.put("comment", model);
parseObject.put("user", ParseUser.getCurrentUser());
}
parseObject.put("userChoice", "none");
parseObject.pinInBackground(Four.COMMENT_CHOICE, new SaveCallback() {
#Override
public void done(ParseException e) {
if (e == null) {
score.setText(Integer.toString(model.getInt("score")));
//votes.setText((model.getInt("minusOne") + model.getInt("plusOne")) + " votes");
minusOne.setVisibility(View.VISIBLE);
plusOne.setImageResource(R.drawable.ic_add_black_18dp);
plusOne.setOnClickListener(plusOneOnClickListener);
minusOne.setEnabled(true);
plusOne.setEnabled(true);
// minusOne.setOnClickListener(minusOneOnClickListener);
BusProvider.getInstance().post(new NewCommentChoicesAdded());
} else {
e.printStackTrace();
}
}
});
}
else{e.printStackTrace();}
}
});
} else {
e.printStackTrace();
Log.i("plus1 error", e.getMessage());
}
}
});
}
};
When the async code is done, you should update the data, not the views. After updating the data, tell the adapter that the data changed. The RecyclerView gets note of this and re-renders your view.
When working with recycling views (ListView or RecyclerView), you cannot know what item a view is representing. In your case, that view gets recycled before the async work is done and is assigned to a different item of your data.
So never modify the view. Always modify the data and notify the adapter. bindView should be the place where you treat these cases.
Chet Haase from Google discusses your exact issue in this DevBytes video.
In short, the framework need to be notified that one of the Views is in "transient" state. Once notified, the framework will not recycle this View until its "transient" flag cleared.
In your case, before you execute the async action, call setHasTransientState(true) on the child View that should change when the async action completes. This View will not be recycled until you explicitly call setHasTransientState(false) on it.
Offtopic:
It looks like you might be manipulating UI elements from background threads. Don't do that! If you can have a reference to Activity then use its runOnUiThread(Runnable action) API instead. If getting a reference to Activity is difficult, you can obtain UI thread's Handler and use its post(Runnable action) API.
Without code to look at, this is going to be difficult (if not impossible) for people to provide an exact answer. However, based on this description it sounds as though your async network loading (using an AsyncTask or custom Loader?) is not specifically tied to an element being tracked by your adapter. You'll need to have some way of tying the two together since the child View objects shown by the RecyclerView are re-used to be more efficient. This also means that if a View is being reused and there is an active async operation tied to it, that async operation will need to be canceled. Otherwise you'll see what you see now: the wrong child View being updated with content from an older async call.
I have a single fragment loaded in an activity using fragment manager inside a container layout. Inside my activity i start a service to connect to other bluetooth device and communicate with it by sending and receiving certain data. Everything works fine when the app is open and the service is connected to bluetooth device.
But when i hit back button and reopen my app , though my service is still connected to other bluetooth device the fragment i'm using to display the same shows it is in disconnected state.
I put a check before setting the text to my fragment's child textview using
fragment.isVisible()
and it returned false (?)
So , i think if i'm not wrong , the activity is creating a different instance of fragment over my original fragment everytime i open the app.
Here is the onCreate() of my activity..
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bt_dashboard);
System.out.println("OnCreate() of BTDashboardActivity........");
if (findViewById(R.id.fragmentContainer) != null) {
if (savedInstanceState != null) {
System.out.println("ListFragmetn allready exist...");
return;
}
System.out.println("adding listfragment to view...");
listFragment = new BTDashboardListFragment();
listFragment.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction()
.add(R.id.fragmentContainer, listFragment).commit();
}
}
EDIT
here is code for onStart() of activity..
#Override
public void onStart() {
super.onStart();
if (!mBluetoothAdapter.isEnabled()) {
Intent enableIntent = new Intent(
BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableIntent,
Constants.REQUEST_ENABLE_BT);
} else {
if (mBluetoothService == null) {
startBluetoothService();
Log.d(TAG, "Chat service Started...############");
showDisconnectedUI();
System.out.println("showing disconnceted status to the user..");
} else {
if (mBluetoothService.getState() == Constants.STATE_CONNECTED) {
System.out.println("showing connection status to the user..");
showConnectedUI();
} else {
System.out.println("showing disconnceted status to the user..");
showDisconnectedUI();
}
}
}
}
And this is code to set data to fragment's child views..
protected void handleResponse(String readMessage) {
System.out.println("response from device: " + readMessage);
if (readMessage != null) {
if (listFragment != null && listFragment.isInLayout()) {
System.out.println("List fragment is found...");
System.out.println("Setting response text to listFragment...");
if(listFragment.isVisible()) // prints not visible after reopening the app
System.out.println("listFragment is visible ...");
else
System.out.println("listFragment is not visible...");
listFragment.setResponseText(readMessage);
}
}
Any help is appreciated..
Making the fragment instance static solved my problem.
I know this is just a workaround
but works like a charm for me right now..
I've run into this error before, but thought it was some mistake by the strict mode system. However, it apparently was right as I sadly found out now. :(
My programm is made of one Activity and loads of Fragments. I have a NetworkWorker fragment, which starts URL requests like this:
public void startURLRequest(Fragment target, String url, String message)
{
if (asyncTask != null) asyncTask.cancel(true);
asyncTask = new FragmentHttpHelper(url, message, target);
asyncTask.doInBackground();
return;
}
FragmentHttpHelper is a custom inner class derived from AsyncTask:
private class FragmentHttpHelper extends AsyncTask<Void, Void, String>
{
//...
#Override
protected String doInBackground(Void... params)
{
if (CheckInternet())
{
try
{
URL myURL = new URL(url);
httpClient = new DefaultHttpClient();
if (this.message == null)
{
httpRequest = new HttpGet(myURL.toExternalForm());
}
else
{
httpRequest = new HttpPost(myURL.toExternalForm());
HttpEntity myEntity = new StringEntity(message, "UTF-8");
((HttpPost) httpRequest).setEntity(myEntity);
}
// and so on...
}
//catches
finally
{
// auf jeden Fall Verbindung beenden
if (httpRequest != null) httpRequest.abort();
// if (httpClient != null) httpClient.close();
}
}
else
{
showDialog(getString(R.string.net_notify_no_network), target);
}
//...
}
/**
* gets called after AsyncTask has finished
*/
protected void onPostExecute(String result)
{
if (target == null)
{
((NetworkWorkerListener) getActivity()).onDownloadHasFinished((!result.contentEquals(ERROR)), result);
}
else
{
((NetworkWorkerListener) target).onDownloadHasFinished((!result.contentEquals(ERROR)), result);
}
}
}
NetworkWorkerListener is just an interface for a callback on the Fragment which started the URL request. This class has always worked fine when I used it in my 2.2 app. I would derive it in my Activities then.
Now, if a menu item is selected, another worker Fragment starts the URL request via the above method and opens a loading dialog:
FragmentManager fragmentManager = getFragmentManager();
NetworkWorker network = (NetworkWorker) fragmentManager.findFragmentByTag(TabletMain.NETWORK);
if (network == null) return WorkerFeedback.NO_NETWORK_WORKER;
myDialog = LoadingDialog.createInstance(getString(R.string.notify_download), this);
myDialog.show(fragmentManager, TabletMain.ONETIME);
network.startURLRequest(this, someurl, null);
At least that's what supposed to happen.
Instead, when I click the menu item, my app freezes and no loading dialog is shown until. Next happening is the reaction to the end of the download (or, in my case an error message, as I am sending nonsense strings). Meaning onPostExecute() was reached.
I feel really stuck now - is it not possible to use AsyncTask with Fragments? Or did I do something wrong?
Thanks for your help,
jellyfish
Don't call doInBackground directly, call execute instead (on the async task)