Populating a second spinner based on what was selected in the first - android

Hi I'm working on an Android application. I have a form with 2 spinners (CarMake & CarModel) that allows a user to select a specific car from my mySQL database. I want to load the CarMake spinner with all the entries in the make column of my car database, and from there I want the carModel spinner to only contain models of that make in the database. What would be the easiest way to implement this?

Assign unique ID's to your CarMake table items, like a primary key. So suppose for Tata you assign ID= 1 Then assign the same ID to all of its model in the CarModel table like a foreign key and then using this ID you can fetch all the models easily. (Hoping you have a different tables for them)

The approach you need is to have a populate method per spinner, so you would have something like this
public void populateCarMake()
{...
// query car make
// update adapter 1
}
public void populateCarModel( int makeID )
{...
// query car model where make_id == makeID
// update adapter 2
}
and in onitemselect() of the first spinner call populateCarModel( makeID );

based on the selection of the spinner item just set the adapter using your new list
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(dataAdapter);

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

How to mirror ListView to Database?

I have a database with a table that stores int and string. The integer value is the primary key. I have created a function to just fetch the strings from the database and store them in a list which is then applied to the ListView using an ArrayAdapter as shown below.
List<String> list = db.getAllStringNotes();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_activated_1, list);
listview.setAdapter(adapter);
listview.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
The trouble is deleting from this listview. Since the primary key is not present along with the string, I was deleting using the position of the item selected from the list view. But that obviously messes up things if I'm removing an entry from the middle of the list.
So was wondering if I could add the primary key i.e. an integer value to this list view along with the String but not display the integer value?
The simple thing is create two List,
1. String - Stored String notes
2. Integer - Stored all Primary Keys
So whenever user click on Listview user get its position, and based on that position get primary key value from second list and then perform your delete query.
There are many ways to do this: but as you are already using an ArrayList so i would suggest just make another arraylist while fetching from database:
So while deleting using the position :
Use the Primary Key from the PrimaryKeyArrayList
and delete values from both the ArrayList;
With this you will get exactly what you need;
Follow these Steps:
Create bean.java file that will have your db value
Create CustomAdapter.java to pupulate the items and handle the delete operation
Create delete method in your adapter and pass the selected bean object to delete from DB and ArrayList.
after deletion call notifyDatasetChanged method to reflect the change in list
EDIT:
Bean File:
public class MyDB_Bean{
public int id;
public String data;
MyDB_Bean(int id,String data){
this.id=id;
this.data=data;
}
}
Calling from Activity
ArrayList<MyDB_Bean> list = db.getAllStringNotes();
MyArrayAdapter adapter = new MyArrayAdapter<String>(this,list);
listview.setAdapter(adapter);
listview.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
For CustomAdapter follow these tutorials
http://www.ezzylearning.com/tutorial.aspx?tid=1763429
Custom Adapter for List View
http://www.vogella.com/tutorials/AndroidListView/article.html
http://learnandroideasily.blogspot.in/2013/06/listview-with-custom-adapter.html

Get and Set values form spinner

I'm looking for to get and set values form spinner loades from database but i don't find it.
private void loadSpinnerData() {
// database handler
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
// Spinner Drop down elements
List<String> lables = db.getAllCadenesAsList();
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, lables);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spinner.setAdapter(dataAdapter);
}
The cursor returns _id and Name and the spinner load correctly (it seems..)
Now when I try to get de selected valued in onItemSelected, I the id and the position is the same, and is not a id of cursor, it is the posicion in de spinner.
How can I get de ID of selected value?
How can I set the value of spinner.
I need and I know the id of database, not the id/position of spinner.
Thanks in advance.
You're not getting a real database _id on onItemSelected of the spinner because you hooked the spinner up to an ArrayAdapter. This sort of adapter hookup is not in any way connected to your database but only the List<String> or other collection you passed into it. To get what you want, you need to set the spinner's adapter to a CursorAdapter instead.

Populating an Android Spinner from an SQLite but with two fields?

I've finally managed to populate a spinner from an sqlite db after much messing about with this code however it's only using one field and I want First and Last names on the same spinner item?
The code is as follows:
private void fillSpinner() {
Cursor c = myDbHelper.FetchDrivers();
startManagingCursor(c);
// create an array to specify which fields we want to display
String[] from = new String[]{"FirstName"};
// create an array of the display item we want to bind our data to
int[] to = new int[]{android.R.id.text1};
// create simple cursor adapter
SimpleCursorAdapter adapter =
new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, c, from, to );
adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
// get reference to our spinner
Spinner s = (Spinner) findViewById( R.id.spr_Driver1);
s.setAdapter(adapter);
}
Now it displays the FirstName and I've tried added "FirstName", "LastName" but it doesn't do anything different, I ideally want it to display the name in full on each spinner item. Is this even possible?
Any help would be great!
Thanks,
Chris
For this, you need to change the android.R.layout.simple_spinner_item and android.R.layout.simple_spinner_dropdown_item, as these layout items are able to display only one item in the dropdown list and on the spinner.
For your purpose, the best way is to create your own layout that has two items.
Here is a link about how to do it.

Refresh spinner data

I have an application with a Spinner whos data is populated by a cursor from the database. Works great if there is something in the database table on startup. However, if I add something to the database after the app is running my spinner always shows that it has zero choices, even if one was there to begin with.
My code is as follows. The adapter.getCursor().requery(); did no good. I would like the Spinner to update its choices when the user clicks on it and I found a couple posts on StackOverflow that say you have to use the TextView behind the Spinner for the OnClickListener. However, for me that did nothing. Mostly likely because I'm missing one minor
c1 = myDbHelper.readCars();
startManagingCursor(c1);
// create an array to specify which fields we want to display
String[] from = new String[]{"nickname"};
// create an array of the display item we want to bind our data to
int[] to = new int[]{android.R.id.text1};
adapter = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, c1, from, to);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinVehicle.setAdapter(adapter);
txtVehiclePrompt.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
adapter.getCursor().requery();
}
});
Just change the underlying data and call notifyDataSetChanged()
list.clear();
//list modify
list.add("A");
list.add("B");
dataAdapter.notifyDataSetChanged();
Ok,
I got it figured out. I needed to close the adapter, stop managing the cursor, close the cursor & close the db helper in the onSuspend() method then set everything back up in the onResume method since I was using a seperate activity to add records to the table.

Categories

Resources