programming style with modal dialog - android

in my android application at some event in an activity I want to ask the user for a name (string). I know how to do this: call showDialog, create the dialog in the Activity.onCreateDialog method (I need to supply a string for the label) and handle the result in the onClick of the dialog. This works fine and to my satisfaction.
BUT this way I have three different places, where this simple task spreads throughout the code of my activity. I would much more prefer to keep this code together, to write some code like this:
string result;
if (showSimpleEditDialog(idForLabelString, result)==DIALOG_OK)
{
// do something with the result
}
or maybe with a class instance
SimpleEditDialog dlg = new SimpleEditDialog(idForLabelString);
if (dlg.showModal()==DIALOG_OK)
{
string result = dgl.getResult();
// do something with the result
}
(The "idForLabelString" would be some resource id for the label to use, DIALOG_OK would be some constant returned when the user clicks OK)
I know, I would have to write this methodes or this class. But for better readibility of my code I would do it. Any suggestions?
Thank you,
Gerhard

"BUT this way I have three different places, where this simple task spreads throughout the code"
So why don't you create a Method for this task? What you are talking about sounds like some sort of 'ActionListener' to me. This can be done in Java/Swing, but not in Android.
But, if you have three Dialogs, which all need to do the same when "YES" e.g. "NO" is pressed, you could define the 'DialogInterface.OnClickListener()' as a global inner-Class (or in a second class which extends the 'onClickListener') and then use it for all the Dialogs.

