Android Pass data from EditText to notification to dialog - android

I have an activity that has an EditText. After a user enters their text, the app may use that text later to make a notification. I can set the notification text just fine, but when the user clicks on the notification, it launches a dialog box that should have the same text. I have tried putExtra with the PendingIntent but that only displays the latest text in the dialog no matter which notification was selected. Is there a way to assign each string from the EditText a number and have the dialog load the text from what number it is?

try following logic,
make one static string variable,
private static strEditText = null;
at the time of EditText input, just store its value ion to strEditText variable like below,
strEditText = EditText.getText().toString().trim();
Now make one public static method, like below,
public static String EditTextValue()
{
return strEditText;
}
Now you can have this variable's value throughout whole project.
You can also try another method in android,
pass your arguments in Bundle

The PendingIntents are pooled/cached and the extra's don't make them different entries, so if you have a bunch of notifications with pendingIntents and the only difference between the intents are extras, then you'll end up with the notifications all using the one of the pendingIntents. [This sounds like what you're seeing, I remember this driving me nuts for a while]. You need to make your pending intents differ in something that the pool/cache cares about, like the data URI or action.

Related

Want the EditText to display in all Activitys

First of, I'm a noob in Android development....
So, my problem is - I have a Activity with a EditText view, so someone can put his name in it.
And what I want to do with that input - I want it to be displayed in all activities in TextViews where it needs to be.
Example - Like in a book -> Input name: What are you doing kid? Kir: "I don't know..." Input name: "Just don't do it anymore..."ect.
And then in the next Activity by clicking a Button, the same, I want that same input name to display in ALL ACTIVITES , TextViews where it needs to be, but I don't know how to declare it, where to declare it, what code ect.
I hope you all will understand what i mean.
And thank you for your support.
One way is by passing data into intents before starting the activity.
Intent intent = new Intent(context, MyActivity.class);
intent.putExtra("key_name", "John Cena");
context.startActivity(intent);
Then retrieve the data in MyActivity by:
Bundle extras = getIntent().getExtras();
String name = extras.getString("key_name"); // name should be John Cena
Simillar functionality also exists for Fragments.
Basically you wants to Preserve the Value\Data. For such scenarios, it is best to use [Preferences] where you can save your data on disk in a private file and read it anytime and anywhere.
Android Preferences

Android: why use intent.putExtra() method?

