How to update a TextView from another - java

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

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. :)

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.

Related

Update a TextView through a outer java class

I am currently working on a project and the following situation appeared: I have a ClientSide (of a server) with two threads and a TextView. I would like to display messages in TextView when the ClientSide receives them. I am currently using this code here: http://www.nakov.com/inetjava/lectures/part-1-sockets/InetJava-1.10-Chat-client-server.html
My TextView has an id of textView, the java class is called ClientSide and it's 1:1 with the one from the link. I tried the following:
- in the TextDataTransmitter thread, in its run() - R.id.textView.setText(data). This pops me out cannot resolve setText
data as you can see in the code is defined as a String.
You need to call setText on the TextView object, not on its id. Get the view by calling findViewById(R.id.textView) on the Activity after setContentView has been called.
R.id.textView is the id of your TextView which is a long, your should get the textView by findViewById(R.id.textView) first.
AND don't modify UI object in non-main thread.

Changing activities with Intent on listview button

In the application I am building I have 2 listviews with, among other elements, 2 buttons on every row, one for eliminating that row and the other to pass to another activity using an Intent.
I am detecting clicks on both buttons by setting click listeners on both buttons on the getView method of the Adapter class.
The first button was something I couldn't figure out, because I needed to identify in which listview the button was clicked and the position of the clicked row which I couldn't realize how to do.
The second one I thought would be easier since all I needed to do was Intents.
However I needed to call a method from the activity class (had to instance it) since I couldn't make Intents in a non-activity class.
This last one threw a NullPointerException.
Will post all code & logcat in a while, be right back, any possible help will be appreciated.
I will need to see your code, but basically what worked for me for being able to call intents within the code of the adapter was to use the host activity.
Something like this:
item_new = (TextView) convertView.findViewById(R.id.my_view);
Activity host = (Activity) item_new.getContext();
Intent intent = new Intent(host, MyActivity.class);
host.startActivity(intent);
If you post the code maybe I can give more details
I'm not figure out the entire problem, but if you want to start an Activity from an Adapter you could simply pass the current activity to the adapter constructor (or cast the getContext() to Activity, if you used that as context of your adapter)
In a line of code:
Activity.class.cast(getContext()).startActivity(intent);

Updating ListAdapter from different activity

I am working on my hobby project to learn more about android programming and stuffs. So i recently ran into a simple problem but couldn't find a solution for it.
My project for now have 2 activity [MainActivity], [CommentActivity].
In MainActivity, I have a listadapter (cusAdapter) will fetch all the content from a database and display it on a listview. Everything works fine there. It will fetch all the comments and total comments for a certain post based on the post id. In the same MainActivity, i have a comment button and a onclick listener which when clicked will bring user to the next activity [CommentActivity] and load all the comments and data needed using another custom adapter. I also have a total comment TextView that will show the total comment fetched by the json and display it in my listview.
When a user posted a comment on CommentActivity, it will save into the database via AsyncTask.
Now the problem is, when i press back button and navigate back from CommentActivity to MainActivity, i use intent with flag (intent.flag_activity_reorder_to_front). So it will never reload the MainActivity again when back button is pressed. Also it will never update the total comment in the MainActivity. What i want to do is when back button is pressed on CommentActivity, i want it to refresh/reload the ListAdapter in MainActivity to fetch the latest data from database.
Also i don't want to reload the whole MainActivity, i just wanna update the view without reloading the whole activity.
I believe there must be some easy way to do this. I can post codes if you guys need it to assists me.
Thank you.
Yes, there is an easy way.
Start your sencond activity using:
startActivityForResult(intent, RESULT_CODE)
use setResult(RESULT_OK) in your second activity after doing the commit of the changes to the database
override onActivityResult() in your first activity to check when the second activity returns properly
update the first adapter as needed.
Hope it helps.
In your MainActivity you could check in onResume to see if the data has changed, as this will be called when you go back to it from the CommentActivity.
If the data has changed, calling notifyDataSetChanged() on the adapter will reload the view with the new data

Android - Listview on click activates a new intent

OK I've been online trying to figure this out, and piecing together bits of code here and there. I still don't understand. All I want is to display three list items and on item click it would go to a new activity. What do I need to fix?
Edit:
I made a stupid mistake. Fixed now - thanks everyone!
I'm going to guess that you should replace this:
String item = (String) getListAdapter().getItem(position);
with this:
String item = view.getText().toString();
getListAdapter() is a function for a ListActivity but not a regular Activity.
Are you sure that you have your new activity classes in the android manifest? You need to add each activity you are going to launch to the manifest.
Check your manifest.xml and declare the activities and you should be good to go. Also change the way you get the info from the listener to so that you getString from the item instead of using the position. That way you can control exactly what is passed to your intent by specifying the textview on the item.
Sam and El Duderino both have valid points, but just as an amusing point nonetheless:
You're checking to see if the items are "Economy" "Basic" "Professional", etc., but your String array has only the months of the year ... :P
Also, when setting the Intents, you should not be using this, because you are in the onClick event, which means this is an onClickListener and not an Activity. So use atcList.this instead of just this.
You never set the onItemClickListener to the listview:
myList.setOnItemClickListener(this);

Android : Text Fields are displaying empty when returning to previous activity

I have a problem with back button functionality in an Activity of Android. The first Activity having the Spinner to select one item from the Spinner's list and second one is the text field. I implemented search functionality using the Spinner and text field. The results are displaying fine as a ListView.
Here my Problem is:
While returning to the first Activity, the Spinner and text field are showing empty in the Activity. It should show the previous searched results.
Help me with the sample code/ links.
Dont create a new intent. You just need to call finish() from your second Activity to handle back event and move back to your first activity.
Its normal. When your first Activity goes to back ground its finished by System itself. So make sure to save your data in some place and in Activitie's onCreate() and onRestart() method reload the data to TextView and spinner..
Edits:
Create a Data class and Store your search results in String[] array or a String or how ever you like it. and make the class a singlton class. and when you come back to this screen fetch those data's and set Text of TextView and adapter for Spinner..
Shafi yes, i am calling the back function using Intent as Intent i=new
Intent (presentClass.this, previousClass.class);
Don't do this. Because the Activity stack will become like loop with same Activities started again n again. Instead just finish presentClass.. it will come back to previousClass
Just an addition to #ntc post look to onSaveInstanceState(Bundle outState) and onRestoreInstanceState(Bundle state) methods in Activity.

Categories

Resources