Now actually the problem with modal dialogs is mostly a problem with programm flow. You want to keep things together that belong together. You want to display a dialog that returns "ok" or "cancel" and additionaly e.g. a string that the user entered into one of the dialog widgets.
I do not want to write half of the code up to the line where I need the result of the dialog on one place and the rest of the code on another place namely the onClickListener of the dialog.
In some scenarios the first dialog might invoke a second dialog e.g. to specify a color which is not in the list of the first dialog's ListView.
Your code will be scattered all over the place (in each dialog's button onClickListener) and will be hard to read or to maintain.
Now after having written some unclear code like that I came up with the following solution which certainly respects the android design guides.
Instead of directly showing a dialog I create a Handler derived class which handles messages.
I send it a first message which creates and shows a dialog. It also forwards the handler to the dialog and the diaolg in it's onStop method sends another message to the handler, indicating the end of the dialog. There you can examine the dialogs properties, the contents of the edit fields or whether it was stopped with OK or CANCEL.
Now in the message handler all the logic of the task sits in different cases of the messages arg1 value.
Some cases might be skipped (e.g. the user selected a standard color and did not need a special color dialog).
The dialogs are independant of the scenario from which they are called and in their code only reflect their simple task (selecting from a list, some checkboxes etc.). They may be reused from other scenarios.
Following a kind of a template how to use this approach:
public class DoSomethingWithDialogs extends Handler
{
Context context; // from which it was called
final static int stepBegin = 0;
final static int stepNext = 1;
final static int stepSomethingElse = 2;
final static int stepLast = 3;
protected DoSomethingWithDialogs(Context context)
{
this.context = context;
}
public static void start(Context context)
{ // this is the main (only) entry point from outside
DoSomethingWithDialogs st = new DoSomethingWithDialogs(context);
st.sendMessage(st.obtainMessage(0, stepBegin, 0));
}
#Override
public void handleMessage(Message msg)
{
// step by step handling the task
switch (msg.arg1)
{
case stepBegin:
{
SomeDlg somedlg = new SomeDlg(context, this, stepNext);
// when the dialog closes, it sends a message to this with stepNext as arg1
somedlg.show();
}
break;
case stepNext:
{ // this message was send by the dialog when it finished
SomeDlg somedlg = (SomeDlg) msg.obj;
if (msg.arg2 == Dialog.BUTTON_NEGATIVE)
{
// has been canceled, nothing to do
} else
{
if (somedlg.someProperty)
{
} else
{
sendMessage(obtainMessage(0, stepSomethingElse, 0));
}
}
}
break;
case stepSomethingElse:
break;
}
}
}

Related

When realmResults.asObservable is called?

I'm implementing a chat client.
Everytime user clicks Send message button - I perform a Realm insert of this message.
I have a service 'waiting' for this change to send this message via socket.
Like this:
Observable<RealmResults<RealmMessage>> observable = getUnsentMessages();
subscribeUnsendMessages = observable
...
.subscribe(message -> {
launchMessageSending(message);
});
and method for getting this observable looks like this:
public Observable<RealmResults<RealmMessage>> getUnsentMessages() {
final Realm instance = getRealmInstance();
return instance.where(RealmMessage.class)
...
.findAllAsync()
.asObservable()
.filter(o -> ... )
.doOnUnsubscribe(instance::close);
}
And here is the problem - there is a corner case, when I perform two operations roughly at the same time.
The second one looks like this:
public boolean shouldTrack(#NonNull RealmChat chat) {
getRealmInstance().executeTransactionAsync(realm ->{
RealmTrackedState trackedStates = realm.where(RealmTrackedState.class).findFirst();
trackedStates.getRealmChats().add(chat);
});
return true;
}
The above method is a tracking cache for remembering if I tracked given chat or not.
(This is a bit more complicated than this, bear with me).
This PROBABLY causes the observer to react on the second database change.
Because these two operations happen almost at the same time - the second notification is still "valid" because there is an unsent message.
So, questions:
Am I right?
If so - how should I handle this?
I don't really want to 'delay' the shouldTrack(); method because I'm afraid that the same problem will occur in a different way.
Maybe create a flag for this message being 'handled', so the second will be ignored - but this is a bit nasty I guess. The second 'send' shouldn't happen.
EDIT:
Here is my TrackedState object
public class RealmATrackedState extends RealmObject {
#PrimaryKey
private int id = 1;
private RealmList<RealmChat> realmChats;
private boolean isSomething;
}
Is updating such object (as posted above) causing RealmMessage table be notified?

Button Text and Booleans (Simple Android App)

I'm build the Fun Facts app on the Android Development Track. I decided to take a exploratory detour and try to create a very basic introductory message to the user. I changed the factTextView text to "You can click the button below to see a new fact!" and changed the showFactButton text to "Try it out!"
From there, I changed the final line onClick object (is that an object?) to the following:
public void onClick(View view) {
String fact = mFactBook.getFact();
// Update the label with our dynamic fact
factLabel.setText(fact);
// Set button text to new fact prompt
showFactButton.setText("Show another fun fact.");
This seems to work fine. However, I feel like "updating" the button text to the same new string on every press isn't always the best practice, even if it is easy and readable. I tried to add a boolean that will check the text of the button, and update it only if it has not already been updated. This is what I've come up with so far:
View.OnClickListener listener = new View.OnClickListener() {
#Override
public String launchText = getResources().getString(R.string.start_text);
public String nextText = getResources().getString(R.string.next_text);
public String buttonText = (String) showFactButton.getText();
public boolean updateLaunchText() {
if (buttonText.equals(launchText)) {
buttonText.replaceAll(launchText, nextText);
return true;
}
else {
return true;
}
}
public void onClick(View view) {
String fact = mFactBook.getFact();
// Update the label with our dynamic fact
factLabel.setText(fact);
}
};
With the following added to strings.xml:
<string name="start_text">Try it out!</string>
<string name="next_text">Show another Fun Fact!</string>
No errors, but the button text stays on "Try it out!" I'm sure that all the extra objects are totally unnecessary compared to the first, working method for the scope of this app, but I'd still like to figure it out since I don't really have any idea what I'm doing with the boolean.
Questions: 1) What am I missing in the longer boolean approach? 2) What's the actual most efficient approach to accomplish this task?
Did you connect the listener to the button object?Without that connection no logic is applied to a button click.It goes like this:
buttonName.setOnClickListener(...)
You'd have to initialize the button object first though :)
Where r u call to method updateLaunchText() ?
you should change the objects to global object (not to create the into the listener):
private String launchText = getResources().getString(R.string.start_text);
private String nextText = getResources().getString(R.string.next_text);
private String buttonText = (String) showFactButton.getText();
and take the method updateLaunchText() out of the listener too.
and then into the onClick(View view) call to updateLaunchText() like this:
public void onClick(View view) {
updateLaunchText();
String fact = mFactBook.getFact();
// Update the label with our dynamic fact
factLabel.setText(fact);
}

