Is it possible to resize/reduce size of photos on the fly inside SimpleCursorAdapter?
Scenario is that im getting the path of the photos that are saved on sdcard via a cursor, and showing them in a listview via SimpleCursorAdapter.
here is part of my code in my Activity's onCreate():
Cursor c = mydb.rawQuery("select * from events;", null);
if (c != null ) {
c.moveToFirst();
}
String[] from = new String[]{DbHandler.column_id, DbHandler.column_name, DbHandler.column_location, DbHandler.column_date, DbHandler.img_loc};
int[] to = new int[] {R.id.textView1, R.id.textView2, R.id.textView3, R.id.textView4, R.id.photoInDb};
SimpleCursorAdapter simpleCurs = new SimpleCursorAdapter(this, R.layout.listviewfinal, c, from, to);
listView.setAdapter(simpleCurs);
If its possible, please tell me how? also do i need to define setViewValue?
Related
I get some data from the database and try to fill them in a listview.
i have checked through cursor.count() there is data exist but the listview show anything :/
I use a simpleCursorAdapter:
rubDb.open();
cur = rubDb.getRubParent();
startManagingCursor(cur);
Log.d("cursor length",Integer.toString(cur.getCount()));
String[] from = new String[] { RubriqueDbAdapter.RUB_NOM, RubriqueDbAdapter.RUB_VISUEL };
int[] to = new int[] {R.id.title, R.id.icon};
listAdapter = new SimpleCursorAdapter(this, R.layout.itemgauche, cur, from, to);
listeGauche.setAdapter(listAdapter);
public Cursor getRubParent() {
Cursor cursorResults = mDb.rawQuery("SELECT * FROM " + TABLE_RUBRIQUE + " WHERE rub_id_parent = 0 ORDER BY rub_ordre ASC", null);
return cursorResults;
}
how can I get a picture from the sdcard and put it on the ImageView in the listview ?
Thanks for your help !
First, you should be using a CursorAdapter, perhaps a SimpleCursorAdapter, instead of manually copying all of the data into objects in an array, just to use ArrayAdapter.
Second, if your list will be anything other than a simple piece of text, you must teach the adapter how to do whatever else you want. With SimpleCursorAdapter, you could associate a ViewBinder with it, or subclass SimpleCursorAdapter and override setViewImage(), or subclass SimpleCursorAdapter and override bindView().
I have made the following code to retrive data from SQLite database.
public Cursor fetchAllScores() {
return database.query(DATABASE_TABLE, new String[] { KEY_ROWID,
KEY_PLAYDATE, KEY_NUMVALA, KEY_NUMVALB }, null, null, null,
null, null);
}
Then I call this function in my main.java file using the following
cursor = dbHelper.fetchAllScores();
startManagingCursor(cursor);
After having cursor I manage to populate myGridView with some data using following code
GridView myGV = (GridView)findViewById(R.id.gridView1);
String[] cols = new String[] { scoreDbAdapter.KEY_PLAYDATE, scoreDbAdapter.KEY_NUMVALA, scoreDbAdapter.KEY_NUMVALB};
int[] views = new int[] { android.R.id.text1, android.R.id.text2, android.R.id.text2 };
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1, cursor, cols, views);
Log.w("NumRows",adapter.getCount() + "");
myGV.setAdapter(adapter);
Now the problem is only first row is populated and first two columns, I want data like in forst row (2011-10-27 , 5 , 6 ) and second row like (2011-10-26 , 3 , 2 ) but i m getting is only forst row like (2011-10-27, 2011-10-26 ).
Can this be fixed in GridView?
For your question, take a look here, Binding to Data with AdapterView.
Note: Below decription is just example for how to bind cursor to an adapter.
Filling the Layout with Data
Inserting data into the layout is typically done by binding the AdapterView class to an Adapter, which retrieves data from an external source (perhaps a list that the code supplies or query results from the device's database).
The following code sample does the following:
1.Creates a Spinner with an existing View and binds it to a new ArrayAdapter that reads an array of colors from the local resources.
2.Creates another Spinner object from a View and binds it to a new SimpleCursorAdapter that will read people's names from the device contacts (see Contacts.People).
// Get a Spinner and bind it to an ArrayAdapter that
// references a String array.
Spinner s1 = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter adapter = ArrayAdapter.createFromResource( this, R.array.colors,
android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s1.setAdapter(adapter);
// Load a Spinner and bind it to a data query.
private static String[] PROJECTION = new String[] { People._ID, People.NAME};
Spinner s2 = (Spinner) findViewById(R.id.spinner2);
Cursor cur = managedQuery(People.CONTENT_URI, PROJECTION, null, null);
SimpleCursorAdapter adapter2 = new SimpleCursorAdapter(this,android.R.layout.simple_spinner_item,cur,new String[] {People.NAME}, new int[] {android.R.id.text1});
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s2.setAdapter(adapter2);
Note that it is necessary to have the People._ID column in projection used with CursorAdapter or else you will get an exception.
If, during the course of your application's life, you change the underlying data that is read by your Adapter, you should call notifyDataSetChanged(). This will notify the attached View that the data has been changed and it should refresh itself.
For more info look at this,
Custom CursorAdapters.
Thanks.
Make a new layout which contains textViews. Find the layout as shown in first line of the code below and attach your data to the listView. ListView loads the layout containing textView.
private void populateGrid()
{
ListView lv = (ListView)findViewById(R.id.listView1);
Cursor c = dbHelper.fetchAllScores();
startManagingCursor(cursor);
String[] cols = new String[] { TodoDbAdapter.KEY_PLAYDATE, TodoDbAdapter.KEY_NUMVALA, TodoDbAdapter.KEY_NUMVALB};
int[] views = new int[] { R.id.txt_date, R.id.txt_no, R.id.txt_yes};
// Now create an array adapter and set it to display using our row
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.listviewtemp, c, cols, views);
Log.w("NumRows",adapter.getCount() + "");
lv.setAdapter(adapter);
}
R.layout.listviewtemp is the listView template layout (separate xml), lv is the listview found in the mainlayout.xml.
public Cursor fetchAllScores() {
return database.query(DATABASE_TABLE, new String[] { KEY_ROWID,
KEY_PLAYDATE, KEY_NUMVALA, KEY_NUMVALB }, null, null, null,
null, null);
}
I have images in my DB and I put it to my GridView by this code:
public void setNotes()
{
String[] columns = {NotesDbAdapt.KEY_ID, NotesDbAdapt.KEY_IMG, NotesDbAdapt.KEY_NAME, NotesDbAdapt.KEY_DATE, NotesDbAdapt.KEY_TIME};
String table = NotesDbAdapt.NOTES_TABLE;
Cursor c = MainNote.mDB.getHandle().query(table, columns, null, null, null, null, null);
startManagingCursor(c);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.file_dialog_row,
c,
new String[] {NotesDbAdapt.KEY_IMG, NotesDbAdapt.KEY_NAME, NotesDbAdapt.KEY_DATE, NotesDbAdapt.KEY_TIME},
new int[] {R.id.img, R.id.txt, R.id.date, R.id.time});
adapter.setViewBinder(new NotesBinder());
gridview.setAdapter(adapter);
}
All ok, but but the scrolling is slow, jerky. Seems that information takes from DB every time. How to fix it?
This method was deprecated in API level 11. Use the new CursorLoader class with LoaderManager instead. It can perform the cursor query on a background thread so that it does not block the application's UI.
given an example SimpleCursorAdapter implementation code:
String[] projection = new String[] {Browser.BookmarkColumns._ID,
Browser.BookmarkColumns.TITLE,
Browser.BookmarkColumns.URL};
String[] displayFields = new String[] {Browser.BookmarkColumns.TITLE,
Browser.BookmarkColumns.URL};
int[] displayViews = new int[] { android.R.id.text1,
android.R.id.text2 };
Cursor cur = managedQuery(android.provider.Browser.BOOKMARKS_URI,
projection, null, null, null);
setListAdapter(new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_2, cur,
displayFields, displayViews));
How can I use my own xml layout to show those list using LayoutInflater?
or maybe I could say "to make my own simple_list_item_2.xml"....
Thanks
SimpleCursorAdapter handles the layout inflation for you. You just need to create your layout in res/layout/my_list_item.xml and then change android.R.layout.simple_list_item_2 to R.layout.my_list_item.
You would also change android.R.id.text1 to the id of the TextView you want to bind the TITLE column to in your layout, for example R.id.title. Likewise for the URL column.
You can start with a copy of simple_list_item_2.xml and experiment with editing it for your needs, saving it to res/layout/my_list_item.xml or whatever layout name you want.
I want to create a contact list application in my android phone. I want to retrieve all the contact list from emulator and the contacts should be listed.
Even API Demo samples has a similar program
Cursor c = getContentResolver().query(Phones.CONTENT_URI, null, null, null, null);
startManagingCursor(c);
// Map Cursor columns to views defined in simple_list_item_2.xml
ListAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_2, c,
new String[] { Phones.NAME, Phones.NUMBER },
new int[] { android.R.id.text1, android.R.id.text2 });
setListAdapter(adapter);