android spinner value set - android

am having activity First here am sending values got in to Second activity.
in second activity contain textview and spinner . here spinner contains values as johncena ,Rock,undertaker....
so in activity am getting values as card2 and Rock which am passing to nextscreen through intent.so in next activity
cardNumber.setText(cardReceived); so here for textview we are giving like above for spinner how we have to do.
so when i navigate from first activity to nextactivity spinner value has to display as Rock.
but it shows default value johncena.
similarly if i get values as card 3 and undertaker then if i navigates to second activity then it has to display card3 and undertaker (for spinner).
but it shows card3 and johncena (default value)
Intent send =new Intent(First.this, Second.class);
send.putExtra("card",cardList.get(position));
send.putExtra("name",nameList.get(position));
startActivity(edit);
String cardReceived = getIntent().getStringExtra("card");
cardNumber =(EditText)findViewById(R.id.cardnumber);
cardNumber.setText(cardReceived);
String nameReceived = getIntent().getStringExtra("name");
System.out.println("name "+nameReceived);
Spinner NameDetail = (Spinner) findViewById(R.id.spinner1);
List list = new ArrayList();
list.add("Johncena");
list.add("Rock");
list.add("UnderTaker");
ArrayAdapter dataAdapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
cardDetail.setAdapter(dataAdapter);

I think you will need to use:
// Set this integer to be between 0 and N-1, where the adapter has N entries
// 0 is the first you put in the adapter, N-1 the last
int selection = 2;
cardDetail.setSelection(selection);

Related

Set selection at beginning of spinner with simplecursoradapter

I have an activity A with a listView. I've got a few items in there. If I click in one of those items, I navigate with an Intent to another page. In that other poage/Activity, I have a spinner, which contains the items of the listView ( which is populated with database). The problem is, every time, i go to that activity B, the item displayed in the spinner is always the first in my database. How can I set the item to be displayed depending on the item I clicked in the listView ?
Here's how I populate my spinner (dropdown mode) :
spinner = (Spinner)findViewById(R.id.spinner_branches);
final Cursor cursor = dbhelper.getAllCours();
String[] from = { "branche_cours" }; int[] to = { android.R.id.text1 };
adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cursor, from, to, 0);
spinner.setAdapter(adapter);
adapter.notifyDataSetChanged();
When I navigate, i pass the selected item with the intent. I remind you that my listView contains the same items of the spinner..
String selected_brancheold= intent.getExtras().getString("branche");
Thank you guys !
You can do one thing if your ListView list and Spinner List both are same.
Activity A
Intent intent = new Intent(YOUR_CONTEXT, ActivityB.class);
intent.putExtra("position",PASS_CURRENT_POSITION_LISTVIEW_ITEM_CLICKED);
startActivity(intent);
Activity B (After setting up the spinner adapter)
int currentPosition = getIntent().getIntExtra("position",0);
spinner.setSelection(currentPosition);
Hope this works for you!!

How to set a string value in Spinner view..?

In spinner view values at andoid(i.e..one,two,three),now i selected two,then i change selected value two as string.after i will asign two(string value) as set to one spinner.
In order to have a spinner, follow this steps
Declare Spinner number
and String Number as global
In onCreate()
number = (Spinner) findViewById(R.id.yourSpinnerId);
addItemsOnSpinner()
Then write addItemsOnSpinner()
public void addItemsOnSpinner() {
List<String> list = new ArrayList<String>();
list.add("one");
list.add("two");
list.add("three");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
number.setAdapter(adapter);
}
How to set a string value in Spinner view..?
If I understand your question correctly, you want to set the selected value (2) as a string right ? You can write in this way
Number=number.getSelectedItem().toString()
The Number will now holds the spinner item, number as a string.

ArrayAdapter wont load string from another activity

I am trying to send data of a List Array to an Array Adapter in another activity. This is based arround a smiple counter app that when i open the second activity it shows which buttons have been pressed and how many times.
So far i have created my array list the first activity
String player1data = lifepointsP1.getText().toString();
ArrayList<String> myList1 = new ArrayList<String>();
myList1.add(player1data);
and this updates everytime the lifepointsP1 text is changed. Then on the button pushed to open the new activity i do this:
Intent mi = new Intent(v.getContext(), MatchHistory.class);
mi.putExtra("p1L", myList1);
startActivity(mi);
which sends the data across to the second activity and i retrieve it like this:
String p1List = (getIntent().getStringExtra("p1L"));
i then create my array adapter and insert that string as the value:
lvP1 = (ListView) findViewById(R.id.lvP1R);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(MatchHistory.this, android.R.layout.simple_list_item_1, p1List);
lvP1.setAdapter(arrayAdapter);
although, this then gives me an error and tells me to take out the 'p1List' string out of the arrayadapter parameters. i dont know why it is doing this.
As other mentioned you are using wrong data type to get the extra from intent.
Replace this line:
String p1List = (getIntent().getStringExtra("p1L"));
With:
ArrayList<String> p1List = getIntent().getStringArrayListExtra("p1L");

how to set checkbox and spinner with repective values in android

I am trying to manipulating checkbox and with respective spinner and sending value to next page.
Here, If first check asia check box and choose on of the country from the first called asia spinner. same as check europe also checked and choose one of the country from the europe spinner.
If i click setdetails button, then go to next page and set all deatils of both checked country. and filly save all countries deatils on database.
Here some sample pictures, that display my problem.
First, you provide spinner with an adapter to fill it's values which looks like this:
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
To then get checked value you write:
String selectedParent = parentSpinner.getSelectedItem().toString();
To set the value you do this:
parentSpinner.setSelection(i);
where i is an index in the list you provided (i.e. 0 means "first element in the list").
To pass those values to the next activity you use set extras to Intent:
Intent i = new Intent(TaskViewActivity.this, TaskViewActivity.class);
i.putExtra("status", status);
TaskViewActivity.this.startActivityForResult(i, 0);
To fetch them from the activity you called you do this:
Bundle extras = getIntent().getExtras();
task_id = extras.getLong("status");
I hope that helps!

How do I set spinner value to string

I have a spinner set up like this:
ArrayAdapter<String> states = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, getResources().getStringArray(R.array.stateabbrev));
states.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
state.setAdapter(states);
As you can see, the source is an array.xml file.
I want to know how to populate it if I know the array value. For instance, I am retrieving information from my database and the user is from "KY" so I have a string "KY" and I want the spinner selection to be on "KY"
at first we should get position of "KY"
int position = states.getPosition("KY");
after that, select in spinner with position
state.setSelection(position);

Categories

Resources