Spinner Data Connectivity issue- data binding successful but spinner shows blank text - android

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}

Related

Retrieve cursor from sqldatabase and present it using simple cursor 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

Android listview with database and fixed values

I'm quite beginner with android and I have problem with my listview.
So I'm getting listview values from database and thats working fine. But I want to edit one of those values before showing it to user and after many failures I kindly ask your advice.
Here is my code for fetching data and showing it in listview
listSquad=(ListView)findViewById(R.id.listview);
String[] arrayColumns = new String[]{"sq_pos", "sq_name", "sq_born", "sq_gam", "sq_goal"};
int[] arrayViewID = new int[]{R.id.pos,R.id.name,R.id.age,R.id.games,R.id.goals};
DBAdapter db = new DBAdapter(this);
db.open();
Cursor c;
c = db.doQuery("squad", null, null, null, null, null, "sq_name");
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.team_list_lay, c, arrayColumns, arrayViewID);
listSquad.setAdapter(adapter);
So I would like to take value of "sq_born" column and alter it and then show up in listview. That column is integer.
Like suggested by Aditya Rai, I used viewbinder like below:
adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
public boolean setViewValue(View view, Cursor cursor, int column) {
if( view.getId() == R.id.age){
//do something here
TextView text = (TextView)view;
//set text in here
text.setText("blaah");
return true;
}
return false;
}
});

Retrieve data from SQLite database

inHow do i retrieve data from sqlite database from adapter class and insert to listview???
I'm having difficulty, been coding day & night and googling non-stop but i just don't understand the links when i open them up. I just start learning android programming recently on my own...
I have attach my code below
public Cursor RetrieveActivityCursor(String NRIC){
String where = NRIC;
Cursor cursor = _db.query(DATABASE_TABLE,new String[]{ MOBILE_HOUSE_VISIT_ID, ELDERLY_NAME,
ELDERLY_NRIC, ELDERLY_DATE_TIME, PHYSICAL_HEALTH_STATUS, MENTAL_HEALTH_STATUS} , where, null, null, null, null);
//Cursor cursor = _db.query(DATABASE_TABLE, new String[] { ELDERLY_NAME,
//ELDERLY_NRIC, DATE_TIME, PHYSICAL_HEALTH_STATUS, MENTAL_HEALTH_STATUS }, null, null, null, null, null);
return cursor;
}
this code is from my adapter class and have to pass in nric and return datetime value inserted into listview
im not too sure how to code to call this method.
If you are extending the SqliteOpenHelper for the database handling and using another class to which extends Activity, in which you have your ListView then, in your Activity class make the object of your class like,
YourDBClass helperDB;
helperDB = new HelperDB(YourActivityClass.this);
And to retrive the data from the Database.
Make Cursor reference like,
Cursor cursor;
And then, do like this,
cursor = helperDB.RetrieveActivityCursor(NRIC);
cursor.moveToFirst();
while(!cursor.isAfterLast()) {
// here you have to collect the data in the collection object.
cursor.moveToNext();
}
cursor.close();
That's it! And you have done with retriving data from database
you need iterate cursor, like this:
Cursor cursor = RetrieveActivityCursor(NRIC);
if (cursor != null && cursor.moveToNext())
do {
String str = cursor.getString(cursor.getColumnIndex("your_column"));
// do something, your code
} while (cursor.moveToNext());
Where you like to retrieve the date value do the below over there.
Cursor cursor = RetrieveActivityCursor(NRIC);
if(mCursor.moveToFirst()) {
do{
String date_time = cursor.getString(cursor.getColumnIndex(column_name));
} while(mCursor.moveToNext());
}
cursor.close();

Android SQLite retrieving data from the database and displaying that data in text

I have created a working SQlite database in my application. I can add new values to the database. I have even created a method which displays all the results of the table on a new activity.
What I would this new activity really to do, is to display the most recent entry into text views. Picking individual fields and slotting them into correct text views. How do I go about doing this?
Assuming you are using autogenerated ID in your table, you need to sort the results based on ID and limit the results to '1' which will return only that row.
return db.query(true, "yourTableName", new String[] { "columnNamesYouwanttoQuery" },
null, null, null, null, "_id DESC", "1");
Then loop through the cursor and get yourColumns
Cursor cur = adaptor.getMileageReport(AutoMainActivity.CARNAME);
startManagingCursor(cur);
if (cur.moveToFirst()) {
do {
String retrievedVal = cur.getString(0);
}
}

ListView Data from SimpleCursorAdapter result of a query

Since a few month I am engaged with Android development and now I have a problem I dont get the right answer.
I have a ListView with data filled from a SimpleCursorAdapter. The query which should provides the results has a where statement, but it returns all records of the table.
final Cursor c = mStorage.loadList();
startManagingCursor(c);
c.moveToFirst();
final ListCursorAdapter adapter =
new ListCursorAdapterAusgabe(this, R.layout.listview,
c, FROM, TO);
adapter.setViewBinder(new ListViewBinderAusgabe());
setListAdapter(adapter);
The query:
int mode = 0;
public Cursor loadList() {
return mDb.getReadableDatabase().query(TABLE.NAME, TABLE.ALL_COLUMNS,
"mode=?", new String [] {String.valueOf(mode)}, null, null, null, null);
}
In read in this forum that there must be an _id column: In the above mentioned table there is one column with "_id". I also tried to enter "mode" in single quotations and double quotations but it didn't work. Has anybody an idea?
Thank you in advance,
Hadja
Have you tried
return mDb.getReadableDatabase().query(TABLE.NAME, TABLE.ALL_COLUMNS,
"mode=" + String.valueOf(mode), null, null, null, null, null);
?

Categories

Resources