0

I am new to android/java programming. I have two class, one is an activity and other normal class. In my activity class contains TextView. Can i update my TextView of one class from a editText(that the user enters) in a another class. I tried with random code, but it fails. Please help I've been looking forever

Joshua Aragon
  • 55
  • 1
  • 10
  • You need to explain what these classes are (activities, POJO, other components...). – Shaishav Aug 11 '16 at 03:21
  • @Shaishav activities sorry I'm new to this. – Joshua Aragon Aug 11 '16 at 03:23
  • If they are different activities, you need to pass data between them. I'm guessing you are using `startActivity()` for starting the other activity...I suggest `startActivityForResult()` – Shaishav Aug 11 '16 at 03:25
  • @Shaishav Sorry this doesn't help I'm just so confused. I watched other videos on how to do this but all of them didn't answer my question. – Joshua Aragon Aug 11 '16 at 03:31

2 Answers2

0

You might update the TextView from wherever in the java code by referring to the

findViewById(R.id.some_text_view_name).

Some what like this:

TextView textViewName = (TextView) findViewById(R.id.some_text_view_name);
textViewName.methodName();

Here methodName() refers to the Public methods listed here

Hope it helps. :)

cRAN
  • 195
  • 1
  • 2
  • 16
  • how would I make it so I can update the textView from Activity 1, using an editText from Activity 2 – Joshua Aragon Aug 11 '16 at 03:27
  • @JoshuaAragon I guess this is what you are seeking for http://stackoverflow.com/questions/10996479/how-to-update-a-textview-of-an-activity-from-another-class – cRAN Aug 11 '16 at 03:32
  • No I saw that post many times. So I have an activity on my application that has you write your name and I want it to update the textView on another Activity. does that make sense? – Joshua Aragon Aug 11 '16 at 03:36
  • @JoshuaAragon Did you try this one http://stackoverflow.com/questions/5654989/pass-text-between-activities – cRAN Aug 11 '16 at 03:46
  • That one I think explains it but idk why i don't understand how to write it in my code could you give me an example? – Joshua Aragon Aug 11 '16 at 05:36
  • @JoshuaAragon here it is https://github.com/cRAN-cg/ExampleOne.git Hope it helps :)) – cRAN Aug 11 '16 at 06:15
  • Yup that was it! Thank you so much! – Joshua Aragon Aug 11 '16 at 20:32
  • And what do you mean by " callAnotherActivity" – Joshua Aragon Aug 11 '16 at 21:44
  • Just a function which triggers intents to another activity. I added it to make it more readable :)) – cRAN Aug 12 '16 at 00:39
  • hey so when I go back to the activity with the text view that the edit text is supposed to be updating it is not changing. do you know why? btw idk if this matters but i have my editTexts being stored to firebase – Joshua Aragon Aug 14 '16 at 07:30
  • I guess putting `nameEditText.setText("");` just before `startActivity(i)` may help. – cRAN Aug 14 '16 at 07:42
0

You can start your second activity using startActivityForResult() instead of startActivity(). In the second activity you can set the result and its status using setResult() and get back to previous activity (via back press or something). In the first activity, this result will be received in onActvityResult(). From here, you can get the data set by second activity and update your textview.

This is the gist of what you are supposed to do. You can get code example here, here and here.

Community
  • 1
  • 1
Shaishav
  • 5,282
  • 2
  • 22
  • 41