Android MVP - where should i read from Textview? - java

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.

Related

Can't understand the View view parameter

I am learning android development from Android Developers
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
// Do something in response to button
}
The sendMessage message method contains a parameter 'View view'? What is a View object and what does it do?
Why is it passed to the method as a parameter and where does it come from?
While writing a click event you might need to know which object is clicked.
In android mostly all the UI components will extend View Class.
So you are getting the instance here
public void sendMessage(View view) {
// Do something in response to button
}
In android we can handle click events by two ways
First way
Providing the method in xml itself. For example
android:onClick="sendMessage"
This is how it happens in the example provided by him.
Second way
We can extend an onClickListener in the Activity or Fragment and we should override onClick method.
See the question "existence of parameter (View view)" for further references.
A View object in Android app development is the building block for a user interface. They are used to create things onscreen for a user to interact with.
http://developer.android.com/guide/topics/ui/overview.html
Edit:
In your case, when the Send button is clicked and it calls the sendMessage function, it passed the View object of the Send button (it passes the View of the object that called the method).
The parameter is the clicked button, that's all. It actually says so in the link that you provided :/
Specifically, the method must:
Be public
Have a void return value
Have a View as the only parameter (this will be the View that was clicked)
I am sure you should have understanded the following method, because it is the basic parameter we often used.
public void sendName(String name) {
//some stuff
}
And this method will be called when the sendName() appeared next time. The only difference is the sendMessage will be called when you click the button, as button is a view exactly, so you have to use View class instead of String class.
sendMessage is a method name
View is your class
view is your parameter
check in xml there are declare Sendmessage button or imageview. and set its property to android:onClick="sendMessage";. so send message is not userdefined method but its a listner method on button or imageview click. so there are View view parameter pass in method.
in that case we con not need to create object and findviewbyid and setonclicklistener class..
thats it....

How to show DialogFragment from within the onLoadFinished method of LoaderCallbacks

I receive an exception when trying to show a DialogFragment from within the onLoadFinished method of a Fragment that implements the LoaderCallbacks interface. Basically I am using the LoaderCallbacks to get some data from a rest service and then on the onLoadFinished I am trying to show a custom DialogFragment that contains a ListVeiw to allow the user to make a selection. Everything works great except when I try to launch the dialog from within the onLoadFinished. How can I accomplish this..and is this the correct approach to the problem.
Here is an example of what I am trying to do:
public class EventFragment extends Fragment implements LoaderCallbacks<someresponse> {
#Override
public void onLoadFinished(Loader<someresponse> arg0, someresponse data) {
//an exception is generated when trying to launch a dialog fragment from
//within the onLoadFinished
FragmentManager manager = getFragmentManager();
ListViewDialogFragment dialog = ListViewDialogFragment.newInstance(data);
dialog.show(manager, "event_list_dialog");
}
}
Thanks!
So after some research, I have determined that my approach was incorrect. You should never try to launch a new fragment from within the onLoadFinished method of an LoaderCallbacks async task. The framework tries to keep you from doing this because the state of the currently running fragment or activity, that implements the LoaderCallbacks is indeterminate and therefore not guaranteed to be there when the async task finishes.
Additionally trying to separate a processing dialog state and data display state into two separate fragments is a bit counter to the MVC design pattern that the android framework supports. That said, my new approach consisted of dynamically changing the view for the fragment implenting LoaderCallbacks to hide or show a specific linear layout, one for the process indicator and one for the display of the data. This approach left me modifying an existing fragment instead of launching a new one which worked out great.
Here is a link to the discussion that finally gave me clarity.
https://groups.google.com/forum/#!topic/android-developers/dXZZjhRjkMk/discussion
The approach you define is quite good, except one case - what will happen if your activity had already gone from the screen, when loading operation got finished? How it will show dialog in this case?
So generally, I would appreciate if you'll tell which exactly exception did you get.
However, as a general approach, it can be useful to check is the activity holding the fragment is still on top or even was it finished or not.
Even better - you should consider cancelling all background operations when activity/fragment is destroying, in this case you'll have no problems with showing dialogs.
Good luck!

