我正在尝试从数据库中检索数据。我没有收到任何错误,但数据未显示。 这是我的代码:
DbHelper
public class DbHelper extends SQLiteOpenHelper{
static final String tag="dbHelper";
static final String DB_NAME="dianaaaa.db";
static final int DB_VERSION=1;
static final String TABLE_JOBS="jobs";
static final String C_START="start";
static final String C_STOP="stop";
static final String C_ID_J=BaseColumns._ID;
Context context;
SQLiteDatabase db;
public DbHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
// TODO Auto-generated constructor stub
this.context=context;
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql2="create table "+TABLE_JOBS+"( "+C_ID_J+" INTEGER PRIMARY KEY, "+C_START+" text, "+C_STOP+" text)";
try{
db.execSQL(sql2);
}
catch(SQLException e){
Log.d(tag, "no");
}finally{
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exists "+TABLE_JOBS);
Log.d(tag, "onUpdated");
onCreate(db);
}
public void insertJStart(String s1){
SQLiteDatabase db = (SQLiteDatabase) getWritableDatabase();
ContentValues values=new ContentValues();
values.clear();
values.put(DbHelper.C_START, s1);
try{
db.insertOrThrow(TABLE_JOBS, C_START, values);
}catch(SQLException e){
Log.d(tag, "nu vreau....");
}finally{
db.close();
}
}
public void insertJStop(String s2){
SQLiteDatabase db=(SQLiteDatabase) getWritableDatabase();
ContentValues values=new ContentValues();
values.clear();
values.put(DbHelper.C_STOP, s2);
db.insertOrThrow(TABLE_JOBS, C_STOP, values);
db.close();
}
public Cursor getCursor(){
SQLiteQueryBuilder queryBuilder=new SQLiteQueryBuilder();
queryBuilder.setTables(TABLE_JOBS);
String[] asColumToRetrun=new String[] { C_ID, C_START, C_STOP};
Cursor mCursor=db.query(TABLE_JOBS, asColumToRetrun, null, null, null, null, "time ASC");
return mCursor;
}
public String getDate(Cursor c, int i){
return (c.getString(i));
}
public Cursor getAll(){
db=this.getReadableDatabase();
Cursor c=db.rawQuery("SELECT * FROM jobs ", new String[]{});
return c;
}
}
的活动:
public class TimerDB extends Activity{
static final String tag="TimerDB";
private ListView list;
MyAdapter aa=null;
private Cursor cursor=null;
private DbHelper helper=null;
final ArrayList<String> aList=new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.times);
list=(ListView)findViewById(R.id.timeList);
helper=new DbHelper(this);
cursor=helper.getAll();
cursor.moveToNext();
startManagingCursor(cursor);
aa=new MyAdapter(cursor);
list.setAdapter(aa);
}
class MyAdapter extends CursorAdapter{
MyAdapter(Cursor c){
super(TimerDB.this, c);
}
public View getView(int position, View convertView, ViewGroup parent){
View row=convertView;
if(row==null){
LayoutInflater inflater=getLayoutInflater();
row=inflater.inflate(R.layout.row, null);
}
return (row);
}
@Override
public void bindView(View row, Context context, Cursor c) {
// TODO Auto-generated method stub
Holder holder=(Holder) row.getTag();
holder.populateFrom(c, helper);
c.moveToNext();
}
@Override
public View newView(Context context, Cursor c, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater=getLayoutInflater();
View row=inflater.inflate(R.layout.row, parent, false);
Holder holder=new Holder(row);
row.setTag(holder);
return row;
}
}
static class Holder {
private TextView id, start, stop;
Holder(View row){
id=(TextView) row.findViewById(R.id.rowViewStart);
start=(TextView) row.findViewById(R.id.rowViewStart);
stop=(TextView) row.findViewById(R.id.rowViewStop);
}
void populateFrom(Cursor c, DbHelper db){
id.setText(db.getDate(c, 0));
start.setText(db.getDate(c, 1));
stop.setText(db.getDate(c, 2));
}
}
}
答案 0 :(得分:0)
在您的自定义适配器中,您要么实施getView()
方法,要么实施newView()
和bindView()
方法(但不能与getView()
一起实施)。现在你实现了这两个方法,但也override
getView()
,你可以从这些方法“覆盖”你的适配器实现。只需离开newView()
和bindView()
,然后移除getView()
方法。
如果查看android源代码,在游标适配器的情况下,getView()
方法将行创建委托给那些方法(它为newView()
对象调用View
并且然后bindView()
绑定游标中的数据。
编辑:
- 也不要拨打c.moveToNext();
,Cursor
将始终正确定位。