First a little background about why I’m asking this question…
I’m working on a scheduling app for a machine shop. I have a database with tables for the machines, jobs and employees. It’s possible that not all of these machines will have jobs or operators, so I needed a way to add “None” to the spinners (there are several reasons I won’t go into for not adding that to the database itself). To accomplish this I settled on getting a cursor from the database and iterating over it, loading those bits I needed into an ArrayList and adding a “None” element at the end.
This works just fine, but now I’ve come upon a sticking point I can’t seem to figure out.
The Job table has two columns (not counting the index): a job number and a part number. I created a custom layout to put them together and then use that in the spinner (so you would see Job # – Part # on each line of the spinner). Populating that from a cursor was simple. Trying to convert that to two arrays has me stumped.
I can get the data from the cursor into the two arrays with no problem, but I can’t figure out how to relate them to the layout ids contained in the layout.
I’m new to android and long time ago schooled in Java, so if I’ve missed something obvious, please point it out!
listlayoutdouble.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:id="@+id/ListItem1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:paddingLeft="12dip"
android:gravity="left|center_vertical"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/ListItem2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:paddingRight="12dip"
android:gravity="right|center_vertical"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
What I have so far:
private void fillJobSpinner() {
Cursor jobsCursor = mDBHelper.fetchAllJobs();
startManagingCursor(jobsCursor);
ArrayList<String> jobnumbers = new ArrayList<String>();
ArrayList<String> jobnparts = new ArrayList<String>();
if (jobsCursor != null) {
operatorsCursor.moveToFirst();
while (operatorsCursor.isAfterLast() == false) {
jobnumbers.add(jobsCursor.getString(jobsCursor
.getColumnIndex(ScheduleDBAdapter.JOB_NUMBER)));
jobnparts.add(jobsCursor.getString(jobsCursor
.getColumnIndex(ScheduleDBAdapter.JOB_PART)));
jobsCursor.moveToNext();
}
jobnumbers.add("None");
jobparts.add("None");
}
}
I think what you need is a custom adapter for your spinner.
You need to extend BaseAdapter
http://developer.android.com/reference/android/widget/BaseAdapter.html
Then override the getView method.
A quick google turned up this tutorial:
http://app-solut.com/blog/2011/03/using-custom-layouts-for-spinner-or-listview-entries-in-android/