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

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.

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 bind values to single spinner from one web service, another default value in android

String[] s={"1","2","3","4"};
ArrayAdapter<String> adp=new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_spinner_dropdown_item,s);
spin.setAdapter(adp);
Now I want to get "4" at zero position of Spinner.
AFAIK, you can't change the position of the items inside a Spinner after you have set its Adapter.
If you want 4 to be the first item, you have to order your list accordingly:
String[] s={"4","1","2","3"};
ArrayAdapter<String> adp = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, s);
spin.setAdapter(adp);

Make list within another app

I did listview tutorial. I can't see how to integrate the list making program into another program I have.
I want to fill an array with sensor input values in one method of my public class, and then display the array as a list after that.
Is it possible to call the list making code as a function, in response to some user activity within the 'sensor' method? How do I do this?
Excuse me if this is stupid, I am a beginner to Java.
Any advice appreciated.
AFAIK, the way you construct a listview is via List<? extends Map<String, ?>>.
if you do want to construct it this way, here's how to do it.
fill the data inside a List<Map<String,?>>, then use a SimpleAdapter (or other adapter) to concatenate the list to ListView. suppose you have a List<Map<String,?>> named mList,
ListView mListView = (ListView)findViewById( --listView id-- );
String[] mFrom = { -key1-, -key2- };
int[] mTo = {android.R.id.text1, android.R.id.text2 };
SimpleAdapter mAdapter = new SimpleAdapter(getApplicationContext(), mList, android.R.layout.simple_list_item_2, mFrom, mTo);
mListView.setAdapter(mAdapter);
listView id is the id of the listview on the xml, key1 and key2 respectively are the data you want to input inside the listview. however this one used android's default feature of list.
if you want to use your own listview template (i.e. you have more than 2 data inside),
define the xml of your custom listview (customlist.xml) then change the mTo variable to match your textviews
int[] mTo = {R.id.-listTextView1-,R.id.-listTextView2-,R.id.-listTextView3-};
and point to that xml on this line
SimpleAdapter mAdapter = new SimpleAdapter(getApplicationContext(), mList, R.layout.--customlistname--, mFrom, mTo);

Change items of a Spinner on item click of another Spinner in android

I have a question that, I want to load items in a spinner on ItemClick of another Spinner. Actually I two spinners, data is loaded into first spinner from json_parsing and I have to load data in second Spinner after selecting an item from first spinner, So, I don't know how it will implemented? Please suggest me the right solution.
Thanks in advance.
You can do it like this,
First time you data will be loaded in First and Second Spinner.
On Selection of item from First Spinner do this.
1.) Clear the previous ArrayList or Array whateven you have passed the
Second Spinner.
2.) Fill the ArrayList or Array of new data & Update the Second Spinnner using
adapter.notifyDataSetChanged();
second_spinner.setSelection(0);
First Set an OnItemClickListner for your First Spinner. In the OnItemClickListner Method first parse your XML. After completing XML parsing, set parsed data to the adapter and set that adapter with your second spinner
Set an OnItemClickListener on your first spinner that would prepare and set the adapter to the second spinner.
Here is a more complete code example:
Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
Cursor c1 = (some code for getting a cursor from an data source, for example, a sqlite database)
SimpleCursorAdapter adapter1 = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, c1, new String[]{"column_name"}, new int[]{android.R.id.text1});
spinner1.setAdapter(adapter1);
Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
Cursor c2 = (some code for getting a cursor from an data source, for example, a sqlite database)
SimpleCursorAdapter adapter2 = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, c2, new String[]{"column_name"}, new int[]{android.R.id.text1});
spinner2.setAdapter(adapter2);
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Cursor c_new = (create a new cursor);
adapter2.changeCursor(c_new);
adapter2.notifyDataSetChanged(); // this is important for notifying the UI
spinner2.setAdapter(adapter2);
}
});
What you do is set a listener to the first Spinner, there change the Cursor of the second Adapter to a new one, notify the UI and reset the Adapter of the second Spinner.

Using a Spinner with a SimpleCursorAdapter

I have an activity which has a Spinner widget to display categories. Initially I was using an ArrayAdapter to populate the the spinner as in the following code
private static final String[] arrayCategories = {
"Business",
"Personal"
};
mCatSpinner = (Spinner) findViewById(R.id.thecategory);
ArrayAdapter<String> catAdapter = new ArrayAdapter<String>(this, R.layout.track_category_item, arrayCategories);
catAdapter.setDropDownViewResource(R.layout.track_category_dropdown_item);
mCatSpinner.setAdapter(catAdapter);
This works fine, and the spinner displays the first array item by default if no selection is made. It does show the selected item when an item is actually selected
But now I want to use a SimpleCursorAdapter to pull the list contents from a db. So I changed it to
SimpleCursorAdapter scaCategories = new SimpleCursorAdapter(this, R.layout.track_category_item,cCategories,new String[] {DBAdapter.KEY_CATEGORIES_NAME},new int[]{R.id.text1});
scaCategories.setDropDownViewResource(R.layout.track_category_dropdown_item);
mCatSpinner = (Spinner) findViewById(R.id.thecategory);
mCatSpinner.setAdapter(scaCategories);
This populates the dropdown, but it does not display the first item in the spinner. Even if selected, it does not show the selected item.
I tried to setSlection to the first item using
if(mCatSpinner.isSelected() != true) {
mCatSpinner.setSelection(0);
}
but it didn't work
What is wrong?
Ok, it would help if I specified the widget id in the layout xml. <:(

Categories

Resources