I am building an app and on my main activity I have a spinner, now I want the selected value to be send to my other activity when I press a button.
I have done this with succes for a couple of EditText's. But for a spinner it seems a bit tricky.
I am following the documentation from de developer.android.com site but I don't really understand it. link: http://developer.android.com/guide/topics/ui/controls/spinner.html
My code where I fill the spinner with data from an array:
I do this code in the onCreate method:
//supply spinner with the array using an instance of ArrayAdapter
Spinner spinner = (Spinner) findViewById(R.id.timespan_spinner);
//create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.timespan_spinner, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
This is how I send the data drom the EditText's to my other activity:
This is in my sendForm method
public void sendForm(View view) {
//creating an Intent
Intent intent = new Intent(this, DisplayFormActivity.class);
//defining fields
EditText editTextFirstname = (EditText) findViewById(R.id.txtFirstname);
EditText editTextLastname = (EditText) findViewById(R.id.txtLastname);
EditText editTextBedrag = (EditText) findViewById(R.id.txtBedrag);
//getting the field values
String firstname = editTextFirstname.getText().toString();
String lastname = editTextLastname.getText().toString();
String bedrag = editTextBedrag.getText().toString();
//putting data in the intent
intent.putExtra(FIRSTNAME, firstname);
intent.putExtra(LASTNAME, lastname);
intent.putExtra(BEDRAG, bedrag);
startActivity(intent);
}
You can take a look at this answer, quoting:
Spinner spinner = (Spinner)findViewById(R.id.spinner);
String text = spinner.getSelectedItem().toString();
So, for your code:
public void sendForm(View view) {
//creating an Intent
Intent intent = new Intent(this, DisplayFormActivity.class);
//defining fields
EditText editTextFirstname = (EditText) findViewById(R.id.txtFirstname);
EditText editTextLastname = (EditText) findViewById(R.id.txtLastname);
EditText editTextBedrag = (EditText) findViewById(R.id.txtBedrag);
Spinner spinner = (Spinner) findViewById(R.id.timespan_spinner);
//getting the field values
String firstname = editTextFirstname.getText().toString();
String lastname = editTextLastname.getText().toString();
String bedrag = editTextBedrag.getText().toString();
String chosenOption = spinner.getSelectedItem().toString();
//putting data in the intent
intent.putExtra(FIRSTNAME, firstname);
intent.putExtra(LASTNAME, lastname);
intent.putExtra(BEDRAG, bedrag);
intent.putExtra(CHOSEN_OPTION, chosenOption);
startActivity(intent);
}
Related
I have a
EditText- addArea = (EditText) findViewById(R.id.editTextArea);
and ArrayList - public static ArrayList<String> areaList = new ArrayList<String>();
and a Button- addbtn = (Button) findViewById(R.id.addBTN);
This is my code to Store Strings into array. It displaying the toastMessage but I doesn't know whether its storing Strings or not.
addbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String area = addArea.getText().toString();
if (!area.isEmpty()){
areaList.add(area);
toastmessage(area+" added successfully.");
addArea.setText("");
}
}
});
What is need is, The spinner is in the next layout. When I click the spinner it should diplay the items that i have enterd in the edittext.
To check whether the list is added, you can check by this way
for (int i=0; i<areaList.size();i++){
addArea.append(areaList.get(i));
addArea.append("\n");
}
You can know whether the string is stored into the areaList by checking the textView setText value.
To pass the areaList to next Activity, add this
Intent intent = new Intent(MainActivity.this, secondActivity.class);
intent.putExtra("areaList",areaList);
In the second Activity, use this line to get all the areaList data
ArrayList<String> areaList = (ArrayList<String>)getIntent().getSerializableExtra("areaList");
Then set the areaList to spinner
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, areaList);
spinner.setAdapter(adapter);
In order to see make sure the item is added you can print the array or check its size.
First declare a global variable as int lastSize;
lastSize = 0; //we start from 0
addbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String area = addArea.getText().toString();
if (!area.isEmpty()){
areaList.add(area);
//size has changed, means new item added successfully
if (areaList.size() > lastSize) {
toastmessage(area+" added successfully.");
addArea.setText("");
}
//lets update lastSize to record latest size
lastSize = areaList.size();
}
}
});
hi i have tried the following code to store the user input values of EditText to an array in another activity and display the values in the array. every time the user input values in EditText of Activity01. it should be stored in an array on the Activity02 to display all the values of user inputs.
The ListView displays one last entered value from Array.
This is the code what i have tried.
Activity01.java
Button GrmBtnSave = (Button) findViewById(R.id.grmbtnsave);
Grmitem = (AutoCompleteTextView) findViewById(R.id.itemsearch);
GrmBtnSave.setOnClickListener(new OnClickListener(){
public void onClick(View view){
Intent intent = new Intent ( ItemSearch.this, MainInvoiceActivity.class );
intent.putExtra("GRM_ITEM", Grmitem.getText().toString());
startActivity(intent);
}
}
});
Activity02.java
ListView lv1;
ArrayList<String> list = new ArrayList<String>();
ArrayAdapter<String> adapter;
protected void onCreate(Bundle savedInstanceState) {
//.....
//.....
lv1=(ListView)findViewById(R.id.getalllist);
adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, list);
lv1.setAdapter(adapter);
String val="";
if(getIntent().getStringExtra("GRM_ITEM")!=null && getIntent().getStringExtra("GRM_ITEM").length() > 0){
val = getIntent().getStringExtra("GRM_ITEM");
list.add(val);
}
adapter.notifyDataSetChanged();
}
I tried solving it but still i get the last entered value in the EditText.
Thank you for help
The static keyword will do the magic.
Make change in declaration of list in your activity02.java like this. I have tested it. and it works.
static ArrayList<String> list = new ArrayList<String>();
I'm new to Android developing programming. I'm trying to pass through multiple information from my starting activity to a new activity and i can't figure out how to retrieve all of the values.
My MainActivity buttonSend event code is this:
public void sendInformation (View view) {
Intent intent = new Intent(this, DisplayInformation.class);
EditText toText = (EditText) findViewById (R.id.toText); //Finds the control 'toText' in the form and gets it by ID
EditText fromText = (EditText) findViewById (R.id.fromText); //Finds the control 'fromText' in the form and gets it by ID
EditText messageText = (EditText) findViewById (R.id.messageText); //Finds the control 'messageText' in the form and gets it by ID
EditText subjectText = (EditText) findViewById (R.id.subjectText); //Finds the control 'subjectText' in the form and gets it by ID
String toMessage = toText.getText().toString(); //Gets the text that you enter for the 'To'Field
String fromMessage = fromText.getText().toString(); //Gets the text that you enter for the 'From' field
String messageMessage = messageText.getText().toString(); //Gets the text that you enter for the 'Message' field
String subjectMessage = subjectText.getText().toString(); //Gets the text that you enter for the 'Subject' field
intent.putExtra(EXTRA_MESSAGE, toMessage);
intent.putExtra(EXTRA_MESSAGE, fromMessage);
intent.putExtra(EXTRA_MESSAGE, messageMessage);
intent.putExtra(EXTRA_MESSAGE, subjectMessage);
startActivity(intent);
}
EXTRA_MESSAGE is defined as public final static String EXTRA_MESSAGE = "com.example.Fun.MESSAGE"; I don't really know what this does to be honest the tutorial just told me to include it.
In my second activity page i have this code for the onCreate instance:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_information);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
Intent intent = getIntent();
String toMessage = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
String fromMessage = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
String subjectMessage = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
String messageMessage = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView toMessagetextView = new TextView(this);
toMessagetextView.setTextSize(40);
toMessagetextView.setText(toMessage);
TextView fromMessagetextView = new TextView(this);
fromMessagetextView.setTextSize(40);
fromMessagetextView.setText(fromMessage);
TextView subjectMessagetextView = new TextView(this);
subjectMessagetextView.setTextSize(40);
subjectMessagetextView.setText(subjectMessage);
TextView messageMessagetextView = new TextView(this);
messageMessagetextView.setTextSize(40);
messageMessagetextView.setText(messageMessage);
setContentView(toMessagetextView);
setContentView(fromMessagetextView);
setContentView(subjectMessagetextView);
setContentView(messageMessagetextView);
}
When I go to run my app it errors out and shuts down.
*Also if anybody could tell me how to put the TextView in the fragment.xml file for the second activity it would be greatly appreciated. I know how to declare the id and what not but i don't know to use it with retrieving data from another activity yet.
Thank you in advance for any help.
You are using same key EXTRA_MESSAGE for all extra values. Intent stores values like Map.
So here you are loosing all the value other than subjectMessage, because it was added at last.
You have to use different keys for each values. like
public final static String EXTRA_MESSAGE_TO = "com.example.Fun.MESSAGE_TO";
public final static String EXTRA_MESSAGE_FROM = "com.example.Fun.MESSAGE_FROM";
public final static String EXTRA_MESSAGE_SUBJECT = "com.example.Fun.MESSAGE_SUBJECT";
public final static String EXTRA_MESSAGE = "com.example.Fun.MESSAGE";
I'm working on a program that uses a ListView which contains strings that a user enters. I want it to list out each string after the user clicks the button. Also I want every string value that is displayed in the ListView to also be in an array so that I can manipulate the list later. This is what I have right now:
public ArrayList<String> choices = new ArrayList<String>();
public String[] choicesArray;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ListView listView = (ListView) findViewById(R.id.listView1);
choicesArray = new String[] { "You have not entered a choice yet" };
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1,
choicesArray);
listView.setAdapter(adapter);
final Button addButton = (Button) findViewById(R.id.Button1);
addButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText editText = (EditText) findViewById(R.id.edit_choice);
String message = editText.getText().toString();
adapter.add(message);
//choices.add(message);
//choicesArray = choices.toArray(choicesArray);
//listView.setAdapter(adapter);
}
});
}
This code creates an error:
FATAL EXCEPTION: main
java.lang.UnsupportedOperationException
at java.util.AbstractList.add(AbstractList.java:404)
at java.util.AbstractList.add(AbstractList.java:425)
at android.widget.ArrayAdapter.add(ArrayAdapter.java:179)
at MainActivity$1.onClick(MainActivity.java:38)
The reason your list is not updating is that you are not ever adding your data items to the Adapter. You are only adding them to choicesArray which is the array that you passed to the new ArrayAdapter() constructor.
That constructor does not tie the adapter to the array, but basically copies the array into the adapter (at the time that you call the constructor) Changes made to the array after that will not be reflected in the ArrayAdapter.
I like to think of it that ArrayAdapter<> is basically the same thing as ArrayList<> with the added bonus of being able to generate child views for some AdpaterView parent. You can pass it an array[] to start with and it will initialize itself with the values in that array, but it does not link the array to the to the Adapter, or to the ListView. After that initial constructor call any changes to either one will not be reflected in the other.
So you should change your code to look like this:
addButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText editText = (EditText) findViewById(R.id.edit_choice);
String message = editText.getText().toString();
adapter.add(message);
//adapter.notifyDataSetChanged(); //<-- you might need to call this, but I think it might work without if you do it this way.
//listView.setAdapter(adapter); //<-- you shouldn't need to call this each time. Once at the begining is enough.
}
});
Edit:
change your ArrayAdapter constructor and remove the choiceArray parameter so you should be left with this:
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1);
after that I think you should be good to go. Essentially the problem was that since you were passing in an array, and since arrays are of a fixed size (yours was length = 1) it was not allowing you to add anything. I suspect you could probably use an ArrayList<String> instead of String[] if you wanted, but honestly for your situation I think it will be easier to just skip that parameter all together and add items directly to your adapter whenever you need to add new ones.
after adding the new String to your list you just use notifyDataSetChanged() of your adapter which tells the adapter to update the display and it'll update the ListView with the new string
addButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText editText = (EditText) findViewById(R.id.edit_choice);
String message = editText.getText().toString();
choices.add(message);
choicesArray = choices.toArray(choicesArray);
adapter.notifyDataSetChanged();
}
});
Update
you should consider making this change too
choicesArray = new String[choices.size()];
choicesArray = choices.toArray(choicesArray);
I have a problem to receive a text from edittext in one activity intent to a listview on another intent.
This is what I have done so far:
Activity B:
Button btn = (Button) findViewById(R.id.button2);
final EditText edit = (EditText) findViewById(R.id.editText1);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(addnote.this, MainActivity.class);
i.putExtra("text", edit.getText().toString());
startActivity(i);
Activity A: where the list view is I just don't know how to receive this text when I clicked the button 2
Basically all you want to do is call getStringExtra() to grab whatever you originally assigned during putExtra(). So for example: In Activity A's onCreate(), you'll want to do this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_a);
mYourString = getIntent().getStringExtra("text");
}
Keep in mind, that Android recommends prefacing the names of the objects your storing with the application's package name. So instead of using "text" you should use "com.example.myproject.text".
thank you for that but I have problem receiving the data and placing it into my list view in my activity A i have done so far :
Intent i = getIntent();
String mdata = getIntent().getStringExtra("text");
ListView listv = (ListView) findViewById(R.id.notelist);
ArrayAdapter<String> Adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, mdata);
listv.setAdapter(Adapter);