MVP in GWT. Complicated Views

Have a question about using mvp on complicated views.
Suppose, we have some widget with boilerplate design (ListWidget) like this:
Control buttons provide functionality for switching between ContentPanel (just regular CellTable with data) and DetailsPanel (here we can edit particular entry, this is not Modal Dialog)
DetailsPanel, in turn, have this structure
Button1, Button2, Button3 should deal only with DetailsPanel and change ActionPanel (ActionPanel some kind of wizard, so its content should be tottaly changed after Button1, Button2 or Button3 clicked), but Buttons Save and Cancel should navigate us back onto ListWidget.
What approach should I use to implement described functionality?
Should I use 2 different ActivityManagers with one of them in master context or not?
How to manage and broadcast messages to dependant panels on ListWidget? (Is it ok to put Handlers in Views and just push event into inner eventBus on some actions?
For instance, when I save item in DetailsPanel, PreviewPanel should change and this item should be focused in CellTable; etc)
If I should use pattern with two different ActivityManagers, how exactly should I handle activity change behavior?
Thanks, hope somebody helps me.
I am answering your question, first with a brief explanation and then point by point taking your questions.
At framework level, you could have a class which implements ValueChangeHandler and set that on History using History.addValueChangeHandler(controller); assuming controller is implementing the value change handler
Any newItem you would put in history with History.newItem("NameOfAction") would invoke implementation of ValueChangeHandler which in this case would be controller
Now within implementation of ValueChangeHandler you can
String token = event.getValue();
if (token.equals("NameOfAction")) {
Call Appropriate presenter
}
Now let's talk about the second part of framework, presenter and view implementation. I am assuming that RootPanel is the container of all your UI Widgets. You can create individual presenters for each action
presenter = new ActionPresenter(rpc, eventbus, new ActionView())
Notice that I am creating a view and passing on to the presenter, presenter gets data and build the view with data. You could later pass on the container to the presenter to show the whole thing on UI.
Now about your specific questions
MVP would be your best bet here The article on MVP here is pretty good
I don't see a reason to use 2 activity managers as long as your container of those widgets is same
EventBus would be ideal. If you have to pass data with events, session variable like static fields in main class would server your purpose
I would need more details on exactly what you want to put in which manager
Hope this helps!

How should I bind new view components to the presenter in GWT's MVP pattern?

I been reading through Large scale application development and MVP and I was wondering how you guys deals with dynamically added components within this suggested way of implementing the pattern.
Lets say you have a table, where you have three buttons in each table row, and table rows can be added dynamically. How would you guys suggest binding these to the presenter?
The new row, if following Google's proposed structure, will get generated in the view, which does not have a direct link to the presenter and can not call back to it (so no calling bindNewButtons(Button, Button, Button)-ish type method).
So, what's good practice here? I was thinking since the presenter will be handling the event which adds a new row in the widget inside the view (handles as in, fires the method in the view which generates this new row), I could have a getRowButtons(int index) method in the view, and then using this to retrieve the components and bind them after they where added.
I'm sure there's a more clever way of doing this though, so I'm seeking a bit of advice here.
The second article in this series shows a view which does have a reference to the presenter.
The view:
private Presenter<T> presenter;
public void setPresenter(Presenter<T> presenter) {
this.presenter = presenter;
}
The presenter:
public interface Presenter<T> {
void onAddButtonClicked();
void onDeleteButtonClicked();
void onItemClicked(T clickedItem);
void onItemSelected(T selectedItem);
}
You could now define a method in the presenter interface which has a parameter which indicates which row was clicked. If you don't want to make the view aware of the presenter interface you could always choose to fire an event on the event bus which the presenter could respond to. Based on your question though, the first option seems a more reasonable answer.

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