I've listed some items in list that extends Activity and the list are listed using Custom Adapter. My question is, this is my xml File I've added some items to this spinner. How can i get the spinner values to next layout. Anyone knows then please tell me? Advance thanks.
I'm not clear on what you're actually asking here, but as a guess there are two possible things you're asking
How to get the currently selected value out of the Spinner
How to set the same value to a spinner in the next layout
1.
Is simple enough
((Spinner)findViewById(R.id.spinner1)).getSelectedItem()
Will return the object selected by your spinner.
Is slightly more complex, you'll need to determine what index in the data supplied corresponds to the result you get from getSelectedItem(), for example if you had an array of Strings then you could search through until you found the index and then set it on the new spinner.
For example:
String[] options = new String[] {"One","Two","Three","Four"};
String val = (String)((Spinner)findViewById(R.id.spinner1)).getSelectedItem();
//.......pass this to a layout/activity etc.........
for (int i=0; i<options.length; i++)
{
if (options[i].equals(test))
{
((Spinner)findViewById(R.id.spinner2).setSelection(i);
break;
}
}
But your best bet would be to try and explain more clearly what you're asking.
first you have to select data from spinner using
spinnerobject.setOnItemSelectedListener(
new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id)
{
Spinner spinnerobject = (Spinner) findViewById(R.id.Spinner02);
string value = (String)spinnerobj.getSelectedItem();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
then u hava to use intent for sending it to next activity..
Use
Intent.putExtra(..):
intent.putExtra("keyName", "somevalue");
This method is overloaded and takes various types as second argument: int, byte, String, various arrays..
To get the data out use appropriate getXYZExtra(). For String this is:
getStringExtra(String keyName)
First thing :: you can pass value from one activity to second activity not one layout to second
second :: if you need to pass value from one activity to second use
first activity::
activity.putExtra("lastpage", lastscore5);
*here lastpage is key which unique for aplication
second activity::
Intent i1 = getIntent();
var = i1.getIntExtra("lastpage", 1);
Related
I have an array of Spinners, hours[]. I am setting it up in a loop. When an item is selected, I need to take it and insert it into a database table, so I need the value of the loop counter in the onItemSelected() function. How do I do this? Here is the code:
for(int i=0; i<36; i++)
{
hours[i].setAdapter(adapter);
hours[i].setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> adapterView, View view, int x, long l)
{
String in=String.valueOf(adapterView.getSelectedItem());
DBHelper db=new DBHelper(AcceptTimetable.this, null, null, 1);
db.changeHour((int)Math.ceil(i/6), (i+1)%6, new Subject(in));
//need to use i here, but it's giving an error
db.close();
}
public void onNothingSelected(AdapterView<?> arg)
{
//do nothing
}
});
}
Also, I'm not sure String in=String.valueOf(adapterView.getSelectedItem()); is correct. Can anyone tell me how to get the selected value from the Spinner? Thanks a lot.
Try this..
for(int i=0; i<36; i++)
{
Log.v("Select Items in Spinner",hours[i].getSelectedItem().toString().trim());
}
From that log you can see the results.
There are two ways to do this.
Easier, but less efficient is to include hours[i].setPrompt(String.valueOf(i)) and then retrieve that value inside the listener via int index = Int.parse(((Spinner) adapterView).getPrompt()).
The more efficient way is to extend the Spinner in a separate class and add an index attribute and keep the index in it.
EDIT: the getSelectedItem() method returns an object whose type matches the one you specified when initializing the adapter you've bound to your AdapterView
I have a Custom ListView of products. Each row of listview contains product name, image, price and description. I want to send the listview's selected item's title, price, description and image be passed from current ListView activity to next activity where it is to be displayed. How do I pass data of selected list item in intent?
This is part of code from my java file which displays the list:
private ArrayList<ThemeTourModel> GetThemeTourResults(){
ArrayList<ThemeTourModel> results = new ArrayList<ThemeTourModel>();
ThemeTourModel item_details = new ThemeTourModel();
item_details.settourname("Solo Woman Travellers");
item_details.settourDescription("Women searching the ultimate liberation may discover it in exploring the tourist destinations of world on their own. More freedom and security while traveling for leisurely getaways and adventures.");
item_details.setImageNumber(1);
item_details.setPrice("Rs.6999 - Rs.8999");
results.add(item_details);
return results;
}
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
// TODO Auto-generated method stub
if(position==0){
Intent i0 = new Intent(this,EnquireActivity.class);
startActivity(i0);
}
You can put the primitive data through the Intent to another activity by intent.putExtra(name,value) and get the values through getIntent().getExtra() on the another Activity. You can find the reference here: http://developer.android.com/reference/android/content/Intent.html#putExtra(java.lang.String, android.os.Bundle)
The other way is to let your item object implement Parcelable to exchange between activities, check this out: Android: How to implement Parcelable to my objects?.
Hope this helps
I am developing an app where i need to set spinner values dynamically based on previous screen values. My code...
Main.java.
String[] values = {"All","Only Walk-in","Only Phone","Only Web","Walkin-phone","Walkin-web","phone-web"};
/*ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item,apttypes);
spinner.setAdapter(adapter);*/
But here what i want is from previous screen i am getting some value (like spinner postion). based on that i need to set spinner value to display...
Means from previous screen if i got values =0 means,
i need to set spinner value to display "All" from values array at top.
If i got value= 5 means,
i want to set spinner value to display as "Walkin-web"
How can i do that. can anyone help me with this...
Pass the value in from the previous Activity using extras in the Intent you use to launch it. Then when you've read the value call
int position = getIntent().getIntExtra( "name", defaultValue );
spinner.setSelection( position );
Which will move the spinner to the index you selected.
Use following array code and create new array adapter each time
String[] str=new String[maxsize-4];
you can implement onItemClick event on Spinner like this and setSelection(position)
//Spinner OnItemClick Event here
payfeeTabStudentNameSpinner.setOnItemSelectedListener(new OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
payfeeTabStudentNameSpinner.setSelection(position);
spinnerSelectedValue = parent.getItemAtPosition(position).toString();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Inside your First Activity A.java
public static String Position = YOUR_SPINNER1.getSelectedItem().toString();
Inside your Second Activity B.java
if(A.Position.equals("0")){
//Set your adapter accordingly
}else if(A.Position.equals("1")){
//Set your adapter accordingly
}
You can assign the Spinner's Position using the following code..
Spinner s1;
-----------
-----------
int position=valueFromPrevious;
s1.setSelection(position);
-----------
-----------
pass the values from previous activity using Extras and then when u want use it in your current activity follow this:
If u get String value then typecast it into interger by parseInt method...
String strtext4 = getIntent().getStringExtra("qualification");
then
int position = Integer.parseInt(strtext4);
after this just set it to your spinner
qualificationspinner.setSelection(position);
How do I use .get[position] and name in .putextra in Android, and how can I use position in putextra using an array?
ainListView = (ListView) findViewById( R.id.mainListView );
mainListView.setOnItemClickListener(new OnItemClickListener() {
int[] myImageList = new int[]{R.drawable.koala_copy, R.drawable.lighthouse_copy};
public void onItemClick(AdapterView<?> parent, View view, int pos,
long id) {
Intent b = new Intent(SimpleListViewActivity.this, Nextclass.class);
b.putExtra("Name", myImageList.get[position]);
b.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
finish();
startActivity(b);
}
private ArrayAdapter<String> getListAdapter() {
// TODO Auto-generated method stub
return null;
}
});
it is very unclear what you mean. and your code as well. Firstly what are you trying to put in the intent? It looks like you're trying to get a position in an array. .get is not a valid array method. an arraylist yes, but not array. Even so, you wouldn't just put a blank variable.
What i suspect that you want the position of a listview selected put into an intent and then fire off to the next activity.
b.putExtra("Name", pos);
is all you need for that, believe it or not, since the int pos (position) conveniently comes as a parameter on the onclicklistener. Perhaps on the next activity you want to display 1 of the two images in an imageview. There you can use a simple if statement, or case if you plan to put more, to choose what image to show. As of now i can see no business of that int array there being where it is.
EDIT:
There is no magic in the implementation of displaying the image in the next activity. As one would intuit, you get the int that you just put in the intent. Then depending on what the number is, you perform an imageview drawable assignment on a new drawable.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int listPos = getIntent().getExtras().getInt("Name");
ImageView image = (ImageView) findViewById(R.id.imageView1);
switch (listPos) {
case 0: image.setImageResource(R.drawable.koala_copy);
break;
case 1: image.setImageResource(R.drawable.lighthouse_copy);
break;
}
}
as for getting the image from an sd card, perhaps this web page will help:
http://android-er.blogspot.com/2010/07/load-bitmap-file-from-sd-card.html
it has a sample file you can download, too.
Finally, in order to not just give you fish for the day, but help you fish - may i suggest some video tutorials? This is a great resource in addition to stackoverflow for acquiring knowledge about android development: http://thenewboston.org/list.php?cat=6
I declare my Spinner in the following manner (it's very static so I
have 2 string arrays in array.xml for titles and values)
<Spinner
android:id="#+id/searchCriteria"
android:entries="#array/searchBy"
android:entryValues="#array/searchByValues" />
I expect spinner.getSelectedItem() to return an array [title, value]
but in fact it returns just a title String. Is it ignoring
android:entryValues? How do I get a value, not a title from it? Is
this doable with XML only or do I need to create adapter and do it
programmatically?
Rather than the dual array method, why not fill your ArrayAdapter programmatically with objects of a known type and use that. I've written a tutorial of a similar nature (link at the bottom) that does this. The basic premise is to create an array of Java objects, tell the spinner about the, and then use those objects directly from the spinner class. In my example I have an object representing a "State" which is defined as follows:
package com.katr.spinnerdemo;
public class State {
// Okay, full acknowledgment that public members are not a good idea, however
// this is a Spinner demo not an exercise in java best practices.
public int id = 0;
public String name = "";
public String abbrev = "";
// A simple constructor for populating our member variables for this tutorial.
public State( int _id, String _name, String _abbrev )
{
id = _id;
name = _name;
abbrev = _abbrev;
}
// The toString method is extremely important to making this class work with a Spinner
// (or ListView) object because this is the method called when it is trying to represent
// this object within the control. If you do not have a toString() method, you WILL
// get an exception.
public String toString()
{
return( name + " (" + abbrev + ")" );
}
}
Then you can populate a spinner with an array of these classes as follows:
// Step 1: Locate our spinner control and save it to the class for convenience
// You could get it every time, I'm just being lazy... :-)
spinner = (Spinner)this.findViewById(R.id.Spinner01);
// Step 2: Create and fill an ArrayAdapter with a bunch of "State" objects
ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(this,
android.R.layout.simple_spinner_item, new State[] {
new State( 1, "Minnesota", "MN" ),
new State( 99, "Wisconsin", "WI" ),
new State( 53, "Utah", "UT" ),
new State( 153, "Texas", "TX" )
});
// Step 3: Tell the spinner about our adapter
spinner.setAdapter(spinnerArrayAdapter);
You can retrieve the selected item as follows:
State st = (State)spinner.getSelectedItem();
And now you have a bona fide Java class to work with. If you want to intercept when the spinner value changes, just implement the OnItemSelectedListener and add the appropriate methods to handle the events.
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
// Get the currently selected State object from the spinner
State st = (State)spinner.getSelectedItem();
// Now do something with it.
}
public void onNothingSelected(AdapterView<?> parent )
{
}
You can find the whole tutorial here:
http://www.katr.com/article_android_spinner01.php
So if you came here because you want to have both label and value in the Spinner - here's how I did it:
Just create your Spinner the usual way
Define 2 equal size arrays in your array.xml file. One for labels, one for values
Set your Spinner with android:entries="#array/labels"
In your code - when you need a value do something like this (no you don't have to chain it)
String selectedVal = getResources().getStringArray(R.array.values)[spinner
.getSelectedItemPosition()];
And remember - these 2 arrays have to match each other as far as number slots and positions
Abort, abort! I don't know what got into me but Spinner does not support android:entryValues attribute. That one is actually from ListPreference which does a similar thing (displays list of items in pop-up dialog). For what I need I will have to (alas) use the SpinnerAdapter