How to change the data in a ListView of one Activity from other Activity

I have seen few question on SOF, but neither of them helped.
In my application I have a List of users which can be accessed by clicking friends of a user. The flow is:
Go to my profile
Click on my friends to go to an Activity which has list of users(my friends)
Click on any of the listView Item takes to that user's profile
From that profile I can see that user's friends list the same way as mine.
The problem is all these listView items have a button to add as friend which makes me and that user as friend in that list(like follow changes to following in twitter) now I come back through the backstack, and somewhere in one of the previous listViews that user is present for whom the button is still add as friend.
How to change the button(a flag in my adapter data) for that user in all the ListViews?
Use Interface to send events back to the activity and update the list or database when the event is received.
Interfaces are ways of passing messages to "the outer world". Just look at a simple button onClickListener. You basically call a setOnClickListener(this) on the button and implement onClickListener, which is an interface here. Whenever the button is clicked, you get an event in onClick. It is the safest way to pass messages between activities without the need of intents ( which according to me is a huge pain in the ... ) Here is an example:
Example:
class A extends Activity implements EventInterface{
public A(){
//set a listener. (Do not forget it!!!)
//You can call it wherever you want;
//just make sure that it is called before you need something out of it.
//safest place is onCreate.
setEventInterfaceListener( A.this );
}
//this method will be automatically added once you implement EventInterface.
void eventFromClassB(int event){
//you receive events here.
//Check the "event" variable to see which event it is.
}
}
class B{
//Interface logic
public interface EventInterface{
public static int BUTTON_CLICKED = 1;
void eventFromClassB(int event);
}
static EventInterface events;
public static void setEventInterfaceListener(EventInterface listener) {
events = listener;
}
private void dispatchEvent(int trigger) {
if (events != null) {
events.eventFromClassB(trigger);
}
}
//Interface ends
void yourMethod(){
//Call this whenever you want to send an event.
dispatchEvent( BUTTON_CLICKED );
}
}

Textview won't display data which has been updated in model (MVC)

