here is my code for spinner with dropdownlist: - android

I have spinner in my application .The spinner have drop down list.I want to take the value of the dropdown list from the database .how can i do this ?
here is my code for spinner with dropdownlist:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, selectdefault);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);

You must create an array to specify which fields you want to display.
You could try this.
DatabaseProfileHelper dbhelper = new DatabaseProfileHelper(context);
// create an array to specify which fields we want to display
String[] from = new String[] { DatabaseProfileHelper.colNama };
int[] to = new int[] {R.id.txtProfileNama};
Cursor c = dbhelper.getAllProfiles();
dbhelper.UpdateProfile(null);
c.moveToFirst();
startManagingCursor(c);
// create simple cursor adapter
SimpleCursorAdapter ca=new SimpleCursorAdapter(context,R.layout.profile_spinner_row, c, from, to);
// get reference to our spinner
spinProfile.setAdapter(ca);

Related

Selected Spinner Item as String for a new Cursor

Right now I am almost done writing my app. All I need is a bit help on getting the selected String value from a spinner populated from my database by a simple cursor adapter. I am not sure how I can get the String from my spinner and pass it to a different cursor and use the string in a query, that will populate depending on the first choice of the spinner, and so on with other spinners.
Here is the code I am using for one of my spinners.
vType = (Cursor) DataBaseHelper.getPowersportsType();
this.startManagingCursor(vType);
SimpleCursorAdapter scaType = new SimpleCursorAdapter(this,
android.R.layout.simple_spinner_item,
vType,
new String [] {DataBaseHelper.POWERSPORTS_TYPE},
new int[] {android.R.id.text1});
scaType.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
vTypeSpinner = (Spinner) findViewById(R.id.typeSpinner);
vTypeSpinner.setAdapter(scaType);
And this is my query for the next spinner in my xml layout
static String MakeWhere = "POWERSPORTS_TYPE=?";
public static Cursor getPowersportsMake(){
return myDataBase.query(POWERSPORTS_TABLE,
new String [] {POWERSPORTS_ID, POWERSPORTS_MAKE},
MakeWhere,
null,
POWERSPORTS_MAKE,
null,
null);
}
Any comments or suggestions are welcomed.
to get the item selected you need to set the onItemSelectedListener
then in your onItemtSelected all you would do is
String selection = vTypeSpinner.getSelectedItem().toString();

Adding default None value to a spinner from DB cursor

I have a spinner which is populated with DB values returned in form of Cursor.but it always have first value as selected. What I need is to have a "None" value instead od ffirst DB value. I know we can do this when we have hardcoded strings but how can i do it with Cursor..??
My code for spinner -
Cursor c = listNamesDbHelper.fetchAllUserLists();
startManagingCursor(c);
Log.i(TAG, "cursor list names--->>" + c.getCount());
// create an array to specify which fields we want to display
String[] from = new String[c.getCount() + 1];
from = new String[]{listNamesDbHelper.KEY_LIST_NAME};
//from[0] = "None";
// create an array of the display item we want to bind our data to
int[] to = new int[]{R.id.ModifyTaskListNameItem};
// create simple cursor adapter
/*SimpleCursorAdapter adapter =
new SimpleCursorAdapter(this, R.layout.choose_list_modify_task, c, from, to );
adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );*/
// get reference to our spinner
CustomSpinnerItemsAdapter adapter = new CustomSpinnerItemsAdapter(this,c,from,to);
Spinner s = (Spinner) findViewById( R.id.spinnerListSelection );
s.setAdapter(adapter);
Thanks in advance,
Ray

How to use an array rather than a cursor for a spinner

