I have a spinner currently being filled with one column (name) from a database. I would like to change this to have each spinner row displaying the name, a space and an Id from the same table. This is the code I am using at the moment, I am not sure how to map multiple rows in the "from" array into the same textview in the "to" array.
Cursor c = db.fetchAllUsers();
startManagingCursor(c);
// create an array to specify which fields we want to display
String[] from = new String[]{"name"};
// 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.setAdapter(adapter);
Thanks!
Use SimpleCursorAdapter.setViewBinder with a custom ViewBinder that does what you wish. To elaborate, you can simply ignore columnIndex in your implementation of setViewValue and use the given Cursor to populate the given View however you wish.
See: http://developer.android.com/reference/android/widget/SimpleCursorAdapter.html#setViewBinder(android.widget.SimpleCursorAdapter.ViewBinder)
Related
I have SQLite Database. I've set it up using android and listview such that I can view the data in the database. Most of the columns are text. For these, I have set up several textView objects that permit me to upload the text in the SQL columns into the list view.
But I have a column of binary data. And I'd like to load this such that if there is a "0" in the column, then the switch is in the off position, and if there is a "1" in the column, the switch is in the on position?
Do I need to set up some kind of case system on the switch object?
Here's my current code for populating the ListView
// THIS UPDATES THE LIST
private void populateListViewFromDNewBMetric(){
Cursor cursor = mydbmetric.getAllRows();
// Setup mapping from cursor to view fields
String[] fromFieldNames = new String[]
{DBAdapter_Metrics.KEY_ROWID,DBAdapter_Metrics.KEY_NAME,DBAdapter_Metrics.KEY_DESCRIPTION,DBAdapter_Metrics.KEY_SWITCH};
int[] toViewIDs = new int[]
{R.id.textViewMetricID,R.id.textViewName,R.id.textViewDescription,R.id.switch1};
SimpleCursorAdapter myCursorAdapter;
myCursorAdapter = new SimpleCursorAdapter(getBaseContext(),R.layout.item_metric,cursor,fromFieldNames,toViewIDs,0);
ListView mylist = (ListView) findViewById(R.id.listViewMetric);
mylist.setAdapter(myCursorAdapter);
}
}
DBAdapter_Metrics.KEY_SWITCH and R.id.switch1 are the things I don't think makes sense. It works for the other ones like DBAdapter_Metrics.KEY_DESCRIPTION because these are text. But KEY_SWITCH is an integer while switch1 is a switch not a field, so I'm not sure if I can somehow "load" a switch.
I'm displaying data from SQLite using
ListAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_2,
employees,
new String[] {"FirstName","LastName"},
new int[] {android.R.id.text1,android.R.id.text2},0);
getListView().setAdapter(adapter);
empoyees - my Cursor.
Data is showing correctly, but how to get what row I used from SQLite duaring OnClick on my item from List?
For example I have 2 tables
1)Category
_id Integer
Name Text
2)Articles
_id Integer
Name text
CategoryId (foreing key)
So on first screen I'm displaying all Categorys, then on list item click I want to display Articles that are from specified category, how to do that?
Don't use your cursor in your adapter. Build up an array and send in the array instead. In that array, typically something like List<MyDataRecordHolder> (where MyDataRecordHolder is an object you've created and filled with data from the database), you will have to set a reference to a row id.
UPDATE:
Create your MyDataRecordHolder: (Pseudo code)
List<MyDataRecordHolder> list = new List<MyDataRecordHolder>();
in your loop where you fetch data:
MyDataRecordHolder record = new MyDataRecordHolder();
record.setId(cursor.get("rowid"))
//set any other pertinent data
list.add(record);
For your adapter:
ListAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_2,
list,
new String[] {"FirstName","LastName"},
new int[] {android.R.id.text1,android.R.id.text2},0);
Note, you are now supplying the List (list) to the adapter.
In your OnClick for your row, in the adapter, you will get the current MyDataRecordHolder:
final MyRecordHolder record = getItem(position);
String id = record.getId();
//make new db query based on your id
I am trying to get the database values to the spinner, this works fine. Also i am saving the spinner selected value to the another database.
This is the code for getting data to spinner from database.
private void filldata()
{
Cursor docdetailsCursor;
Spinner docdetailsSpinner = (Spinner) findViewById(R.id.doctornamespinner);
docdetailsCursor = mDbHelper1.fetchAlldetails();
startManagingCursor( docdetailsCursor);
/*Create an array to specify the fields we want to display in the list (only the 'colourName' column in this case) */
String[] from = new String[]{DoctorDb.KEY_NAME};
/* and an array of the fields we want to bind those fields to (in this case just the textView 'tvDBViewRow' from our new db_view_row.xml layout above) */
int[] to = new int[]{R.id.text1};
SimpleCursorAdapter docdetailsAdapter =
new SimpleCursorAdapter(this, R.layout.spinner_row, docdetailsCursor, from, to);
/* and assign it to our Spinner widget */
docdetailsSpinner.setAdapter(docdetailsAdapter);
}
here i am trying to get the spinner value and printing.
String dosage_type = mPill_pattern.getSelectedItem().toString();
System.out.println("spinner:"+ mPres_doctor);
when i print the spinner selected value i am getting
10-07 09:52:20.084: INFO/System.out(5500): spinner:android.widget.Spinner#43f04420
can anybody what is the solution for this.
After the successful connection to the server(my local host),i want to retrieve the data that server sends to me. Can anyone please suggest me what should i do for this?
After receiving that data, i have to store it into an Array and then have to fetch it one by one into the dropdown list.
Thanks
Sheetal i have not worked with any application with server,but i retrieved data locally and then placed it in an array and showed in the spinner, this might help u
Cursor c = db.fetchAllReminders();
startManagingCursor(c);
// Create an array to specify the fields we want (only the TITLE)
String[] from = new String[]{DataManager.NAME};
// and an array of the fields we want to bind in the view
int[] to=new int[] { android.R.id.text1 };
// to[0]=android.R.id.text1;
final Spinner spinner = (Spinner) this.findViewById(R.id.spins);
// Now create a simple cursor adapter and set it to display
final SimpleCursorAdapter reminders =new SimpleCursorAdapter(this,
android.R.layout.simple_spinner_item, c, from, to);
spinner.setAdapter(reminders);
I recently started programming for android and java development in general. Currently I train by writing a timetable app.
I want to get a list with all subjects from the db (the table also contains teacher and rooms) and put them into a spinner.
Thats the code I wrote
Cursor c = dba.fetchAllSubjects();
if (c.getCount() != 0) {
Spinner subjectSpinner = (Spinner) findViewById(R.id.newlesson_subject);
startManagingCursor(c);
String[] from = new String[]{DbAdapter.KEY_SUBJECT};
int[] to = new int[] {android.R.layout.simple_spinner_item};
SimpleCursorAdapter subjectAdapter =
new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, c, from, to);
subjectSpinner.setAdapter(subjectAdapter);
}
The problem is that when clicking on the spinner it shows a list with as much items as the db has entries, but the list doesn't show the names (you can say it's empty). So I probably messed something up with the simple cursor adapter, but I don't know what.
Thanks for your help
You're error lies in specifying the wrong Resource ID in your to integer array.
int[] to = new int[] {android.R.layout.simple_spinner_item};
You need to specify the ID of a TextView (or similarly typed view) and not the layout itself. Replace the line above with:
int[] to = new int[] { android.R.id.text1 };