I've tried the following approaches to try to get the TextView to update from the model:
TextChangeListeners - error
Other Threads
asynctask
Added a refresh button which updates the data, I force the click
Scenario is as follows:
Standard thread running which updates the Model (MVC) object every second, which works perfectly.
When the Model object is updated, it notifies all classes which implement "Observer".
My GolfHomeScreen extends Activity and implements Observer (code below).
The GolfHomeScreen.update (Observable observable, Object data) method works perfectly. It executes every second as expected and S.O.P the correct data (see --> //* #1 *).
This then kicks off a Thread. I read on this site that you have to UI Thread to execute code which updates widgets and then run the "runOnUiThread" method - the code looked something like I have done below but I found it a little hard to follow. Anyway, this thread runs (see --> //* #2 ) which then executes method onClick(View v) (see --> // #3 *).
System.out.println("driverType :" + mvcModel.getDriverName()); (see --> //* #4 *) works perfectly.
//* #5 * - it MUST update but it doesn't.
If I actually physically click the button, the data is displayed correctly on the screen. My only guess is that the screen must not refresh on the forced click or else I am not using the UI thread to make the update.
NOTE: If the code below is missing something, it is because I removed a ton of code to simplify the understanding of what's going on.
public class GolfHomeScreen extends Activity implements Observer
{
GolfHomeScreen golfHomeScreen = null;
TextView driverName = null; // Type of driver used by golfer (eg: 1 wood)
Button refreshData = null;
#Override public void onClick(View v) //*** #3 ***
{
if ((v.getId()) == 12345) // ID of wood (12345 = 1 wood)
{
//*** #4 ***
System.out.println("driverType :" + mvcModel.getDriverName()); //THIS PRINTS OUT THE LATEST DATA!!!
//*** #5 ***
driverName.setText(String.valueOf(mvcModel.getDriverName())); //THIS DOESN'T AFFECT THE SCREEN?? WHY :-(
}
}
#Override public void onCreate(Bundle savedInstanceState)
{
golfHomeScreen = this;
refreshData = indViewById(R.id.golfhomescreen_button_refreshData);
driverName = (TextView) findViewById(R.id.golfhomescreen_text_drivername);
}
#Override public void update(Observable observable, Object data)
{
// #1 ***
System.out.println("driverType :" + mvcModel.getDriverName());
golfHomeScreen.runOnUiThread(new Runnable()
{
#Override
public void run()
{
refreshData.performClick(); //*** #2 ***
}
});
}
}
I'll run a few suggestions and a few fixes and hopefully your problem will be gone.
I'm saying that because conceptually you do have some wrong stuff, and my answer is kind of fixing those concepts:
remove GolfHomeScreen golfHomeScreen = null; and golfHomeScreen = this; you don't need an object referencing to itself. It's just making confusion. This line golfHomeScreen.runOnUiThread(new Runnable() you should just call runOnUiThread(new Runnable()
Change System.out.pr*** to Log.d(TAG, MESSAGE); that's just because that is Android specialised way of dumping logs and it won't really matter for your result. It's nicer than standard Java System.out because allows you to filter those logs by TAG and severity.
Do never call refreshData.performClick(); or anything similar to that because you want the action inside that button to be performed. If you want a certain thing to happen from more than one source, create a method doThatThing(), put the actions in there, and from the Click and from the Update, you call the method. The way you're doing is a bit of a "Rube Goldberg machine" that is updating something, to click a button, to change a text. Just change the text.
do never compare a view ID with a number if ((v.getId()) == 12345) the ID of views created from XML are generated by the system during compilation and you can't know if it's a fixed number. You should compare with the static int ID of that view, like this : if(v.getId() == R.id.golfhomescreen_text_drivername). If the view was not created in XML (which is not the case here), you can compare with the actual object if(v.equals(driverName))
with those changes I believe your code will work. Make sure to let me know and mark as correct answer if in fact does.

Basics ---> Checking strings using if else to set value of an int, possible wrong use of onResume

So I'm still working on my first little app here, new to Android and Java, so I'm stuck on a basic little problem here. Answers to my first questions were really helpful, so after researching and not coming up with anything, I thought I'd ask for some more help!
The idea is that on another screen the user makes a choice A, B, C, or D, and that choices is passed as a string through the intent. OnResume checks if the choice is not null and sets an integer that corresponds to that string. Later when the user pushes another button, some if else logic checks that int and performs and action based on which was chosen. The problem is that the App crashed at onResume.
I learned that I have to use equals(string) to compare string reference, but maybe the problem is that I am trying to compare a string in reference to a literal string? Any help would be greatly appreciated.
Thanks!
protected void onResume() {
super.onResume();
// Get the message from the intent
Intent intent = getIntent();
String choice = intent
.getStringExtra(ExtensionSetupSlector.TORQUE_SETUP);
// Create the text view
TextView displayChoice = (TextView) findViewById(R.id.displayChoice);
if (!choice.equals("")){
displayChoice.setText(choice);
if (choice.equals("A")) {
myChoice = 1;
}
if (choice.equals("B")) {
myChoice = 2;
}
if (choice.equals("C")) {
myChoice = 3;
}
if (choice.equals("D")) {
myChoice = 4;
}
}
}
myChoice is declare right after ...extends Activity{ Also I'm not quite sure If this should really be in onResume, but it was working before I started try to set myChoice in the onResume (when I was just displaying the choice). Thanks again!
Change if (!choice.equals("")) to check for null instead. Otherwise your app attempts to access an empty reference and crashes.

Categories

Resources