I am currently using a simpleCursorAdaptor and a cursor to load a spinner with data, however I would prefer to convert the cursor to a simple array and use this instead (as the list is short and static).
What's the simplest way of doing this?
My code currently is:
private void loadEmployeeList(){
LoginDataHandler dataHandler = new LoginDataHandler(getContentResolver());
Cursor data = dataHandler.activeEmployeeList();
if (null!=data){
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_spinner_item,
data,
new String[]{MyobiliseData.Columns_employees.NAME},
new int[] { android.R.id.text1 }
);
// Attach the data to the spinner using an adaptor
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
}
}
ArrayList<String> mArrayList = new ArrayList<String>();
for(data.moveToFirst(); !data.isAfterLast(); data.moveToNext()) {
mArrayList.add(data.getString(data.getColumnIndex(MyobiliseData.Columns_employees.NAME)));
}
now you can proceed as you have an array mArrayList of cursor's data,
If it is short and static, you might consider putting it in your strings.xml as an array and accessing it that way rather than incurring somewhat greater overhead of reading from a DB and translating to an array.
data.xml
<resources>
<string-array name="States">
<item>AL</item>
<item>AK</item>
<item>AR</item>
</string-array>
</resources>
Then to use it for your spinner:
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
getActivity(), R.array.States, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(R.layout.listlayout_black);
final Spinner states = (Spinner) v.findViewById(R.id.mbr_state_spinner);
states.setAdapter(adapter);
Then you have to write a logic for iterating over cursor and fetch data. Create array with these data you got from cursor.
Here you go
ArrayList<String> list = new ArrayList<String>();
Cursor data = dataHandler.activeEmployeeList();
if (data.moveToFirst())
{
do {
list.add(data.getString(data.getColumnIndex(MyobiliseData.Columns_employees.NAME)));
} while (data.moveToNext());
}

Populate spinner with different cursors

I have a spinner that I populate with a cursor from database sqlite column, this works OK, but isn't ideal. I added another spinner to select a column and show it in spinner, but this is my problem when I build the adapter. This is my code:
cursor = myDB.obtenerColumna(getBaseContext(),elemento);
String[] columns = new String[] { "_id", "columna", "columnb", "columnc", "columnd" };
menudesplegable.setPrompt(getText(R.string.seleccionaEtiqueta));
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_spinner_item,cursor,
columns, new int[] {android.R.id.text1});
adapter.setDropDownViewResource
(android.R.layout.simple_spinner_dropdown_item);
I get columns from database correctly, but if I pass a cursor from other column the app fail building adapter. I try pass only name column in String[] but fail too.
Thanks.
I resolved this:
/**
* Crea el menĂº desplegable para seleccionar tiqueta.
*/
public void construirMenuDesplegable(String elemento) {
// get items of database using selected element in other spinner
cursor = myDB.obtenerColumna(getBaseContext(), elemento);
menudesplegable.setPrompt(getText(R.string.seleccionaEtiqueta));
// Create adapter
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_spinner_item, cursor,
new String[] { elemento }, new int[] { android.R.id.text1 });
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Indicate adapter
menudesplegable.setAdapter(adapter);
try {
myDB.close();
} catch (Exception e) {
Log.e("miError", "Error al cerrar db", e);
}
}
/**
* Build spinner for select a element for show in other spinner.
*/
public void construirMenuDesplegable2() {
// Create adapter
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(
this, R.array.listables, R.layout.custom_spinner);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Indicate adapter
menudesplegable2.setAdapter(adapter2);
}
No need to do the whole thing.
once the first spinner is populated just close the cursor.
mycursor.close();
then repeat same logic to the second spinner.

Android Gallery Text Only

I've looked into the API Demos and they have an example of the gallery where they show just text, the code just uses a Cursor , however I want to use a String array. How can I achieve using it? This is the code for the example in API Demos:
Cursor c = getContentResolver().query(People.CONTENT_URI, null, null, null, null);
startManagingCursor(c);
SpinnerAdapter adapter = new SimpleCursorAdapter(this,
// Use a template that displays a text view
android.R.layout.simple_gallery_item,
// Give the cursor to the list adatper
c,
// Map the NAME column in the people database to...
new String[] {People.NAME},
// The "text1" view defined in the XML template
new int[] { android.R.id.text1 });
If you just want to display a static list of String objects, an ArrayAdapter should do:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_gallery_item, new String[] {People.NAME});
However, if you want to be able to add more String objects to the list later, you should use a List.
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_gallery_item, new ArrayList<String>());
You need to replace the CursorAdapter with an ArrayAdapter.

Categories

Resources