Not getting value assigned outside the function - android

I am not getting a value assigned to the variable name outside the function. Tried with initializing value in the start also.
public class HomeActivity extends BaseActivity {
FirebaseAuth mFirebaseAuth;
TextView tVWelcome;
String name,email;
private static final String TAG = "HomeActivity";
private FirebaseAuth.AuthStateListener mAuthStateListener;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
tVWelcome = (TextView) findViewById(R.id.tvWelcome);
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
String userid = user.getUid();
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Users");
ref.child(userid).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
name = dataSnapshot.child("fname").getValue().toString();
Log.d(TAG,"Name11"+name);
email= dataSnapshot.child("email").getValue().toString();
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
String text="Welcome "+name+"!";
Log.d(TAG,"text:"+text);
SpannableString ss=new SpannableString(text);
ClickableSpan clickableSpan=new ClickableSpan() {
#Override
public void onClick(View view) {
Intent ProfileIntent = new Intent(HomeActivity.this, ProfileActivity.class);
startActivity(ProfileIntent);
}
};
ss.setSpan(clickableSpan,8,text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
tVWelcome.setText(ss);
tVWelcome.setMovementMethod(LinkMovementMethod.getInstance());
}
}
Also, output wise, it's setting TextView value before the assignment
Output Log:
2019-10-18 22:13:33.738 10399-10399/com.example.treasurehuntapp D/HomeActivity: text:Welcome null!
2019-10-18 22:13:33.763 10399-10461/com.example.treasurehuntapp D/FA: Logging event (FE): screen_view(_vs), Bundle[{firebase_event_origin(_o)=auto, firebase_previous_class(_pc)=LoginActivity, firebase_previous_id(_pi)=4300107314116723691, firebase_screen_class(_sc)=HomeActivity, firebase_screen_id(_si)=4300107314116723692}]
2019-10-18 22:13:36.556 10399-10399/com.example.treasurehuntapp D/HomeActivity: Name11Joe
Please tell me what I am doing wrong.

My advice is to extract the logic that retrieves data from firebase and also a method to set the data to the textView. Below is an example:
private void loadData(String userId) {
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Users");
ref.child(userId).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot != null)
renderDataToView(dataSnapshot.child("fname").getValue().toString());
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
Second method to set data to the view:
private void renderDataToView (String name) {
if (name != null ) {
<yourTextView>.setText(name);
}
}

Related

