I have a problem with adding values to list which is in SecondActivity. In MainActivity I set text in EditText boxes and send to second class. For the first time values are adding, but when I back to previous activity and one more time set text and send it, values in the list are replaced, not added. Someone know what is the source of this problem?
try this in first activity:
String[] array = {"Hi", "there", "yeah"};
Intent goIntent = new Intent(this, NewAppActivity.class);
/*
* put extra with "array" as a key and the String[] with your values as the value to pass
* */
goIntent.putExtra("array", array);
startActivity(goIntent);
and in second activity:
Bundle extras = getIntent().getExtras();
if (extras != null) {
String[] array = extras.getStringArray("array");
}
Related
I want to use an intent string extra to dynamically determine what String array in an XML file to use.
Java code:
Intent myIntent = getIntent();
String stringID = myIntent.getStringExtra("stringID");//pull string id
String[] allStrings = getResources().getStringArray(stringID);
The XML:
<string-array name="set1">
<item>item1</item>
<item>item2</item>
<item>item3</item>
</string-array>
The last line in the java code doesn't work because it wants something like r.array.set1, but I want to choose this dynamically instead. How can I accomplish this? Would it be easier to use the ID of the string array somehow?
Actually r.array.set1 is a reference to an Integer. So you should pass it as an Integer, not a String. So:
Intent myIntent = getIntent();
int intID = myIntent.getIntExtra("stringID", r.array.set1);//The second parameter is the default value if nothing is specified
String[] allStrings = getResources().getStringArray(intID);
And in the other activity pass it as an Integer:
intent.putExtra("stringID", r.array.set1);
I am making a simple budget app and would like to add all inputted income then save this income to use in other classes. I'm lost and not sure how to do it. Here's the portion of my code inside of my onCreate method. I have income and incomeName both as Strings
addIncomeButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//TODO: Transfer this info to line in scroll view showing incomes
if (TextUtils.isEmpty(enterIncomeEditText.getText()) |
TextUtils.isEmpty(enterIncomeNamesEditText.getText())) {
Toast.makeText(Income.this, "Entry Empty", Toast.LENGTH_SHORT).show();
} else {
//create income and incomeName strings
income = enterIncomeEditText.getText().toString();
incomeName = enterIncomeNamesEditText.getText().toString();
mLayout.addView( createNewTextView(incomeName + " " + income));
}
}
});
You can use Intent.
Assume you want to pass all inputted data to Activity B
Intent intent = new Intent(getApplication(), ActivityB.class);
intent.putExtra("income",income);
intent.putExtra("incomeName",incomeName);
startActivity(intent);
Then in Activity B, you can use getStringExtra() to get all inputted value.
The value in string.xml file...
<string name="bangla_history_2ndpoint">SOME VALUE </string>
From this activity i am trying to pass value to another activity ... by using putextra
Intent ptwo=new Intent("com.powergroupbd.victoryday.of.bangladesh.HISTORYDESCRIPTION");
ptwo.putExtra("header", R.string.bangla_history_2ndpoint);
startActivity(ptwo);
Then get the value in this activity ...
But it is not getting the value from the string.xml file...
text_point = getIntent().getStringExtra("header");
Toast.makeText(getApplicationContext(), text_point, Toast.LENGTH_LONG).show();
But it is toasting blank ....
Please give a solution...
That's because you're trying to retrieve a String, but what you're passing in as extra is actually the resource identifier of it, an int. Either put an actual string as extra, or retrieve an int on the receiving end to fix this.
// put:
ptwo.putExtra("header", R.string.bangla_history_2ndpoint);
// get:
int extraResourceId = getIntent().getIntExtra("header");
text_point = getString(extraResourceId);
Or:
// put:
ptwo.putExtra("header", getString(R.string.bangla_history_2ndpoint));
// get:
text_point = getIntent().getStringExtra("header");
I'm trying to pass 2 variables through a couple of Android Activities. One of them keeps turning up as null on the last page:
The first Activity:
Intent intent= new Intent(RoundOptionActivity.this, MoveOptionActivity.class);
intent.putExtra("numRounds", "5");
startActivity(intent);
The second Activity:
Bundle extras = getIntent().getExtras();
if(extras !=null) {
numRounds = Integer.parseInt(extras.getString("numRounds"));
}
.........
Intent intent = new Intent(MoveOptionActivity.this, MoveActivity.class);
intent.putExtra("numRounds", numRounds);
intent.putExtra("playerChoice", playerChoice);
startActivity(intent);
(Note that at this point I printed numRounds in LogCat and it was set to the right number, and not null)
The Third Activity:
Bundle extras = getIntent().getExtras();
if(extras !=null) {
playerChoice = Integer.parseInt(extras.getString("playerChoice"));
numRounds = Integer.parseInt(extras.getString("numRounds"));
}
At this point, the application crashes at the line where I try to parse numRounds to an integer, with a NumberFormatException complaining that it can't parse a null value. There's never a problem with playerChoice, only numRounds. I've tried handling numRounds the exact same way as playerChoice, but nothing seems to work. What's going on? D:
You have to use extras.getInt("numRounds");
because in second Activity you added to putExtra int value:
numRounds = Integer.parseInt(extras.getString("numRounds"));
use
numRounds = extras.getInt("numRounds");
intead of
numRounds = Integer.parseInt(extras.getString("numRounds"));
because you are passing numRounds as Integer in intent.putExtra("numRounds", numRounds); from second Activity
or pass as if you want to receive as String:
Intent intent = new Intent(MoveOptionActivity.this, MoveActivity.class);
intent.putExtra("numRounds", numRounds+"");
intent.putExtra("playerChoice", playerChoice);
startActivity(intent);
As far as i think in your second activity you are setting numRounds a integer value in putExtra() i.e the integer variable numRounds thats why it causing problem. either get the numRounds in third activity as directly like extras.getInt("numRounds") or send value as String in second activity i.eintent.putExtra("numRounds", numRounds+"");
All- I have an app in which the user enters the names of players in a game. He/she can enter 2-4 players. The app takes the names and puts them into a spinner. When the user enters 4 players it works great but when they enter only 2 or 3 players, the spinner has 2 or 1 (respectively) empty spaces. How can I make it so when the user enters a number of players less than 4, only that number of names appears in the spinner (no empty spaces). Here is the code I am using:
String[] items = new String[] {"No Owner", message, message2, message3, message4};
Spinner spinner = (Spinner) findViewById(R.id.owner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
message= Player 1,
message2= Player2,
etc.
Sample code welcome, and thanks for your time.
EDIT:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
String message2 = intent.getStringExtra(MainActivity.ANOTHER_MESSAGE);
String message3 = intent.getStringExtra(MainActivity.YET_ANOTHER);
String message4 = intent.getStringExtra(MainActivity.AND_ANOTHER);
setContentView(R.layout.next_main);
You could check which messages are empty and then modify your items array based on that information. The goal being to pass an array to your ArrayAdapter with no extra spaces in it
Edit:
List<String> playersList = new ArrayList() ;
if(!message.equals("")){
playersList.add(message);
}
etc..
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, playersList);
Since you're importing your player names into a string, I would run string compare to see if each string matches the default value, if there is no player filling that slot. In other words, if
message.compareTo("")
returns 0, don't include that in items, which would be best used as an ArrayAdapter. You can do this through a simple if block.
Example code:
ArrayAdapter items = new ArrayAdapter<String>(this, int textViewResourceId);
if (message.compareTo("") != 0) {
items.add(message);
}
if (message2.compareTo("") != 0) {
items.add(message2);
}
....
And you would keep going with the rest of your items, using the resulting array (which you can pull out using toString()) to generate the Spinner.
EDIT: Fixed constructor code.
EDIT 2: Fixed textViewResourceId in the constructor.