This is a real noob question I'm sure, but I am finding it quite perplexing.
Why an earth would you want to ever use intent.putExtra method to share information between classes in Android?
Let me explain. I am making my first Android app following the instructions from the developers guide (I am already at a moderate level with Java) and I am using some code that looks like this:
//Class field
//key holds string????? not fully understanding this...
public static final String EXTRA_MESSAGE = "self.anon.myfirstapp.MESSAGE";
//this method is activated by a button being pressed
public void sendMessage(View view) {
Intent intent = new Intent(this,DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
//puts string message inside the string EXTRA_MESSAGE - why????
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
OK firstly I want to point out I see what is happening and for the most part how it works (am just confused by the field declaration = "myClassPath" why?)...
BUT....
Surely it would be easier just to have a static field called:
public static String message;
then my method would look like this:
public void sendMessage(View view) {
Intent intent = new Intent(this,DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
message = editText.getText().toString();
startActivity(intent);
}
Then when my class DisplayMessageActivity needs the string message he just calls for:
String message = myClass.message;
That seems so much more straight forward. What is with the creation of the new string EXTRA_MESSAGE which just seems to hold the string message and why send it with the intent when my other class can access this info directly anyway -- and what does the field declaration with the "self.anon.myfirstapp.MESSAGE" mean? I can find no such folder or path leading to anything.
As someone else stated there are often situations (such as a screen rotate) in which the android system destroys and restarts the app - so all variable data is lost. It would only work consistently the way you suggest if your data is hard coded as a final variable. That is not the only reason for using intents though.
The great thing with using an intent to pass information is that you can use the intent not just to communicate with sub-activities within your own application but to any activity installed on that android system. For example you may want to launch an intent which starts the phone application and include as an extra the number that you want to call.
Perhaps a better question than yours though is "why would you not use intents to pass information?" The intent.putExtra() method allows you a convenient flexible and straight forward method to pass as much information as you like in a safe and secure way to any other activity.
intent.putExtra(EXTRA_MESSAGE, message);
works like a key value pair, when you want to retrieve the information from the intent you can simply do intent.get<type>Extra and get said information, in this case, intent.getStringExtra("self.redway.myfirstapp.MESSAGE'). its simply the key to retrieve the information, it does not have to be your entire classpath.
it could just as easily be intent.putExtra("message",message).
They are helpful when passing information that you don't necessarily want to reveal to another class but you do want it to be able to get that information in another manner from what i have found.
message = myClass.message It is not always certain that this will retain its value especially when it extends Android framework classes like Activity. When your activity is recreated(change of screen orientation) then message can lose its current value and be assigned a default value. myClass.message would work if message was a static field or else you would need to provide getter and setter methods for object of the Activity Class. Well creating objects of activity class is unheard of in my experience.

getStringExtra - Public Static Final - The Busy Coder's Guide to Android

I am on page 301 of this book and it is an example of an Activity getting "extras" from the intent that started it. I am fairly new to Java so maybe am missing something pretty obvious but...
I thought that when you declare a variable as "final" it meant that it doesn't change.
There is a line of code initialising a final variable:
public static final String EXTRA_MESSAGE="msg";
and then later in onCreate method:
tv.setText(getIntent().getStringExtra(EXTRA_MESSAGE));
The text displayed in the activity is not "msg" but is the string passed from the intent "I am the other activity". Why do you have to have the variable declaration above for the code to work? I don't understand what its doing.
Thanks
You are getting the extra received from another Activity indexed by the key 'msg'.
Like when you do this with the Intent used to start your Activity:
intent.putExtra("msg", "text going in the TextView");
The key is 'msg', but the value you get for the TextView is 'text going in the TextView'
Yes, final means EXTRA_MESSAGE value won't change, but you're not displaying EXTRA_MESSAGE value, but
getIntent().getStringExtra(EXTRA_MESSAGE)
which actually contains the value put in the previous activity. Regarding your question
Why do you have to have the variable declaration above for the code to work?
You don't actually need that variable for the code to work, but it's a good practice to use constant values instead of just hardcoding string values such in.-
getIntent().getStringExtra("msg")
The parameter you pass to getStringExtra is the key to which the String is mapped. All the extras you put in an Intent are mapped as key-value, so if you want to get a value you have to know the key, which must be the same key you used in the previous activity to save the value (with putStringExtra).
http://developer.android.com/reference/android/content/Intent.html#getStringExtra(java.lang.String)

Application with different requests sent to a database (SQL) depending on buttons clicked

I still am a beginner in Android development and will try to make my question as clear as possible with a schema of what I have in my mind.
Purpose of this application:
- I want the user to have the choice between a few buttons and when clicking on any of them, it would open a list view with different content according to the button.
ex : if you click on "Category_1" button, only elements with a fitting id will appear in the listview.
So far, I have :
- defined my "handler" class (extends SQLiteOpenHelper) : name/path of DB, definition of CRUD, .getReadableDatabase, etc.
- define a class for my table, in my case "Restaurants.java" with getters/setters and constructor.
- defined my MainActivity with empty listeners for my button.
- defined my "DatabaseAdapter.java" in which I want to define the methods/sql requests which will communicate with the database and get the information I want from it.
- defined my ListViewActivity with nothing to display so far.
Here is a schema of what I want with the idea of how to make it to try to optimize my application :
To sum up:
- I want a listener for each button setting a variable to a certain value (for example: "if you click on 1 then set the value of A to 1") and opening the ListViewActivity.
- There would be a method defined in "...Adapter.java" sending a request to the database and having the variable A defined earlier as an input.
- And then, when clicking on the button, the ListViewActivity will open and call the method from "..Adapter.java", and finally display the results.
So, here are my questions :
- First of all, is the design optimized enough to allow my application to run fast? I think it should as all the button open only one activity and there is only one method defined for all buttons.
- I have a hard time defining the method in "...Adapter.java" which will be called from my ListViewAcitivity. The input should be the variable obtained when clicking on the button but I don't really know how to get a variable in one activity, open a second activity where to display results by using the variable in a third activity... :s
Is it fine to set a variable to a certain value when we click on a button and use this variable in another class as an input for a method?
public findNameInTable(int A){
string sql = " select NAME from MY_TABLE where CAT1 = " + A;
c = database.rawQuery(sql, null); }
Thanks in advance for any indications, suggestions or links which could help me to make my application come true, and sorry if some questions really sounds newbie, I am starting !
Have a good day !
Part 1: The best way I have found to pass variables to other activities is with a putExtra(String, variable);. Say you change the variable name on a button press, you can then call:
YourNewActivityClassName var = new YourNewActivityClassName();
Intent i = new Intent(context, YourNewActivityClassName.class);
i.putExtra("name", name);
startActivity(i);
Then in the activity you just created, you can call this in the onCreate method:
Intent i = getIntent();
final String name = i.getStringExtra("name");
Of course this is assuming the variable was defined as a String before the putExtra was called.
If you want to use other variable types, there are different get***Extra commands you can call like getIntExtra(int, defaultval) but the putExtra will still be used to send it.
Part 2: For calling a method with a variable assigned in a button click, I have found the best way to do this is with a "holder class" this holder can be defined as a final, and a button press assigns a value to one of it's slots. Here is my holder for Integers:
public class holder {
int to;
public void setTo(int to){
this.to = to;
}
public int getTo(){
return to;
}
}
I instantiate my class as final within my on create final holder hold = new holder();
then call my hold.setTo(int); within a list click listener. When I want to get the data, I simply call hold.getTo(); and I have my integer.
Heres a similar post: Pass value outside of public void onClick
Hope this helps!
Mike

How do I display consecutive AlertDialogs in Android?

I'm trying to port one of my iPhone apps over to the Android. It was all going along swimmingly until I came to AlertDialogs. In the iPhone app, sometimes there will be more than one alert to pass to the user. When this happens, the first alert dialog will come up, and when they click it away the next one will come up.
I can't seem to get more than one dialog box to come up like that in Android. Is it possible to display back to back AlertDialogs where a second one pops up as soon as the first is finished?
You execute 2 consecutive call to 'showDialog()' after eachother and the second will show after the 1st was dismissed:
showDialog(FIRST_DIALOG_ID);
showDialog(SECOND_DIALOG_ID);
Ofcourse you also have to implement onCreateDialog().
If you feel that you will be having multiple dialogs, one after another, you could create a custom class that holds all of the information for the alert, such as the title, text, icon, etc. From there, create an arraylist to store the custom class objects. When you are done with your first dialog, remove it from the arraylist, then check to see if there are any remaining dialogs that need to be presented.
The only issue you'll run into is that it will be much more difficult if you want to have different conditions in your Confirm and Cancel options.
public class DialogObject(){
String title;
String body;
String iconName; // or just an Image asset
}
ArrayList<DialogObject> dialogList = new ArrayList<>();
When a dialog is required, add it to the list if there is a dialog already on screen
dialogList.add(new DialogObject(param1, param2, param3));
This may not be the best way, but it is an option. The ArrayList will need to be in a separate class itself so you don't lose the data when moving from screen to screen.
For example - Note the "static" keyword.
public class DialogHolder(){
public static ArrayList<DialogObject> = new ArrayList<>();
}

Categories

Resources