SimpleCursorAdapter from SQLiteDatabase Cursor not displaying properly in Spinner adapter
i'm trying to connect query a database and display the results in a Spinner. below is what i have so far. unfortunately the spinner only displays the last row that should have been returned from the query. so from the example above, only "Elvis Presley" shows up in the list. i've tried using "Cursor.moveToFirst()" (as you can see) but that doesn't do it.
i realized that i rebuild and update the database everytime i open it. i'm doing this on purpose for now.
package com.conceptualsyste开发者_运维知识库ms.android4api.sms;
import java.util.List;
import java.util.ArrayList;
import java.util.LinkedList;
import android.app.Activity;
import android.app.Dialog;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.view.Window;
import android.view.View;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.EditText;
import android.widget.Toast;
import android.widget.SimpleCursorAdapter;
public class smsActivity extends Activity
{
private static class smsDbOpenHelper extends SQLiteOpenHelper {
smsDbOpenHelper(Context context) {
super(context,smsDbSchema.DATABASE_NAME, null, smsDbSchema.DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(smsDbSchema.CustomerSchema.CREATE_TABLE);
db.execSQL(smsDbSchema.ProductSchema.CREATE_TABLE);
}
@Override
public void onOpen(SQLiteDatabase db) {
//open db
db.execSQL("DROP TABLE IF EXISTS " + smsDbSchema.CustomerSchema.TABLE_NAME);
db.execSQL("DROP TABLE IF EXISTS " + smsDbSchema.ProductSchema.TABLE_NAME);
this.onCreate(db);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//upgrade db
}
}
private smsDbOpenHelper mDbHelper;
private SQLiteDatabase mDb;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
mDbHelper = new smsDbOpenHelper(getApplicationContext());
mDb = mDbHelper.getReadableDatabase();
//fill customer table with some fake data
ContentValues cv = new ContentValues();
cv.put(smsDbSchema.CustomerSchema.COLUMN_NAME, "Charles Keith Gilliam");
cv.put(smsDbSchema.CustomerSchema.COLUMN_NAME, "Jarrod Martin");
cv.put(smsDbSchema.CustomerSchema.COLUMN_NAME, "John Lennon");
cv.put(smsDbSchema.CustomerSchema.COLUMN_NAME, "Sammy Hagar");
cv.put(smsDbSchema.CustomerSchema.COLUMN_NAME, "David Lee Roth");
cv.put(smsDbSchema.CustomerSchema.COLUMN_NAME, "Keith Richards");
cv.put(smsDbSchema.CustomerSchema.COLUMN_NAME, "Steven Tyler");
cv.put(smsDbSchema.CustomerSchema.COLUMN_NAME, "Brent Michaels");
cv.put(smsDbSchema.CustomerSchema.COLUMN_NAME, "Eddie Veder");
cv.put(smsDbSchema.CustomerSchema.COLUMN_NAME, "Kurt Cobain");
cv.put(smsDbSchema.CustomerSchema.COLUMN_NAME, "Elvis Presley");
cv.put(smsDbSchema.CustomerSchema._ID, "0298437598745");
cv.put(smsDbSchema.CustomerSchema._ID, "7585684317298");
cv.put(smsDbSchema.CustomerSchema._ID, "7585684317298");
cv.put(smsDbSchema.CustomerSchema._ID, "7585684317298");
cv.put(smsDbSchema.CustomerSchema._ID, "7585684317298");
cv.put(smsDbSchema.CustomerSchema._ID, "7585684317298");
cv.put(smsDbSchema.CustomerSchema._ID, "7585684317298");
cv.put(smsDbSchema.CustomerSchema._ID, "7585684317298");
cv.put(smsDbSchema.CustomerSchema._ID, "7585684317298");
cv.put(smsDbSchema.CustomerSchema._ID, "7585684317298");
cv.put(smsDbSchema.CustomerSchema._ID, "7585684317298");
mDb.insert(smsDbSchema.CustomerSchema.TABLE_NAME, null, cv);
//cv.
setScreen(R.layout.inbound);
Spinner custSpn = (Spinner)findViewById(R.id.cust_spn);
Cursor custCur = null;
try {
custCur = mDb.query(smsDbSchema.CustomerSchema.TABLE_NAME, null, null, null, null, null, null);
} catch(Exception e) {
Log.e("smsdb", e.toString());
}
custCur.moveToFirst();
startManagingCursor(custCur);
SimpleCursorAdapter qc = new SimpleCursorAdapter(
this,
android.R.layout.simple_spinner_item,
custCur,
new String[] {smsDbSchema.CustomerSchema.COLUMN_NAME},
new int[] {android.R.id.text1}
);
qc.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
custSpn.setAdapter(qc);
Spinner prdSpn = (Spinner)findViewById(R.id.prd_spn);
Cursor prdCur = null;
try {
prdCur = mDb.query(smsDbSchema.ProductSchema.TABLE_NAME, null, null, null, null, null, null);
} catch(Exception e) {
Log.e("smsdb", e.toString());
}
prdCur.moveToFirst();
startManagingCursor(prdCur);
qc = new SimpleCursorAdapter(
this,
android.R.layout.simple_spinner_item,
prdCur,
new String[] {smsDbSchema.ProductSchema.COLUMN_NAME},
new int[] {android.R.id.text1}
);
qc.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
prdSpn.setAdapter(qc);
mDb.close();
}
public void setScreen(int resource) {
setContentView(resource);
if(resource==R.layout.inbound) {
//setup main entry screen
final Button transmit = (Button)findViewById(R.id.transmit_btn);
final Button clear = (Button)findViewById(R.id.clear_btn);
transmit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// transmit data ////////
/////////////////////////
}
});
clear.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//clear fields
}
});
}
}
}
You are putting values in Content Values.
ContentValues cv = new ContentValues();
cv.put(smsDbSchema.CustomerSchema.COLUMN_NAME, "Charles Keith Gilliam");
..
..
..
Anf finally u write a insert statement. So, to my understanding essentially u put only one value in the ContentValues since others are overwritten.
You can also pull the db file using DDMS and open using SQLite browser to make that all entires are there in the DB
You should try inserting each content value lik this. Pls try this so that all entries are made in the db.
cv.put(smsDbSchema.CustomerSchema.COLUMN_NAME, "Charles Keith Gilliam");
cv.put(smsDbSchema.CustomerSchema._ID, "7585684317298");
mDb.insert(smsDbSchema.CustomerSchema.TABLE_NAME, null, cv);
cv.put(smsDbSchema.CustomerSchema.COLUMN_NAME, "Jarrod Martin");
cv.put(smsDbSchema.CustomerSchema._ID, "0298437598745");
mDb.insert(smsDbSchema.CustomerSchema.TABLE_NAME, null, cv);
精彩评论