Update a TextView through a outer java class - java

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.

Related

How to link a new textview created on java with its xml characteristics in android studio?

In Java, I have created a TextView whose text is the string written by the customer on an EditText in a previous activity. In the XML of the activity, I wrote the characteristics I want this TextView to have. My code creates a new TextView without those characteristics, anyone knows how to link them? I attach the java and XML codes. javacode xmlcodeThanks in advance!
You need to define the textview properly.
In oncreate method add
TextView text_message_1;
text_message_1=(TextView) findViewById(R.id.text_message_01);
Then set the text you get from other activity.
Since you are creating a TextView dynamically, so there is no need define a TextView tag in XML. You can simply set properties of that TextView in Java and add it to any ViewGroup or Table or anything you want.
And you can also try using findViewById() method for that TextView.
If you still face any problem, let me know.
I hope it answers well.

How to update a TextView from another

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.

AsyncTask updates data after fragment is shown

I have fragment that displays data about user downloaded from web service. I want to download that data after the fragment is shown, just like some applications do. While there's no data avaliable, I would like to use either some default value or ProgressBar. What is the proper way to do this? Should I call asyncTask in onCreate or onCreateView method? (since it's not clear if this fragment will be ever shown)
I would do it in the on create method. You want the dialog to begin in the doInBackground method of the Async task and hide the dialog in the on post execute method where you update your view depending on what information you get back.

Sending Data Back to an AlertDialog's Host

To the point: what is the best way to send data inputted into an AlertDialog back to the host of the AlertDialog?
I have an AlertDialog which is created by a DialogFragment created in the MainActivity of my Android application. It has OK and cancel buttons, and uses a custom layout simply to display an EditText box. When the user hits OK, the contents of the EditText box should be sent to a method in the host activity of the Dialog, MainActivity.
I've accomplished this by creating a listener in the DialogFragment that gets implemented in the MainActivity. The listener gets invoked and the activity receives the DialogInterface and a String from the EditText. It works fine, but it seems awfully complicated code-wise for what seems like something that should be able to be accomplished without much extra code in the onClick method of the Dialog.
Is this really the best way to pass back a simple command, such as running a single method with an argument in the host Activity? Or even just changing the value of a variable of the host activity? If I wanted to use more than one class of DialogFragment in the same activity. I would have to implement another listener, and make sure the methods in each don't have the same names?
Thanks.
This is a pattern that fragments and host activity communicate each other. An other way plz try "Otto event bus". Google it you can find some.

Update Android TextView

I have an simple android app that I want to write on the display the value of a single field, that belongs to a different class.
Using the simple text view I can write the initial value of the field but I don't know how to update the text on the display whenever the field has changed value.
Btw, it is my first android app so im still lost
Here is my activity code:
public class findIT extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PositionHolder ph = new PositionHolder();
TextView tv = new TextView(this);
setContentView(tv);
//this.updateText();
tv.setText("You are at " + ph.getPosition());
}
}
You need to create a TextView in an xml layout as following:
<TextView
android:id="#id/textView01"
android:text="Enter your initial text "
android:layout_width="wrap content"
android:layout_height="wrap content"
></TextView>
Then write the following in your class after setContentView() function:
TextView textView = (TextView) findViewById(R.id.textView01);
textView.setText("Enter whatever you Like!");
Chris.... Here is a most basic way to develop an app. Divide up the problem into three pieces. The presentation or view. The algorithm or model. The Controller that responds to user and system events. This creates a "separation of concerns" such that the Controller owns the view and the model. You create the view using xml as in main.xml. You create a separate class to do the work say MyModel.java and of course there is the Controller or the Activity class say MyActivity.java. So the data comes from the model goes to the Controller that updates the view.
So your question is how to get the data from the model and update the view. Naturally this will take place in the controller, your Activity. The simplest way to do this is to put a button in the activity and when the user hits the button, call model.getLatestData() and update the view. This is PULLing data. The next way is for the Controller to check for an update say every minute. This is POLLING for data. The next way is for the Controller to register an interest in changes to the model and sit around waiting for the model to signal a change and then update the view. This is asynchronous PUSHING of data from the model to the controller and could be done with the OBSERVER pattern.
I know this makes no sense to you when you are struggling with trying to just get the code to work, but hopefully I have planted the seed of an idea in your head that will bother you and make sense sometime in the future.
JAL
You're going to need to define a layout in xml, then when you create the TextView, you associate it with its layoutID. Something like:
TextView tv = (TextView) findViewById(R.id.something);
I can explain a little more, if you need, but this will give you a starting place to look for more answers.

Categories

Resources