userid which in the private void sendMessage part. please help .child(userid); and chatRef.child("id").setValue(userid); [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have a problem in the userid in sendMessage method.
.child(userid); and chatRef.child("id").setValue(userid);
Also I used userid in my RegisterActivity page but I think it is not about this.
error: cannot find symbol variable userid
public class MessageActivity extends AppCompatActivity {
CircleImageView profile_image;
TextView username;
FirebaseUser fuser;
DatabaseReference reference;
ImageButton btn_send;
EditText text_send;
MessageAdapter messageAdapter;
List<Chat> mchat;
RecyclerView recyclerView;
Intent ıntent;
ValueEventListener seenListener;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_message);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(MessageActivity.this,StartActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
}
});
recyclerView = findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext());
linearLayoutManager.setStackFromEnd(true);
recyclerView.setLayoutManager(linearLayoutManager);
profile_image = findViewById(R.id.profile_image);
username = findViewById(R.id.username);
btn_send = findViewById(R.id.btn_send);
text_send = findViewById(R.id.text_send);
ıntent = getIntent();
final String userid = ıntent.getStringExtra("userid");
fuser = FirebaseAuth.getInstance().getCurrentUser();
btn_send.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String msg = text_send.getText().toString();
if(!msg.equals("")){
sendMessage(fuser.getUid(),userid,msg);
}else{
Toast.makeText(MessageActivity.this,"You cannot send empty message", Toast.LENGTH_SHORT).show();
}
text_send.setText("");
}
});
reference = FirebaseDatabase.getInstance().getReference("Users").child(userid);
reference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
User user = dataSnapshot.getValue(User.class);
username.setText(user.getUsername());
if(user.getImageURL().equals("default")){
profile_image.setImageResource(R.mipmap.ic_launcher);
}else{
Glide.with(getApplicationContext()).load(user.getImageURL()).into(profile_image);
}
readMessages(fuser.getUid(),userid,user.getImageURL());
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
seenMessage(userid);
}
private void seenMessage(final String userid){
reference = FirebaseDatabase.getInstance().getReference("Chats");
seenListener = reference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot snapshot : dataSnapshot.getChildren()){
Chat chat = snapshot.getValue(Chat.class);
if(chat.getReceiver().equals(fuser.getUid())&& chat.getSender().equals(userid)){
HashMap<String,Object> hashMap = new HashMap<>();
hashMap.put("isseen",true);
snapshot.getRef().updateChildren(hashMap);
}
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
private void sendMessage(String sender,String receiver, String message){
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
HashMap<String,Object> hashMap = new HashMap<>();
hashMap.put("sender",sender);
hashMap.put("receiver",receiver);
hashMap.put("message",message);
hashMap.put("isseen",false);
reference.child("Chats").push().setValue(hashMap);
final DatabaseReference chatRef = FirebaseDatabase.getInstance().getReference("Chatlist")
.child(fuser.getUid())
.child(userid);
chatRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
if(!dataSnapshot.exists()){
chatRef.child("id").setValue(userid);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
private void readMessages(final String myid, final String userid, final String imageURL){
mchat = new ArrayList<>();
reference = FirebaseDatabase.getInstance().getReference("Chats");
reference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
mchat.clear();
for (DataSnapshot snapshot : dataSnapshot.getChildren()){
Chat chat = snapshot.getValue(Chat.class);
if(chat.getReceiver().equals(myid) && chat.getSender().equals(userid) ||
chat.getReceiver().equals(userid) && chat.getSender().equals(myid)){
mchat.add(chat);
}
messageAdapter = new MessageAdapter(MessageActivity.this,mchat,imageURL);
recyclerView.setAdapter(messageAdapter);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
private void status(String status){
reference = FirebaseDatabase.getInstance().getReference("Users").child(fuser.getUid());
HashMap<String,Object> hashMap = new HashMap<>();
hashMap.put("status",status);
reference.updateChildren(hashMap);
}
#Override
protected void onResume() {
super.onResume();
status("online");
}
#Override
protected void onPause() {
super.onPause();
reference.removeEventListener(seenListener);
status("offline");
}
}
You have initalized in onCreate
final String userid = intent.getStringExtra("userid");
Instead initialize the string userid as an instance variable accessible to the whole class.
RecyclerView recyclerView;
Intent intent;
ValueEventListener seenListener;
String userid = "";
and in onCreate like this,
userid = intent.getStringExtra("userid");
Check for null when using userid always,
if(userid!=null) {
//do something
}
else {
Log.d("ClassName","userid is null");
}
sir in sendMessage function parameters you change the name userid into sender, so you need to write sender instead of userId. check you function parameters

How to compare data in firebase?

I want to read back the data in Firebase which is medical = "Diabetes" which key in by the user.If this user has the medical history of diabetes will display something not allow the user to buy. Anyone can teach me how to write this condition in the android studio?
Firebase data structure
public class Pain_and_Fever extends AppCompatActivity implements View.OnClickListener{
private Button btnSubmit, btnCancel;
private String userID;
Query query;
//add Firebase Database stuff
private FirebaseDatabase mFirebaseDatabase;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private DatabaseReference myRef;
#Override
protected void onCreate(#Nullable final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pain_and__fever);
btnSubmit = (Button) findViewById(R.id.bttnsubmit);
btnCancel = (Button) findViewById(R.id.bttncancel);
//declare the database reference object. This is what we use to access the database.
//NOTE: Unless you are signed in, this will not be useable.
mAuth = FirebaseAuth.getInstance();
mFirebaseDatabase = FirebaseDatabase.getInstance();
final FirebaseUser user = mAuth.getCurrentUser();
userID = user.getUid();
myRef = mFirebaseDatabase.getReference();
btnSubmit.setOnClickListener(this);
btnCancel.setOnClickListener(this);
query = myRef.orderByChild("medical").equalTo("Diabetes");
}
private void submit(){
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()){
for (DataSnapshot issue : dataSnapshot.getChildren()){
UserInformation uInfo = issue.getValue(UserInformation.class);
if (uInfo.getMedical().equals("Diabetes")){
startActivity(new Intent(getApplicationContext(),Medicine.class));
}else{
myRef.child("Medicines").child("Pain and Fever").child(userID).setValue("Acetaminophen");
startActivity(new Intent(getApplicationContext(),Medicine.class));
}
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
#Override
public void onClick(View view) {
if (view == btnSubmit){
submit();
}
if (view == btnCancel){
startActivity(new Intent(this,Medicine.class));
}
}
}
Try this way, this works for me
Query chatRoomsQuery = mFirebaseDatabase.orderByChild("medical").equalTo("your value");
chatRoomsQuery.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
// dataSnapshot is the "issue" node with all children with id 0
search_list=new ArrayList<SearchModel>();
for (DataSnapshot issue : dataSnapshot.getChildren()) {
// do something with the individual "issues"
UserRegisterModel mModel = issue.getValue(UserRegisterModel.class);
if(mModel.getArea().equals(sel_area))
hidepDialog();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}

JUnit testing on class with firebase

I'm trying to JUnit test this class:
public class WeekListActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
private ArrayList<String> weekList = new ArrayList<>();
private ArrayAdapter<String> adapter;
ListView weekListView;
Button AddWeekButton;
EditText InsertWeekEditText;
String weekNumber;
String subjectName;
String subjectCode;
User user;
DatabaseReference mDatabase;
DatabaseReference mRef;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_week_list);
FirebaseApp.initializeApp(this);
FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();
FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
Intent moveToDetailIntent = this.getIntent();
subjectName = moveToDetailIntent.getExtras().getString("Subject");
subjectCode = moveToDetailIntent.getExtras().getString("Fagkode");
mDatabase = FirebaseDatabase.getInstance().getReference().child("Studentfag").child(subjectCode).child("Week");
mRef = FirebaseDatabase.getInstance().getReference().child("Users").child(firebaseUser.getUid()).child("User info");
weekListView = (ListView) findViewById(R.id.WeekListView);
AddWeekButton = (Button) findViewById(R.id.AddWeekButton);
InsertWeekEditText = (EditText) findViewById(R.id.InsertWeek);
String userID = firebaseUser.getUid();
mRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
user = dataSnapshot.getValue(User.class);
if (user.isStudent){
View a = weekListView;
a.setMinimumHeight(80);
View b = AddWeekButton;
b.setVisibility(View.GONE);
View c = InsertWeekEditText;
c.setVisibility(View.GONE);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
adapter = new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_list_item_1, weekList);
weekListView.setAdapter(adapter);
weekListView.setOnItemClickListener(this);
AddWeekButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
weekNumber= InsertWeekEditText.getText().toString();
mDatabase.child(weekNumber).child("id").setValue(weekNumber);
}
});
mDatabase.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String week = dataSnapshot.getKey().toString();
weekList.add("Week: " + week);
adapter.notifyDataSetChanged();
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
//Urelevante metoder for oss.
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {}
#Override
public void onCancelled(DatabaseError databaseError) {}
});
}
The problem is that when I build a new activity in my setup method it complains because this sentence:
mRef = FirebaseDatabase.getInstance().getReference().child("Users").child(firebaseUser.getUid()).child("User info");
is not able to build, when we don't have a firebase user.
Therefore I tried to Mock a firebaseuser in my testclass. The question is, how can I tell the class that it should use the mocked firebaseuser in onCreate? Is there a way to "send" the mocked object over? Thanks!
The beginning of my setup method:
#Before
public void setUp() throws Exception {
Intent i = new Intent();
i.putExtra("Subject", "Matematikk 1");
i.putExtra("Fagkode", "TMA4100");
FirebaseUser mockFirebaseUser = mock(FirebaseUser.class);
when(mockFirebaseUser.getUid()).thenReturn("uTZpVPPz8NT2LOvP4ufjs1L6r3P2");
Activity activity = Robolectric.buildActivity(WeekListActivity.class).withIntent(i).create().get();
}
As usual, I suggest everybody to not mix presentation and storage code. And this is a question for another topic.
And here the trick how you can achieve what you want.
First, extract method for Firebase initialisation and providing FirebaseAuth:
#VisibleForTest
#NonNull
FirebaseAuth initAndReturnFirebaseAuth() {
FirebaseApp.initializeApp(this);
FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();
}
Second, create test activity and override this method:
public class TestWeekListActivity extends WeekListActivity {
#Override
#NonNull
FirebaseAuth initAndReturnFirebaseAuth() {
FirebaseAuth authMock = mock(FirebaseAuth.class);
when(authMock.getCurrentUser()).thenReturn(mockFirebaseUser);
return authMock;
}
}
And then use test activity in test instead of you real activity.
Hope it helps!

