1. Are you ready for the Galaxy S20? Here is everything we know so far!

How to make a text view equal to firebase string

Discussion in 'Android Development' started by Cinemato, May 22, 2020.

  1. Cinemato

    Cinemato Lurker
    Thread Starter

    Hello, I'm making a project using android studio and java. I have some data in a realtime firebase database and I'm trying to take the data in an activity then making a text view in another activity have the value of that data. I was able to get the data but I when I try to set the textview to equal to it, it does not change.. its like the value is null. I tried printing the value to know if it is null or not and it appeared okay so I dont know the problem.. heere is the code:
    Code (Java):
    1. public class HistoryActivity extends AppCompatActivity {
    2.  
    3.     TextView history;
    4.  
    5.     @Override
    6.     protected void onCreate(Bundle savedInstanceState) {
    7.         super.onCreate(savedInstanceState);
    8.         setContentView(R.layout.activity_history);
    9.         history = (TextView) findViewById(R.id.historyText);
    10.  
    11.         showHistory();
    12.     }
    13.  
    14.     void showHistory()
    15.     {
    16.         String history_text = getIntent().getStringExtra("HISTORY");
    17.         System.out.println(history_text); //DEBUG
    18.         history.setText(history_text);
    19.     }
    20. }
    Here is the first class that takes the data and sends it:
    Code (Java):
    1. protected void onCreate(Bundle savedInstanceState)
    2.     {
    3.         super.onCreate(savedInstanceState);
    4.         setContentView(R.layout.activity_main);
    5.  
    6.         rootNode = FirebaseDatabase.getInstance();
    7.         reference = rootNode.getReference("CompanyInfo");
    8.  
    9.         historyBtn = (Button) findViewById(R.id.historyBtn);
    10.         addressBtn = (Button) findViewById(R.id.addressBtn);
    11.         supportBtn = (Button) findViewById(R.id.supportBtn);
    12.         visionBtn = (Button) findViewById(R.id.visionBtn);
    13.  
    14.         Query getTxt = reference;
    15.  
    16.         getTxt.addListenerForSingleValueEvent(new ValueEventListener() {
    17.             @Override
    18.             public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
    19.                 if(dataSnapshot.exists())
    20.                 {
    21.                     historyTxt = dataSnapshot.child("History").child("Text").getValue(String.class);
    22.                 }
    23.             }
    24.  
    25.             @Override
    26.             public void onCancelled(@NonNull DatabaseError databaseError) {
    27.             }
    28.         });
    29.  
    30.  
    31.         historyBtn.setOnClickListener(new View.OnClickListener()
    32.         {
    33.             @Override
    34.             public void onClick(View v)
    35.             {
    36.                 Intent intent = new Intent(getApplicationContext(), HistoryActivity.class);
    37.                 intent.putExtra("HISTORY", historyTxt);
    38.  
    39.                 startActivity(intent);
    40.             }
    41.         });
    I realllllly need help so anything is appreciated :)
     



    1. Download the Forums for Android™ app!


      Download

       
Loading...

Share This Page

Loading...