0

The data gets called perfectly from the parse because i can see the correct value of String caption in the Toast that is inside the method done(); but as soon as it gets out of the done(), The second Toast displays null.

So i tried passing the value of caption to the intent inside the done() but it doesnt get passed to the other SingleItemView.class

I've checked if all the spellings in the other class' getExtraString are the same and they are..

Forget the Intent Part, if anyone could tell me why the correct value gets shown in the first Toast message but the second Toast message shows null, that'd be great

Can anyone tell me what i might be doing wrong here?

Thanks

'caption' is a public string.

final  Intent i = new Intent(context, SingleItemView.class); 
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(SchoolClass);

query.getInBackground(post.get(position).getObjectId(), new GetCallback<ParseObject>() {
    public void done(ParseObject object, ParseException e) {
        // TODO Auto-generated method stub
        caption = object.getString("caption");
        i.putExtra("Caption", caption);
        Toast.makeText(context, caption, Toast.LENGTH_LONG).show();
    }
});

Toast.makeText(context, caption, Toast.LENGTH_LONG).show();
context.startActivity(i);
ᴛʜᴇᴘᴀᴛᴇʟ
  • 4,466
  • 5
  • 39
  • 73
matlab hater
  • 163
  • 3
  • 12
  • object.getString("caption"); This caption is the name of the table in the database "Caption is just the String name that you pass in the intent" This is how I'm receiving it in the other class caption = i.getStringExtra("Caption"); "caption" is a public string that get's the value of the data from Parse – matlab hater Jan 06 '17 at 01:04
  • lol forget about the intent part. can you explain why the toast inside the done() can print the value but the second Toast shows null? – matlab hater Jan 06 '17 at 01:07
  • 1
    It doesn't show in the second toast bc the value hasn't been set into intent yet. I'm assuming the done() method runs on a different thread. Things are not executing in the order you think. At the point when the second toast call is executed, the done() method hasn't run. – Jeffrey Blattman Jan 06 '17 at 01:18
  • yeah you're right about the different thread, is there anyway can i make it that second Toast lines run after done() gets executed? – matlab hater Jan 06 '17 at 01:21
  • yep, The single query and calling it inside worked!! Thanks. – matlab hater Jan 06 '17 at 01:26

2 Answers2

2

done is call back method which will execute when getInBackground method execution completed to return result on Main UI Thread.

To get it work use startActivity inside done method.

public void done(ParseObject object, ParseException e) {
  .....
  Intent i = new Intent(context, SingleItemView.class); 
  i.putExtra("Caption", caption);
  context.startActivity(i);  //<< start Activity here
}
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • I can't start the activity there because there are other strings that i need to pass in the same intent and before i do that, i have to use other parse queries....is there a way that i can store that value caption in some other string and pass it to the intent at a later time? – matlab hater Jan 06 '17 at 01:09
  • @matlabhater: `pass it to the intent at a later time` when? – ρяσѕρєя K Jan 06 '17 at 01:10
  • after i've called other parse queries....right before i actually start the next activity? – matlab hater Jan 06 '17 at 01:13
  • 1
    @matlabhater: instead of multiple queries why not executing one by one? – ρяσѕρєя K Jan 06 '17 at 01:15
  • Forget the intent part, can you tell me how i can make the caption value appear in the second Toast? the one that is outside the done() – matlab hater Jan 06 '17 at 01:16
  • @matlabhater: :) when `caption value appear in the second Toast` then you will also startActivity with `caption` problem is same. because both Toast and startAcivity lines are executing before done method – ρяσѕρєя K Jan 06 '17 at 01:17
  • @matlabhater: you can do it like as `Handler handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { Toast.makeText(context, caption, Toast.LENGTH_LONG).show(); context.startActivity(i); } }, 1500);` but this is not good way – ρяσѕρєя K Jan 06 '17 at 01:20
  • is there anyway can i make it that second Toast and the startActivity lines run after done() gets executed? – matlab hater Jan 06 '17 at 01:20
  • i'll try using just one query and starting the activity in the done() – matlab hater Jan 06 '17 at 01:23
  • yep, The single query and calling it inside worked!! Thanks – matlab hater Jan 06 '17 at 01:26
1

You are on the right track. just print log for "caption" string. So you can get better idea about return value.

For send data from one activity to another one, Refere this link, you'll get better idea

Community
  • 1
  • 1
Bhoomika Patel
  • 1,895
  • 1
  • 13
  • 30