public class SearchActivity extends AppCompatActivity {
private EditText InputString;
private Button btnScan, btnSearch;
private DatabaseReference mFirebaseDatabaseReference;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
InputString = (EditText) findViewById(R.id.input_search);
btnScan = (Button) findViewById(R.id.btn_Scan);
btnSearch = (Button) findViewById(R.id.btn_Search);
mFirebaseDatabaseReference = FirebaseDatabase.getInstance().getReference();
btnSearch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final String checkString = "InputString";
Query query = mFirebaseDatabaseReference.child("Stock").orderByChild("productName").orderByChild("productId").equalTo(checkString);
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
setContentView(R.layout.activity_product__detail_);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
});
}
}
I want to do let the users search if the users key in the "Product name", "Product code" or using "barcode" to search the product and then the application will display the product detail.For example, the users enter "mild steel", the application will display the product name, product code, barcode and the quantity
Here sample code
final String checkString = "inputString";
Query query = mDatabase.child("user").orderByChild("productName").equalTo(checkString);
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
}
mDatabase.child("e-inventory-store").chile("Stock")
.addListenerForSingleValueEvent(new
ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Map<String, String> map = (Map<String, String>) dataSnapshot.getValue();
String productName = map.get("productName");
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Try this one,
Related
I am still learning to use Android Studio, I have difficulty to set the status of the togglebutton in accordance with the realtimedatabase in Firebase, when exiting the application and entering the button again is off when the database is on,
so what I want to ask, when the database is activated, then togglebutton on Android is active and vice versa,
Thank you.
public class MainActivity extends AppCompatActivity {
TextView teks;
DatabaseReference dref;
String status;
ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
teks=(TextView)findViewById(R.id.text1);
Typeface customfont=Typeface.createFromAsset(getAssets(),"font/jd_led3.ttf");
teks.setTypeface(customfont);
teks=(TextView)findViewById(R.id.text1);
dref = FirebaseDatabase.getInstance().getReference();
dref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
status=dataSnapshot.child("Suhu").getValue().toString();
teks.setText(status);
}
#Override
public void onCancelled(DatabaseError error) {
}
});
final ToggleButton button = (ToggleButton) findViewById(R.id.toggleButton);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FirebaseDatabase database = FirebaseDatabase.getInstance();
button.performHapticFeedback(HapticFeedbackConstants.KEYBOARD_TAP);
DatabaseReference myRef = database.getReference("RELAY1_STATUS");
if(button.isChecked()){
myRef.setValue("ON");
}else{
myRef.setValue("OFF");
}
}
});
final ToggleButton button1 = (ToggleButton) findViewById(R.id.toggleButton1);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FirebaseDatabase database = FirebaseDatabase.getInstance();
button1.performHapticFeedback(HapticFeedbackConstants.KEYBOARD_TAP);
DatabaseReference myRef = database.getReference("RELAY2_STATUS");
if(button1.isChecked()){
myRef.setValue("ON");
}else{
myRef.setValue("OFF");
}
}
});
final ToggleButton button2 = (ToggleButton) findViewById(R.id.toggleButton2);
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FirebaseDatabase database = FirebaseDatabase.getInstance();
button2.performHapticFeedback(HapticFeedbackConstants.KEYBOARD_TAP);
DatabaseReference myRef = database.getReference("RELAY3_STATUS");
if(button2.isChecked()){
myRef.setValue("ON");
}else{
myRef.setValue("OFF");
}
}
});
}
}
sorry if my post is a mess I am also the first time using stackoverflow
ON ONCREATE You will need need to add value event listener like
MyRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
status=dataSnapshot.child("RELAY2_STATUS").getValue().toString();
if(status.equals("ON"){
button1.isChecked(true)
}
#Override
public void onCancelled(DatabaseError error) {
}
});
dref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.d(TAG, "onDataChange: " + status);
status = dataSnapshot.child("RELAY1_STATUS").getValue().toString();
if (status.equals("ON")) {
a.setChecked(true);
} else {
a.setChecked(false);
}
}
#Override
public void onCancelled(DatabaseError error) {
Log.w(TAG, "onCancelled: ",error.toException() );
}
});
Hi guys I need help on a rather trivial problem that I can't solve, then I'm writing an app for android with android studio, what the app should do is read the contents of the TextView, text-only format without numbers or anything , and based on the content of the TextView should change image or text, I can not I tried to write this code but it does not work, could you please tell me where I am wrong and write me some code, thanks.
public class CashBackPage extends AppCompatActivity {
public FirebaseAuth mAuth;
public AdView adViewTop, adViewBot;
TextView mLogout;
TextView mTxtPhone;
TextView mPhone;
TextView mReadCash;
TextView mVersionApp;
TextView mCodeRappCash;
TextView mGeneralTextCash, mSendRequestText;
ImageButton mSendRequest, mCancel;
LinearLayout mbannerTop, mbannerBot;
FirebaseDatabase mDatabase;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cash_back_page);
mLogout = (TextView) findViewById(R.id.logoutbutn);
mTxtPhone = (TextView) findViewById(R.id.TextPhone);
mPhone = (TextView) findViewById(R.id.NumberPhTxt);
mReadCash = (TextView) findViewById(R.id.ReadCashBack);
mVersionApp = (TextView) findViewById(R.id.VersionApp);
mSendRequest = (ImageButton) findViewById(R.id.SendRequestBtn);
mCancel = (ImageButton) findViewById(R.id.CancelBtn);
mCodeRappCash = (TextView) findViewById(R.id.CodeDiscountCash);
mGeneralTextCash = (TextView) findViewById(R.id.textgeneralacash);
mSendRequestText = (TextView) findViewById(R.id.textsendrequest);
mSendRequestText.setVisibility(View.GONE);
mGeneralTextCash.setVisibility(View.GONE);
mAuth = FirebaseAuth.getInstance();
initFirebase();
//Set Orientation Portrait
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
getnumberprefs();
if (!isEmpty (String.valueOf(mCodeRappCash))){
String textgen = mCodeRappCash.getText().toString().trim();
if (textgen.equals("Request code sent...")){
mSendRequestText.setVisibility(View.VISIBLE);
mGeneralTextCash.setVisibility(View.GONE);
}else{
mSendRequestText.setVisibility(View.GONE);
mGeneralTextCash.setVisibility(View.VISIBLE);
}
}
}
#Override
protected void onStart() {
super.onStart();
// Read to the database CashBack
DatabaseReference myRef = mDatabase.getReference();
myRef.child("Utenti").child(mPhone.getText().toString()).child("CashBack").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
String cashback = dataSnapshot.getValue(String.class);
mReadCash.setText(cashback);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
// Read to the database Version App
myRef.child("Utenti").child(mPhone.getText().toString()).child("Version App").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
String versionapp = dataSnapshot.getValue(String.class);
mVersionApp.setText(versionapp);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
// Read to the database Code Cash Back
myRef.child("Utenti").child(mPhone.getText().toString()).child("RappCash Code").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
String coderappcash = dataSnapshot.getValue(String.class);
mCodeRappCash.setText(coderappcash);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
public void logoutclick(View view) {
Vibrator vib = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
vib.vibrate(30);
mAuth.signOut();
updateUI();
}
private void initFirebase() {
mDatabase = FirebaseDatabase.getInstance();
}
private void updateUI() {
FirebaseUser currentuser = mAuth.getCurrentUser();
if(currentuser == null){
Intent intTologin = new Intent(this, MainActivity.class);
finish();
startActivity(intTologin);
}
}
private void getnumberprefs() {
SharedPreferences numb = getSharedPreferences(Register.NUMB, MODE_PRIVATE);
String numberphn = numb.getString(Register.KEY_NUMB,null);
mPhone.setText(numberphn);
}
public void sendrequest(View view) {
Vibrator vib = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
vib.vibrate(30);
DatabaseReference myRef = mDatabase.getReference();
myRef.child("Utenti").child(mPhone.getText().toString()).child("RappCash Code").setValue("Request code sent...");
}
public void cancelrequest(View view) {
Vibrator vib = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
vib.vibrate(30);
new CountDownTimer(15000, 1000) {
public void onTick(long millisUntilFinished) {
DatabaseReference myRef = mDatabase.getReference();
myRef.child("Utenti").child(mPhone.getText().toString()).child("RappCash Code").setValue("Request code canceled...");
}
public void onFinish() {
DatabaseReference myRef = mDatabase.getReference();
myRef.child("Utenti").child(mPhone.getText().toString()).child("RappCash Code").setValue("#RappCashCode");
}
}.start();
}
private boolean isEmpty (String mCoins) {
return mCoins.contentEquals("#RappCashCode");
}
}
so only if the text is the same as "Request code sent ..." it changes image or text contained in another textview, I entered this code in both OnStart and OnCreate but in both cases it doesn't work.
I state that the TextView reads the content of a line of a real time database firebase and that it returns in the app what is written on the database
Edit:
Okay, relating to your code the problem is simple. TextView is checked for text, before is set.
You are firstly calling check on onCreate():
if (!isEmpty (String.valueOf(mCodeRappCash))){
String textgen = mCodeRappCash.getText().toString().trim();
if (textgen.equals("Request code sent...")){
mSendRequestText.setVisibility(View.VISIBLE);
mGeneralTextCash.setVisibility(View.GONE);
}else{
mSendRequestText.setVisibility(View.GONE);
mGeneralTextCash.setVisibility(View.VISIBLE);
}
}
And then you are calling on onStart():
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
String coderappcash = dataSnapshot.getValue(String.class);
mCodeRappCash.setText(coderappcash);
}
You have to get reference to database earlier, and set your TextView before check. So for example:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cash_back_page);
mLogout = (TextView) findViewById(R.id.logoutbutn);
mTxtPhone = (TextView) findViewById(R.id.TextPhone);
mPhone = (TextView) findViewById(R.id.NumberPhTxt);
mReadCash = (TextView) findViewById(R.id.ReadCashBack);
mVersionApp = (TextView) findViewById(R.id.VersionApp);
mSendRequest = (ImageButton) findViewById(R.id.SendRequestBtn);
mCancel = (ImageButton) findViewById(R.id.CancelBtn);
mCodeRappCash = (TextView) findViewById(R.id.CodeDiscountCash);
mGeneralTextCash = (TextView) findViewById(R.id.textgeneralacash);
mSendRequestText = (TextView) findViewById(R.id.textsendrequest);
mSendRequestText.setVisibility(View.GONE);
mGeneralTextCash.setVisibility(View.GONE);
mAuth = FirebaseAuth.getInstance();
initFirebase();
//Set Orientation Portrait
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
getnumberprefs();
//You have to set mCodeRappCash value before this check!!!
// Read to the database Code Cash Back
DatabaseReference myRef = mDatabase.getReference();
myRef.child("Utenti").child(mPhone.getText().toString()).child("RappCash Code").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
String coderappcash = dataSnapshot.getValue(String.class);
mCodeRappCash.setText(coderappcash);
}
if (!isEmpty (String.valueOf(mCodeRappCash))){
String textgen = mCodeRappCash.getText().toString().trim();
if (textgen.equals("Request code sent...")){
mSendRequestText.setVisibility(View.VISIBLE);
mGeneralTextCash.setVisibility(View.GONE);
}else{
mSendRequestText.setVisibility(View.GONE);
mGeneralTextCash.setVisibility(View.VISIBLE);
}
}
}
}
I solved and found this banal problem, thanks to the same to all those who answered, under the portion of code solved.
#Override
protected void onStart() {
super.onStart();
// Read to the database CashBack
DatabaseReference myRef = mDatabase.getReference();
myRef.child("Utenti").child(mPhone.getText().toString()).child("CashBack").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
String cashback = dataSnapshot.getValue(String.class);
mReadCash.setText(cashback);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
// Read to the database Version App
myRef.child("Utenti").child(mPhone.getText().toString()).child("Version App").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
String versionapp = dataSnapshot.getValue(String.class);
mVersionApp.setText(versionapp);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
// Read to the database Code Cash Back
myRef.child("Utenti").child(mPhone.getText().toString()).child("RappCash Code").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
String coderappcash = dataSnapshot.getValue(String.class);
mCodeRappCash.setText(coderappcash);
assert coderappcash != null;
if (!isEmpty (coderappcash)){
if (coderappcash.equals("Request code sent...")){
mSendRequestText.setVisibility(View.VISIBLE);
mGeneralTextCash.setVisibility(View.GONE);
}else{
mSendRequestText.setVisibility(View.GONE);
mGeneralTextCash.setVisibility(View.VISIBLE);
}
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
the information was contained in (coderappcash)
I want to get all of the "Group No" keys in a single Spinner, then all of the "School No" keys of the selected "Group No" in a second Spinner.
Here's my database:
Here's my Java code:
public class Collection extends AppCompatActivity {
TextView ttl, tgat, tsch;
Button btnshw, btngt;
Spinner sping;
DatabaseReference d2ref;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_collection);
tgat = (TextView) findViewById(R.id.texigat);
tsch = (TextView) findViewById(R.id.texisch);
//tcls = (TextView) findViewById(R.id.texiclass);
ttl = (TextView) findViewById(R.id.texirs);
btnshw = (Button) findViewById(R.id.bshow);
btngt = (Button) findViewById(R.id.bgat);
sping = (Spinner) findViewById(R.id.spingat);
btnshw.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
d2ref = FirebaseDatabase.getInstance().getReference().child("2018-19").child("Group No 14")
.child("School no 109").child("Standard 2nd");
d2ref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
String rs = dataSnapshot.child("Rupees").getValue().toString();
ttl.setText(rs);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
});
btngt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
d2ref = FirebaseDatabase.getInstance().getReference().child("2018-19");
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
final List<String> grpno = new ArrayList<String>();
for (DataSnapshot dsnap : dataSnapshot.child("Group No 14").getChildren()) {
String grp = dsnap.getKey();
grpno.add(grp);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(Collection.this, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sping.setAdapter(adapter);
/*I wasn't able to get the value in 1st spinner so I didn't
wrote program for second spinner*/
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
};
d2ref.addListenerForSingleValueEvent(eventListener);
}
});
}
}
I've tried to fetch it in the TextView and increment the value with the Button, but the TextView gets only "Group No 2" in it.
Putting the key into a Spinner is as simple as putting the value into the Spinner. All you have to do is to remove the child() from the for loop.
Instead of doing this:
for (DataSnapshot dsnap : dataSnapshot.child("Group No 14").getChildren()) {
String grp = dsnap.getKey();
grpno.add(grp);
}
You have to do this:
for (DataSnapshot dsnap : dataSnapshot.getChildren()) {
String grp = dsnap.getKey();
grpno.add(grp);
}
Hope this helps..
I was trying to retrieve the data from user and time. But I can't access the data, it's not really accessing the database.
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_balance);
mFirebaseDatabase=FirebaseDatabase.getInstance();
cashRef=mFirebaseDatabase.getReference().child("ASSETS").child("cash_at_bank");
TextView view=(TextView)findViewById(R.id.view);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
cashRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
int time=dataSnapshot.getValue(int.class);
int values =dataSnapshot.getValue(int.class);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
});
To get the time, please use the following code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference cashAtBankRef = rootRef.child("ASSETS").child("Cash at bank");
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
Long time = ds.child("time").child("time").getValue(Long.class);
Log.d("TAG", time + "");
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
cashAtBankRef.addListenerForSingleValueEvent(eventListener);
The output will be:
150555043995
//and so on
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");
}