Populate textView from firebase database

I am trying to populate a TextView from a firebase database. Here is the sample json file.
{
"Player" : {
"Club" : "Valley Rovers",
"Name" : "John Murphy"
}
}
Here is my android code:
public class MainActivity extends AppCompatActivity {
private TextView mPlayer;
private DatabaseReference mDatabase;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPlayer = (TextView) findViewById(R.id.player);;
mDatabase = FirebaseDatabase.getInstance().getReference("Player").child("Name");
final String player = mDatabase.push().getKey();
mDatabase.child("Name").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
// Check for null
if (player == null) {
Log.e(TAG, "User data is null!");
return;
}
mPlayer.setText(player);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
I want to add the name John Murphy to the firebase database and doing so the TextView mPlayer will populate with John Murphy.
You are getting the child "Name" twice on your code (leading to Player/Name/Name). Remove it from the mDatabase initialization:
mDatabase = FirebaseDatabase.getInstance().getReference("Player");
And you're never really getting the value from the DataSnapshot received. To do so, use this:
mDatabase.child("Name").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String playerName = dataSnapshot.getValue(String.class);
mPlayer.setText(playerName);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
In case someone wants to use POJO approach to this problem in future, follow the example below:
Remove the child("Name") from mDatabase.child("Name").addValueEventListener(new ValueEventListener()
unless you want to fetch the "Name" child only. Also remember to effect the correction made on mDatabase initialization.
mDatabase.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
// String playerName = dataSnapshot.getValue(String.class);
PlayerModel playerModel = dataSnapshot.getValue(PlayerModel.class);
((TextView)findViewById(R.id.textviewName)).setText(playerModel.getName());
((TextView)findViewById(R.id.textviewClub)).setText(playerModel.getClub());
// mPlayer.setText(playerName);
}

How to transfer data that are stored in TextView in an activity for calculation in another activity?

This is my DisplayBooks class:
public class DisplayBooks extends AppCompatActivity
private FirebaseAuth myAuth;
private DatabaseReference myDatabase;
private TextView book1display,book2display,book3display,book4display,book5display,book6display,book7display;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_books);
myAuth = FirebaseAuth.getInstance();
myDatabase = FirebaseDatabase.getInstance().getReference();
book1display = (TextView) findViewById(R.id.book1display);
book2display = (TextView) findViewById(R.id.book2display);
book3display = (TextView) findViewById(R.id.book3display);
book4display = (TextView) findViewById(R.id.book4display);
book5display = (TextView) findViewById(R.id.book5display);
book6display = (TextView) findViewById(R.id.book6display);
book7display = (TextView) findViewById(R.id.book7display);
String user_id = myAuth.getCurrentUser().getUid();
DatabaseReference userid_database = myDatabase.child(user_id);
DatabaseReference book1 = userid_database.child("Books").child("Book 1").child("Page");
DatabaseReference book2 = userid_database.child("Books").child("Book 2").child("Page");
DatabaseReference book3 = userid_database.child("Books").child("Book 3").child("Page");
DatabaseReference book4 = userid_database.child("Books").child("Book 4").child("Page");
DatabaseReference book5 = userid_database.child("Books").child("Book 5").child("Page");
DatabaseReference book6 = userid_database.child("Books").child("Book 6").child("Page");
DatabaseReference book7 = userid_database.child("Books").child("Book 7").child("Page");
book1.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String text = dataSnapshot.getValue(String.class);
book1display.setText(text);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
book2.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String text = dataSnapshot.getValue(String.class);
book2display.setText(text);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
book3.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String text = dataSnapshot.getValue(String.class);
book3display.setText(text);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
book4.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String text = dataSnapshot.getValue(String.class);
book4display.setText(text);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
book5.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String text = dataSnapshot.getValue(String.class);
book5display.setText(text);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
book6.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String text = dataSnapshot.getValue(String.class);
book6display.setText(text);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
book7.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String text = dataSnapshot.getValue(String.class);
book7display.setText(text);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
Below is an example output. I want to get the numbers that in the picture for make a calculation in the another activity.
This is my other activity:
public class HowMuch extends AppCompatActivity
private TextView howmuch1,howmuch2;
TextView hay;
Button button2;
private DatabaseReference myDatabase;
private FirebaseAuth myAuth;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_how_much);
myAuth=FirebaseAuth.getInstance();
myDatabase= FirebaseDatabase.getInstance().getReference();
howmuch1=(TextView)findViewById(R.id.howmuch1);
howmuch2=(TextView)findViewById(R.id.howmuch2);
hay=(TextView)findViewById(R.id.hay);
String user_id= myAuth.getCurrentUser().getUid();
DatabaseReference userid_database=myDatabase.child(user_id);
DatabaseReference book1=userid_database.child("Books").child("Book 1").child("Page");
DatabaseReference book2=userid_database.child("Books").child("Book 2").child("Page");
book1.addValueEventListener(new ValueEventListener()
{
#Override
public void onDataChange(DataSnapshot dataSnapshot)
{
String text= dataSnapshot.getValue(String.class);
int value=Integer.parseInt(text);
int value1=value/2;
String x= String.valueOf(value1);
howmuch1.setText(x);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
If I write like that for book2, book3 etc. I can make calculattion but I also want to make sum of these values. Hovewer, I couldn't transfer values of them to make new calculation. I tried to transfer values from my DisplayBooks activity writing this:
public void transfer(View view){
String value = book1display.getText().toString();
Intent intent = new Intent(DisplayBooks.this, HowMuch.class);
intent.putExtra("key",value);
startActivity(intent);
}
And adding this to HowMuch class:
String value = getIntent().getExtras().getString("key");
hay.setText(value);
But it didn't work.
It should be
String value = getIntent().getStringExtra("key");
hay.setText(value);
Edit: links for reference:
Send data
https://developer.android.com/training/sharing/send.html
Receive data https://developer.android.com/training/sharing/receive.html
Bundle receivedBundle = getIntent().getExtras();
if (receivedBundle != null) {
String extraString = receivedBundle.getString("key");
int extraInt = receivedBundle.getInt("key");
}

Categories

Resources