how to set checkbox and spinner with repective values in android - 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!

Related

To set the positions of items in spinner

I am populating my spinner from database. I have a collection of mobile brands. I have added "Add a new brand" also... But when I am setting the spinner items from DB, it comes in somewhere middle.. I want it to go at the end.. Can i do it? if yes, how? please help, thanks in advance.
Spinner brand;
brand=(Spinner)findViewById(R.id.spinner_brand);
private void loadSpinnerData() {
// database handler
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
// Spinner Drop down elements
List<String> Brand = db.getBrands();
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, Brand);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
brand.setAdapter(dataAdapter);
}
Add time column in your brand table and on querying sort by time. So, you will get last added item at last in spinner.
Create an ArrayList and add all your database items to it. and then add your "Choose a brand" string to it. and then pass the ArrayList to Spinner.
Although I should warn you, you are "probably" doing it wrong. If you add "Choose a brand" item to spinner, it will also be selectable, which you might not want :)

android spinner value set

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);

spinner with no select option

I'm trying to create spinner which should not have any select but instead of it, it should show Blank, after clicking that items can be selected.
Here is my code, please help.
urineGlucoseSpinner = (Spinner) view.findViewById(R.id.spnner_urine_glucose);
ArrayList<String> ugList = new ArrayList<String>();
ugList.add(0,"");
ugList.add("1.5");
ugList.add("5.5");
ugList.add("0.8");
ugList.add("9.5");
ugList.add("12.0");
//ArrayAdapter<String> urineGlucoseAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_spinner_item, ugList);
ArrayAdapter<String> urineGlucoseAdapter = new ArrayAdapter<String>(getActivity(),R.layout.custom_spinner_text, ugList);
urineGlucoseAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
urineGlucoseSpinner.setAdapter(urineGlucoseAdapter);
urineGlucoseSpinner.setSelection(0);
urineGlucoseSpinner.setOnItemSelectedListener(new OnUGItemSelected());
By default spinner takes array 0th element if u not selecting any one..u have to make object of ArrayList and for 0th element u have to put "" (null Sting) inside semicolon and make it as 0th element...i think this is the only solution for your question..
ArrayList<String> ugList = new ArrayList<String>();
ugList.add("");
I can see two ways to do this.
1) Add the blank line to your data at position 0, and then create a custom spinner adapter and override the getView method and in it use an if to set the 0 position view to GONE (thus getting rid of the blank line in the listing).
2) An alternative might be setting an empty EditText in your form, and when it gains focus pop a listview in a dialog with your possible choices.

Spinners populated from database not registering selection

I'm having a problem implementing spinners and getting them to work the way I want. I have one spinner containing state abbreviations that is populated from an array. Depending on the state that the user selects, the application should perform a database query and populate the second spinner with locations of stores in the selected state.
The first spinner works fine, performs the query successfully and populates the second spinner. I want to get the value from the second spinner and display it in a toast message. Here is where the problem occurs. The second spinner is not registering clicks after it is initially filled.
In the example below, the user selects RI from the first spinner. The second spinner is populated and the first town in the spinner "COVENTRY" is shown in a toast message. The problem comes in when I try to select other towns from the 2nd spinner... although the towns are shown in the spinner, they aren't coming up in the toast message.
if (parent.getItemAtPosition(pos).toString().equals("RI")) {
HDHelper hdtable = new HDHelper(getApplicationContext());
hdtable.open();
Cursor c = hdtable.fetchRI();
if (c != null){
SimpleCursorAdapter hdadapter1 = new SimpleCursorAdapter(getApplicationContext(),
android.R.layout.simple_spinner_item, c, // Give the cursor to the list adapter
new String[] {c.getColumnName(2)}, // Map the column in the HD database to...
new int[] {android.R.id.text1}); // The view defined in the XML template
hdadapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner hdstoresspin = (Spinner) findViewById(R.id.hdstorespinner);
hdstoresspin.setAdapter(hdadapter1);
hdstoresspin.setOnItemSelectedListener(new MyOnItemSelectedListener());
String text = c.getString(c.getColumnIndex(hdtable.KEY_STORENUMBER));
Toast.makeText(parent.getContext(), text, Toast.LENGTH_LONG).show();
}
startManagingCursor(c);
hdtable.close();
}
Here is the relevant DBHelper section dealing with the above query:
public Cursor fetchRI() {
String RIquery = "SELECT * FROM HDStores WHERE state = 'RI' ORDER BY storenumber";
return mDb.rawQuery(RIquery, null);
}
UPDATE: I actually think the OnItemSelectedListeneris working properly. It is registering clicks, but no matter which value I click on, only the first value is reflected in the toast message. Again, any help would be greatly appreciated. Thank you.
I figured it would be something stupid that I missed. I went back to working on it again after a few days break and it jumped right out at me. The cursor needed to be finalized as well.

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