SimpleCursorAdapter - Binding to multiple item values in ListView - android

I have the following code to intantiate a SimpleCursorAdapter to use with a ListView. As you can see I have passed the R.layout.log_item to display the list items, and one value/control to bind to (TripDate).
SimpleCursorAdapter sca = new SimpleCursorAdapter(this, R.layout.log_item,c,new String[] {DBAdapter.KEY_LOG_TRIPDATE},new int[]{R.id.txtTripDate});
this works
I currently only have one widget in the layout xml, a TextView to hold the TripDate.
How do I pass multiple binding parameters for the additional widgets in the layout? so I can also display other info.
thanks
patrick

SimpleCursorAdapter sca = new SimpleCursorAdapter(this, R.layout.log_item,c,new String[] {DBAdapter.KEY_LOG_TRIPDATE, DBAdapter.KEY_FOO, DBAdapter.KEY_BAR},new int[]{R.id.txtTripDate, R.id.otherid1, R.id.otherid2});

Related

Adding items to the top of a dynamic listview in Android

I am new to android and not a killer in programming. I am trying to implement a dynamic listview in which items must be added to the top, rather than getting added to the bottom by default. Is there a way to achieve this? Below is the code I used to create a dynamic listview. Please help!
private void populateListView() {
Cursor cursor = myDb.getAllRows();
startManagingCursor(cursor);
String[] fromFieldNames = new String[]
{DBadapter.KEY_FIELD1,DBadapter.KEY_FIELD2};
int[] toViewIDs = new int[]
{R.id.field1,R.id.field2};
SimpleCursorAdapter myCursorAdapter =
new SimpleCursorAdapter(
this,
R.layout.inner_list_view,
cursor,
fromFieldNames,
toViewIDs
);
ListView myList = (ListView) findViewById(R.id.sampleList);
myList.setAdapter(myCursorAdapter);
myCursorAdapter.notifyDataSetChanged();
}
ListViews support stacking the items from the bottom. You can either set it in the XML with android:stackFromBottom or you can set it programmatically by invoking setStackFromBottom(boolean).
I don't understand the 2nd part of your question.
you can use the "insert" method. It allows you to define position to add new items
myCursorAdapter.insert(newItem, 0);
Use a loop if you have multiple elements

Multiple rows in a single TextView

I have an application that has a Database of vehicles with multiple fields.
I then have a ListView in which I want to display some of the fields.
Currently I have this:
SimpleCursorAdapter vehicleAdapter = new SimpleCursorAdapter(this, R.layout.vehiclerow, vehicleCursor, new String[] {"registration"}, new int[] { R.id.vehicleRow});
And I want to put multiple values on the vehicleRow TextView for instance:
Toyota - Corolla
in which Toyota and Corolla come from separate fields of the database.
I've tried this:
SimpleCursorAdapter vehicleAdapter = new SimpleCursorAdapter(this, R.layout.vehiclerow, vehicleCursor, new String[] {"make", "model"}, new int[] { R.id.vehicleRow});
but it doesn't work.
What could be the problem?
Regards
You can only map one data field to one view with the standard adapters.
You either need to create a custom adapter to let you put two data fields.into one widget (not always trivial) or create a new rowlayout with the necessary textviews and use that with a standard adapter.
I'd go for the second option.
Create another TextView and entered its Id in the cursorAdapter.
try in this way :
String[] from = new String[]{DbManager.Title, DbManager.Date};
int[] to = new int[] { R.id.list_name_main,R.id.list_date_main};{
Adapter = new SimpleCursorAdapter(this, R.layout.row_item_by_list, c1, from, to);
//Adapter.setViewBinder(new ScoreHistoryBinder());
list_item.setAdapter(Adapter);
list_item.setOnItemClickListener(listOnItemClickListener);}

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

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.

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

Android ListView update with SimpleCursorAdapter

Hey i use a listview for demonstrate entries which are stored in a database. I also have a EditText element and a button which adds the content of the EditText into the Database. To bind the view to the database content i use the SimpleCursorAdapter and following populate function:
private void populate() {
cursor = dbAdapter.getAllItems();
startManagingCursor(cursor);
String[] from = new String[] { DBAdapter.KEY_TASK };
int[] to = new int[] { android.R.id.text1 };
// Now create an array adapter and set it to display using our row
cursorAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cursor, from, to);
list.setAdapter(cursorAdapter);
}
If i added a new entry, by clicking on the button i want to refresh the listview but this only works with the populate function and not with the common adapter function notifyDataSetChanged();. Do i have a bug or is this the right way to refresh a listview?
Have you seen this, tried the swap cursor method, or tried just simply calling setAdapter() again?
I had a similar issue where I could not get my list to update, and what I did was just create a refreshListView() method. Now you can call this initially from your onCreate(), AND anytime a user adds something to the DB. All it does is re-bind the listview to a cursor. With all the deprecating methods (requery()), and issues with notifyDataSetChanged(), I decided this was the easiest way.
Please refer this link...it works like charm
Update SimpleCursorAdapter while maintaining scroll position in ListView
for dynamic listview on scroll i added new item from database ..
I did mistake here ..
i was assigning new adapter for each time for same simplecursoradapter .
Instead of creating new adapter.
just use
adapter.changecursor(newcursorValue);
adapter.notifydatasetChanged();
lsMedicine1.setSelectionFromTop(lsMedicine1.getLastVisiblePosition()-20, 0);
You need to call swapcursor() before notifyDataSetChanged() on the adapter.

Categories

Resources