Update Android TextView - java

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.

Related

How to use dynamic buttons with array data

I am still working on my first android app and hope you can help me once more. I use the YouTube API to get all the content from my channel and create the following list:
Now my problem is I do not know how I can tell the buttons in the middle of each thumbnail to start the according video. I have the correct video ID for each element (provided in a textview on the right during troubleshooting...) but when I try to connect this information to the button onclick method it will just use the first textview content on the screen, so it is always just opening the first video => for every single button.
public void jumpin(View view) {
TextView textViewHelper = (TextView) findViewById(R.id.videoIdTester);
String textViewHelperString = textViewHelper.getText().toString();
Intent intent = YouTubeStandalonePlayer.createVideoIntent(this, API_KEY, textViewHelperString);
startActivity(intent);
}
My basic problem is that the buttons are not filled with the video ID information like the textviews for example. The buttons are just created and I call the onclick method which needs the video id then which is not reachable from my perspective as soon as the list is created.
I think this is by far too complicated at the moment but I have no plan how to solve that. I can provide more code if you need it but I think this problem is more about button and api/array behaviour in general.
Anwered by myself, using an onitemclick listener instead of the buttons ...

Android MVP - where should i read from Textview?

After a user clicks a button i want to read what's in the TextViews of the activity and then call a method from the presenter to get a return based on the info sent to it.
However i don't know if the presenter should get this data by itself or if i should pass to him from the Activity class when i call one of his methods (this would mean get the data in the TextFields inside the activity class and then send the data as parameters when i call a Presenter method).
I've tried both methods but i don't know which one is the more organised way to do this.
You will pass the string to the presenter wanted method , then do what you want .
button.setOnClickListenr(new View.OnClickListener() {
#Override
public void onClick(View view) {
String str = myTextView.getText().toString();
presenter.doSomething(str);
)}
When using the MVP architecture pattern, you're supposed to have a reference to the Presenter on the View (in this case, the Viewis your Activity). They have very different responsibilities:
Your View should be as dumb as possible, i.e., it should only be responsible for displaying whatever data to the user and collecting user input.
Your Presentershould be responsible for both handling all the data that is displayed on the View and acting as a middleman between the View and the Model. In other words, for each possible user input, there should be a method in the Presenter capable of handling it.
For instance, in your case, the Viewis responsible for handling the contents of the TextView over to the Presenter. Then, the Presenter has to pass those contents down to the Model, which does something with it according to your business logic. When the Model finishes processing, it returns the result to the Presenter, which then prepares the content to be displayed on the View. When the content is ready, the Presenter then sends it to the View.
Knowing all of this, you should be able to answer your question. It would be something like this (pseudo code):
class MyActivity
{
// you should inject this
Presenter myPresenter;
TextView myTextView;
...
myPresenter.doStuff(myTextView.getText().toString());
}
Why you need read what's in the TextViews?
All data show in View should come from Presenter.
Your data should save in your Repository, Presenter can get everything that you want in your Repository.

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.

Android: dynamic text creation?

Let's say I have some strings in Java that I've retrieved from a web script. It doesn't matter really, they're just strings stored in variables.
Now my question is how to dynamically append them to the application (to show the user) and possibly style their position, from Java.
To draw an analogy, I want to do something similar to the following in JavaScript:
var text = document.createElement('div');
text.appendChild(document.createTextNode("some string"));
text.style.position = "whatever";
// etc, more styling
theDiv.appendChild(text); // add this new thing of text I created to the main application for the users to see
I've looked into the TextView, and I don't seem to be using it properly (I don't understand the constructor I guess?). What I want to try right now is to have the user press a button in my application and then have some text dynamically generated. Here's what I've been trying:
search_button = (Button) findViewById (R.id.backSearchButton);
search_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String test = new String("testing!");
TextView test2 = new TextView(this); //constructor is wrong, this line gives me an error
test2.setText(test);
setContentView(test2);
}
});
As you can probably tell, I don't come from much of a Java background. I'm just trying to draw parallels to stuff I would want to do for a web app.
Thanks for the help.
Try:
TextView test2 = new TextView(ParentActivity.this);
replace "ParentActivity" with the name of your activity.
your this pointer is a reference to the OnClickListener that you have anonymously created. You need a pointer to the containing Activity.
You would probably be better constructing your text view in the onCreate of your activity and just setting the text from your onClick method
I'm new to Java and Android, myself. But I'll give your question a try.
You have to decide if you want to create the TextView in XML or dynamically in Java. I think creating it in XML in the layout creator is better.
I don't think you should be using setContentView(test2).
If you create a TextView dynamically, I don't think you need to put anything in its constructor. But you do have to add the TextView to the parent view. In other words, let's say you have a LinearLayout somewhere in your layout. You have to do: linearLayout1.add(myTextView) or something.
The rest of your code seems fine. But then again, I'm still new to this, myself. Let me know how helpful this answer is, I'll try Googling for more help if it's not enough.

Android: How to refresh a tablelayout after removing a row?

I have a tablelayout that retrieves data from a *.txt file.
For every line of data in the txt file, there will be one row of data.
Let's say I have two rows of data in the txt file right now, it makes sense that two tablerows will be generated.
Then, I added a OnLongPressListener which, when called, will delete one row of data from the txt file.
Now's the first question: After deleting data in the txt file, how do I refresh my tablelayout to reflect that change?
Second question is: After I get my first question solved, is it possible to have some kind of animation where one row will fade out or slide out instead of just disappearing outright?
Thanks!
Not sure if you are still looking for answer. But I came across this post facing kinda the same problem. Plus the fact that I was forced to use TableLayout (the amount of code written using TableLayout is huge and it wasn't my call to switch to ListView).
What I ended up doing was to remove all the views from TableLayout using:
`tableLayout.removeAllViews();`
But that's not gonna work if the number of rows after removal changes drastically. I needed to invalidate my view too, using a handler. Here is the rest of my code.
protected static final int REFRESH = 0;
private Handler _hRedraw;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tree_view_activity);
_hRedraw=new Handler(){
#Override
public void handleMessage(Message msg)
{
switch (msg.what) {
case REFRESH:
redrawEverything();
break;
}
}
};
...
}
private void redrawEverything()
{
tableLayout.invalidate();
tableLayout.refreshDrawableState();
}
There is only one part left and that's the part where you send a message to your handler to refresh your view. Here is the code for it:
_hRedraw.sendEmptyMessage(REFRESH);
Why not use a ListView instead? It gives you better control when using an MVC model where there's a dataset tied to a View. So you could have a class that extends BaseAdapter. This class binds your data to the View and this class also has a method: notifyDataSetChanged() which is what should solve your problem.
You may find notifyDataSetChanged example and customListView helpful.
Let me know if you need any help further.
Generally in onCreate() we do all the stuff that shows the ui with text, try to put the code that makes up UI in a private method say setUpTabView() and try to call this from onCreate and even try calling this setUpTabView() when ever the text changed. This kind of approach i did in grid view. worked very well...!

Categories

Resources