I'm trying to load the contents of my SQLite table into a ListView using the following code.
myDbHelper = new DatabaseHelper(this);
Cursor cursor = getData();
CursorAdapter dataSource = new SimpleCursorAdapter(this, R.layout.menu_item, cursor, fields, null);
menuList.setAdapter(datasource); //My ListView
private Cursor getData() {
SQLiteDatabase db = myDbHelper.getReadableDatabase();
Cursor cursor = db.query(TABLE_NAME, FROM, null, null, null,null, ORDER_BY);
return cursor;
}
When I run the app it passes through this code and then throws an error. Am I using the correct technique for adding data to a ListView?
You need to give a little more information.
Where does it crash?
What is the error that it gives?
You are using adapt but you do not declare it or set it to anything in your code snippet - if it's null, then that will be a problem.
menuList.setAdapter(adapt); //My ListView
Take a look at my example code here for how I use ListViews with SQLite.
Related
SQLiteDatabase db = mDbHelper.getReadableDatabase();
String[] projection = {
HistoryEntry._ID,
HistoryEntry.COLUMN_HISTORY_INPUT,
HistoryEntry.COLUMN_HISTORY_RESULT
};
Cursor cursor = db.query(HistoryEntry.TABLE_NAME,
projection,
null,
null,
null,
null,
null);
ListView historyListView = (ListView) findViewById(R.id.list);
mCursorAdaptor = new HistoryCursorAdaptor(this, cursor);
historyListView.setAdapter(mCursorAdaptor);
//the code is using to read data from database but error in query. says ," free up cursor after use."
you need to call cursor.close() to release the cursor and its resources but as per your code, once we close the cursor then your adapter won't be able to access it, so the idle place will be to close the cursor inside onDestroy method of activity life cycle and for that, you can declare cursor as global variable (outside current method)
Assuming HistoryCursorAdaptor is a CursorAdapter, it takes ownership of the passed in Cursor and you should not be closing it yourself.
If you want to explicitly close the cursor owned by a cursor adapter, you can call changeCursor(null) on the adapter.
I am trying to create a database and add the items of list view to the database. I have succeeded in creating the database and adding the data to database.My problem is in presenting the data as cursor using simple cursor adapter. When I tried to use toast,it works.My problem is with cursor I think....
Here's my method in DBAdapter
public Cursor getAllRecords(){
String where=null;
Cursor c=db.query(true,DATABASE_TABLE,new String[]{KEY_TITLE,KEY_ALBUM},where,null,null,null,null,null);
if(c!=null){
c.moveToFirst();
}
return c; }
This is my method to populate the list...
private void populatelist(){
db.open();
c=db.getAllRecords();
if(c!=null){
String[] fields=new String[]{DBAdapter.KEY_TITLE,DBAdapter.KEY_ALBUM};
int[] intof=new int[]{R.id.data_item_layoutTextView,R.id.dataitemlayoutTextView1};
SimpleCursorAdapter mAdap=new SimpleCursorAdapter(getBaseContext(),R.layout.data_layout,c,fields,intof,0);
ListView lv=(ListView)findViewById(R.id.data_layoutListView);
lv.setAdapter(mAdap);
}else{
Toast.makeText(getApplicationContext(),"Nothing!!!",1000).show();
}
db.close();
}
Please Help me
cursor = getContentResolver().query(GroceryItemProvider.ITEM_URI, projection, id+"="+GroceryItemTable.LIST_ID, null, null);
adapter= new SimpleCursorAdapter(this, R.layout.list_row, cursor, from, to , 0);
adapter.swapCursor(cursor);
This creates a list with the entire table rather than the cursor I pass with custom values, is that the default behaviour??
Do you mean you are getting all columns of data or all rows of data? If you are getting all columns, then please mention the value of variable projection. If you are getting all rows, then you can limit rows by providing the last argument to getContentResolver().query() method like:-
cursor = getContentResolver().query(GroceryItemProvider.ITEM_URI, projection, id+"="+GroceryItemTable.LIST_ID, null, "id limit 10");
i have been trying to bind spinner to a database and finally got succeeded. i have connected spinner to database table using cursoradapter. but the problem is the spinner gets populated but the list items in it shows blank text. i know binding is successful because it shows as many rows as there are records in database table.
can not figure out what it is. some body please help i am stuck here
i am posting the code below
public long createAccount() {
ContentValues initialValues = createContentValues();
Log.i("DB", initialValues.get(KEY_NAME)+":"+
initialValues.get(KEY_MAILBOXTYPE)+":"+
initialValues.get(KEY_OUTPORT)+":"+
initialValues.get(KEY_INPORT)+":"+
initialValues.get(KEY_INSERVER)+":"+
initialValues.get(KEY_OUTSERVER)+":");
return database.insert(DATABASE_TABLE, null, initialValues);
}
/**
* Return a Cursor over the list of all todo in the database
*
* #return Cursor over all notes
*/
public Cursor fetchAccount() {
Log.i("DB", "Cursor opened");
return database.query(DATABASE_TABLE, new String[] { KEY_ROWID,
KEY_NAME,KEY_INSERVER}, null, null, null,
null, null);
}
Code for spinner binding is below:
mAccAdap.createAccount();
Cursor c = mAccAdap.fetchAccount();
startManagingCursor(c);
SimpleCursorAdapter CursorAdapter = new SimpleCursorAdapter(
this,android.R.layout.simple_spinner_item,c,
new String[]{mAccAdap.KEY_INSERVER},new int[]{R.id.tvDBViewRow});
CursorAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinEmail=(Spinner)findViewById(R.id.spinAccount);
spinEmail.setAdapter(CursorAdapter);
Try changing this part of your SimpleCursorAdapter...
new int[]{R.id.tvDBViewRow}
to this...
new int[]{android.R.id.text1}
I am trying to populate a spinner with data from a database. The code is in a subroutine. Everything executes but the spinner is empty. If I copy the code to the "onCreate(Bundle saveInstanceState) it works fine. I am thinking I have context issue but can not figure it out.
Here is the subroutine:
public void showMatchNames (){
Cursor c;
String sMatchName = "MatchTable";
c = db.query(sMatchName, new String[]{KEY_ROWID, KEY_TITLE}, null, null, null, null, null);
if(c.moveToFirst()){
SimpleCursorAdapter MatchAdapter = new SimpleCursorAdapter(this,android.R.layout.simple_spinner_item, c, new String[]{KEY_TITLE}, new int[]{android.R.id.text1});
MatchAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner spin2 = (Spinner)this.findViewById(R.id.spinner2);
spin2.setAdapter(MatchAdapter);
}
scrMatchView.setOnItemSelectedListener(new mySpinnerListener());
c.close(); // Done with Cursor so close it
}
There is data in the database, confirmed by exporting to SQliteMan and doing a query.
I can also pull the names from the database and populate the spinner with a simple ArrayAdapter from the "showMatchNames" subroutine.
I am pretty sure it is something simple but I have been staring at this for hours now and can't get it to work.
Any help would be very welcome.