Android Gallery Text Only - android

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.

Related

setListAdapter multiple items?

Is it possible to have SimpleCursorAdapter retrieve multiple items and have them display? This is from the android Notepad tutorial(edited a bit) and when I try to have it pull AScoresDbAdapter.KEY_COL2_NAME, AScoresDbAdapter.KEY_COL1_DATE it only displays the first one in the TextView
Is there a reason for this or am I missing something?
private void fillData() {
// Get all of the notes from the database and create the item list
Cursor c = scoresDbAdapter.fetchAllNotes();
startManagingCursor(c);
String[] from = new String[] { AScoresDbAdapter.KEY_COL2_NAME, AScoresDbAdapter.KEY_COL1_DATE};
int[] to = new int[] { R.id.text1 };
// Now create an array adapter and set it to display using our row
SimpleCursorAdapter notes =
new SimpleCursorAdapter(this, R.layout.shots_row, c, from, to,0);
setListAdapter(notes);
}
Ok so I guess it slipped pass me at first that they are arrays and that it is probably putting one from with one int so I added another TextView and edited the above to this:
String[] from = new String[] { AScoresDbAdapter.KEY_COL1_DATE,AScoresDbAdapter.KEY_COL2_NAME};
int[] to = new int[] { R.id.text1, R.id.text2 };
And now it displays [Date][Name] instead of just [Date] or [Name]
Not sure if this this the correct way, but if there is a better one I would be glad to know of it.

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

here is my code for spinner with dropdownlist:

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

Categories

Resources