【发布时间】:2014-05-19 06:10:12
【问题描述】:
我有 SQLite 数据库,我想在用户完成注册表单后在 ListView 中显示数据
注意:我使用的是 Fragment
此课程用于注册表
public class DocReg extends Fragment {
TextView tvDocRegID, tvDocCode, tvLastname, tvFirstname, tvSpecialty, tvCardID, tvRegion, tvLocation, tvContact;
EditText editDocRegID, editDocCode, editLastname, editFirstname, editCardID, editLocation, editContact;
Spinner spinSpecialty, spinRegion;
Button btnReg;
SQLController dbcon;
public DocReg() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.doc_reg, container,
false);
// TextView
tvDocRegID = (TextView) view.findViewById(R.id.tvDocRegID);
tvDocCode = (TextView) view.findViewById(R.id.tvDocCode);
tvLastname = (TextView) view.findViewById(R.id.tvLastname);
tvFirstname = (TextView) view.findViewById(R.id.tvFirstname);
tvSpecialty = (TextView) view.findViewById(R.id.tvSpecialty);
tvCardID = (TextView) view.findViewById(R.id.tvCardID);
tvRegion = (TextView) view.findViewById(R.id.tvRegion);
tvLocation = (TextView) view.findViewById(R.id.tvLocation);
tvContact = (TextView) view.findViewById(R.id.tvContact);
// EditText
editDocRegID = (EditText) view.findViewById(R.id.editDocRegID);
editDocCode = (EditText) view.findViewById(R.id.editDocCode);
editLastname = (EditText) view.findViewById(R.id.editLastname);
editFirstname = (EditText) view.findViewById(R.id.editFirstname);
editCardID = (EditText) view.findViewById(R.id.editCardID);
editLocation = (EditText) view.findViewById(R.id.editLocation);
editContact = (EditText) view.findViewById(R.id.editContact);
// Spinner
spinSpecialty = (Spinner) view.findViewById(R.id.spinSpecialty);
spinRegion = (Spinner) view.findViewById(R.id.spinRegion);
btnReg = (Button) view.findViewById(R.id.btnRegister);
btnReg.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String regid = editDocRegID.getText().toString();
String doc_code = editDocCode.getText().toString();
String firstname = editFirstname.getText().toString();
String lastname = editLastname.getText().toString();
String specialty = spinSpecialty.getSelectedItem().toString();
String location = editLocation.getText().toString();
String card_id = editCardID.getText().toString();
String region = spinRegion.getSelectedItem().toString();
String contact = editContact.getText().toString();
dbcon.insertData(regid, doc_code, firstname, lastname, specialty, card_id, region, location, contact);
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.content_frame, new FragmentInsertDoctor());
ft.commit();
}
});
return view;
}
}
这是我的 SQLiteOpenHelper 和 DB 类
public class SqlDbHelper extends SQLiteOpenHelper {
public static final String DATABASE_TABLE = "doc_table";
public static final String DATABASE_NAME = "doc_db";
public static final int DATABASE_VERSION = 1;
public static final String COL_ID = "col_id";
public static final String COL_REG_ID = "reg_id";
public static final String COL_DOC_CODE = "doc_code";
public static final String COL_FNAME = "firstname";
public static final String COL_LNAME = "lastname";
public static final String COL_SPEC = "specialty";
public static final String COL_CARD_ID = "card_id";
public static final String COL_REGION = "region";
public static final String COL_LOCATION ="location";
public static final String COL_CONTACT = "contact";
private static final String CREATE_DATABASE = "CREATE TABLE "
+ DATABASE_TABLE + " (" + COL_ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + COL_REG_ID
+ " TEXT NOT NULL, " + COL_DOC_CODE
+ " TEXT NOT NULL, " + COL_FNAME
+ " TEXT NOT NULL, " + COL_LNAME
+ " TEXT NOT NULL, " + COL_SPEC
+ " TEXT NOT NULL, " + COL_CARD_ID
+ " TEXT NOT NULL, " + COL_REGION
+ " TEXT NOT NULL, " + COL_LOCATION
+ " TEXT NOT NULL, " + COL_CONTACT + " TEXT NOT NULL);";
public SqlDbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(CREATE_DATABASE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
}
控制器
public class SQLController {
private SqlDbHelper dbhelper;
private Context context;
private SQLiteDatabase database;
public SQLController(Context c) {
context = c;
}
public SQLController open() throws SQLException {
dbhelper = new SqlDbHelper(context);
database = dbhelper.getWritableDatabase();
return this;
}
public void close() {
dbhelper.close();
}
public void insertData(String regid, String doc_code, String firstname, String lastname,
String specialty, String card_id, String region, String location, String contact) {
ContentValues cv = new ContentValues();
cv.put(SqlDbHelper.COL_REG_ID, regid);
cv.put(SqlDbHelper.COL_DOC_CODE, doc_code);
cv.put(SqlDbHelper.COL_FNAME, firstname);
cv.put(SqlDbHelper.COL_LNAME, lastname);
cv.put(SqlDbHelper.COL_SPEC, specialty);
cv.put(SqlDbHelper.COL_CARD_ID, card_id);
cv.put(SqlDbHelper.COL_REGION, region);
cv.put(SqlDbHelper.COL_LOCATION, location);
cv.put(SqlDbHelper.COL_CONTACT, contact);
database.insert(SqlDbHelper.DATABASE_TABLE, null, cv);
}
public Cursor readData() {
String[] allColumns = new String[] { SqlDbHelper.COL_ID, SqlDbHelper.COL_REG_ID, SqlDbHelper.COL_DOC_CODE,
SqlDbHelper.COL_FNAME, SqlDbHelper.COL_LNAME, SqlDbHelper.COL_SPEC, SqlDbHelper.COL_CARD_ID,
SqlDbHelper.COL_REGION, SqlDbHelper.COL_LOCATION, SqlDbHelper.COL_CONTACT };
Cursor c = database.query(SqlDbHelper.DATABASE_TABLE, allColumns, null, null, null, null, null);
if(c != null){
c.moveToFirst();
}
return c;
}
}
这里我想使用 ListView 显示数据,但我只想显示 COL_FNAME COL_LNAME COL_SPEC COL_LOC
public class FragmentInsertDoctor extends Fragment {
TextView tvFirstname, tvLastname;
EditText editFirstname, editLastname;
ListView docList;
SQLiteDatabase db;
SQLController dbcon;
Cursor cursor;
private List<Doctor> Doctor = new ArrayList<Doctor>();
public static final String IMAGE_RESOURCE_ID = "iconResourceID";
public static final String ITEM_NAME = "itemName";
public FragmentInsertDoctor() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.insertdoctor, container,
false);
docList = (ListView) view.findViewById(R.id.LVdoctor);
dbcon = new SQLController(getActivity());
dbcon.open();
Cursor cursor = dbcon.readData();
String[] from = new String[] { SqlDbHelper.COL_FNAME, SqlDbHelper.COL_LNAME, SqlDbHelper.COL_SPEC, SqlDbHelper.COL_LOCATION };
int[] to = new int[] { R.id.docFname, R.id.docLname, R.id.docSpec, R.id.docLoc };
SimpleCursorAdapter adapter = new SimpleCursorAdapter(getActivity(), R.layout.doc_view, cursor, from, to);
adapter.notifyDataSetChanged();
docList.setAdapter(adapter);
return view;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
getActivity().getMenuInflater().inflate(R.menu.add_doctor, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.addbutton:
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.content_frame, new DocReg());
ft.commit();
return false;
default:
break;
}
return false;
}
}
我尝试运行此代码,但我从 LogCat 在线收到错误
SimpleCursorAdapter adapter = new SimpleCursorAdapter(getActivity(), R.layout.doc_view, cursor, from, to);
LogCat:
05-19 13:45:42.284: E/Trace(4448): error opening trace file: No such file or directory (2)
05-19 13:45:42.694: D/dalvikvm(4448): GC_FOR_ALLOC freed 85K, 3% free 6101K/6275K, paused 38ms, total 39ms
05-19 13:45:42.704: I/dalvikvm-heap(4448): Grow heap (frag case) to 7.361MB for 1440016-byte allocation
05-19 13:45:42.824: D/dalvikvm(4448): GC_CONCURRENT freed 1K, 3% free 7506K/7687K, paused 19ms+2ms, total 117ms
05-19 13:45:42.934: D/dalvikvm(4448): GC_FOR_ALLOC freed 0K, 3% free 7506K/7687K, paused 27ms, total 27ms
05-19 13:45:42.934: I/dalvikvm-heap(4448): Grow heap (frag case) to 7.969MB for 639616-byte allocation
05-19 13:45:42.984: D/dalvikvm(4448): GC_CONCURRENT freed 0K, 3% free 8130K/8327K, paused 16ms+2ms, total 47ms
05-19 13:45:42.984: D/dalvikvm(4448): WAIT_FOR_CONCURRENT_GC blocked 6ms
05-19 13:45:43.064: D/libEGL(4448): loaded /system/lib/egl/libEGL_mali.so
05-19 13:45:43.114: D/libEGL(4448): loaded /system/lib/egl/libGLESv1_CM_mali.so
05-19 13:45:43.124: D/libEGL(4448): loaded /system/lib/egl/libGLESv2_mali.so
05-19 13:45:43.154: D/OpenGLRenderer(4448): Enabling debug mode 0
05-19 13:45:47.804: D/dalvikvm(4448): GC_FOR_ALLOC freed 1419K, 19% free 6795K/8327K, paused 19ms, total 19ms
05-19 13:45:47.804: I/dalvikvm-heap(4448): Grow heap (frag case) to 7.325MB for 691216-byte allocation
05-19 13:45:47.844: D/dalvikvm(4448): GC_CONCURRENT freed 2K, 11% free 7468K/8327K, paused 12ms+3ms, total 36ms
05-19 13:45:47.844: D/dalvikvm(4448): WAIT_FOR_CONCURRENT_GC blocked 14ms
05-19 13:45:48.874: D/dalvikvm(4448): GC_CONCURRENT freed 724K, 15% free 7129K/8327K, paused 12ms+3ms, total 49ms
05-19 13:45:49.934: I/Choreographer(4448): Skipped 72 frames! The application may be doing too much work on its main thread.
05-19 13:45:54.444: D/AndroidRuntime(4448): Shutting down VM
05-19 13:45:54.444: W/dalvikvm(4448): threadid=1: thread exiting with uncaught exception (group=0x40bd3300)
05-19 13:45:54.494: E/AndroidRuntime(4448): FATAL EXCEPTION: main
05-19 13:45:54.494: E/AndroidRuntime(4448): java.lang.IllegalArgumentException: column '_id' does not exist
05-19 13:45:54.494: E/AndroidRuntime(4448): at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:302)
05-19 13:45:54.494: E/AndroidRuntime(4448): at android.support.v4.widget.CursorAdapter.init(CursorAdapter.java:174)
05-19 13:45:54.494: E/AndroidRuntime(4448): at android.support.v4.widget.CursorAdapter.<init>(CursorAdapter.java:122)
05-19 13:45:54.494: E/AndroidRuntime(4448): at android.support.v4.widget.ResourceCursorAdapter.<init>(ResourceCursorAdapter.java:54)
05-19 13:45:54.494: E/AndroidRuntime(4448): at android.support.v4.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:63)
05-19 13:45:54.494: E/AndroidRuntime(4448): at com.droid.FragmentInsertDoctor.onCreateView(FragmentInsertDoctor.java:73)
05-19 13:45:54.494: E/AndroidRuntime(4448): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:829)
05-19 13:45:54.494: E/AndroidRuntime(4448): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
05-19 13:45:54.494: E/AndroidRuntime(4448): at android.app.BackStackRecord.run(BackStackRecord.java:635)
05-19 13:45:54.494: E/AndroidRuntime(4448): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1397)
05-19 13:45:54.494: E/AndroidRuntime(4448): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:426)
05-19 13:45:54.494: E/AndroidRuntime(4448): at android.os.Handler.handleCallback(Handler.java:615)
05-19 13:45:54.494: E/AndroidRuntime(4448): at android.os.Handler.dispatchMessage(Handler.java:92)
05-19 13:45:54.494: E/AndroidRuntime(4448): at android.os.Looper.loop(Looper.java:137)
05-19 13:45:54.494: E/AndroidRuntime(4448): at android.app.ActivityThread.main(ActivityThread.java:4745)
05-19 13:45:54.494: E/AndroidRuntime(4448): at java.lang.reflect.Method.invokeNative(Native Method)
05-19 13:45:54.494: E/AndroidRuntime(4448): at java.lang.reflect.Method.invoke(Method.java:511)
05-19 13:45:54.494: E/AndroidRuntime(4448): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
05-19 13:45:54.494: E/AndroidRuntime(4448): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-19 13:45:54.494: E/AndroidRuntime(4448): at dalvik.system.NativeStart.main(Native Method)
05-19 13:45:56.434: I/Process(4448): Sending signal. PID: 4448 SIG: 9
【问题讨论】:
标签: android database sqlite listview