我的应用未显示数据库中的数据。我无法弄清楚为什么这不起作用,因为我相信我的所有设置都已正确完成。任何人都可以帮我弄清楚为什么我的数据没有显示?
这是我的列表视图代码:
package com.work.plan;
// multiple imports .... not included for length purpose
public class WorkoutList extends Activity {
//====================================================================
// Member variables
private Button addNewWorkout;
private WorkoutDbAdapter mDbHelper;
public static final int CREATE_WORKOUT = 1;
public static final int SET_WORKOUT = 2;
private String dateCreated = null;
private Calendar now = null;
private SimpleDateFormat format = null;
private ListView myWorkoutList;
Intent prevIntent ;
String woName,userName;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.workout_list);
addNewWorkout = (Button) findViewById(R.id.btnNewWorkout);
prevIntent = getIntent();
userName = prevIntent.getStringExtra("userName");
// do the work for getting the current time and formatting it
now = Calendar.getInstance();
format = new SimpleDateFormat("EEE MMM dd hh:mm aaa");
dateCreated = format.format(now.getTime());
mDbHelper = new WorkoutDbAdapter(this);
mDbHelper.open();
myWorkoutList = (ListView)findViewById(R.id.workoutList);
registerForContextMenu(myWorkoutList);
myWorkoutList.setDivider(getResources().getDrawable(R.color.mainDivider));
myWorkoutList.setDividerHeight(1);
myWorkoutList.setOnItemClickListener(WorkoutListener);
addNewWorkout.setOnClickListener(NewWorkout);
fillData();
}
OnClickListener NewWorkout = new OnClickListener(){
// ......... click listener code for my add button
// ......... creates a new workout
};
//===============================================================================
//
@Override
public void onPause(){
super.onPause();
}
//===============================================================================
//
@Override
public void onResume(){
super.onResume();
mDbHelper.open();
}
@Override
public void onDestroy(){
super.onDestroy();
mDbHelper.close();
}
//================================================================================
//
// Fill the data for UI rebuilds
private void fillData(){
Log.d("User_NAME",""+ userName);
Cursor workoutCursor = mDbHelper.fetchAllWorkouts();
startManagingCursor(workoutCursor);
String [] from = new String [] { WorkoutDbAdapter.KEY_WORKOUT_NAME,
WorkoutDbAdapter.KEY_WORKOUT_CREATED_DATE ,WorkoutDbAdapter.KEY_WORKOUT_TYPE};
int [] to = new int [] {R.id.dateCreatedLabel, R.id.nameLabel, R.id.typeLabel};
SimpleCursorAdapter workouts = new SimpleCursorAdapter(this, R.layout.workout_row, workoutCursor, from, to);
myWorkoutList.setAdapter(workouts);
}
@Override
public void onCreateContextMenu(ContextMenu menu , View v, ContextMenuInfo menuInfo){
//........ stuff for my menu
}
@Override
public boolean onContextItemSelected(MenuItem item){
//.......... code for my menu, handles menu clicks
return super.onContextItemSelected(item);
}
//================================================================================
OnItemClickListener WorkoutListener = new OnItemClickListener(){
public void onItemClick(AdapterView<?> arg0, View view, int position,
final long id) {
AlertDialog.Builder dialog = new AlertDialog.Builder(WorkoutList.this)
.setIcon(R.drawable.edit)
.setTitle("Update Selected Workout")
.setMessage("Would you like to update the current Workout? Click continue to proceed.")
.setPositiveButton("Continue", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
final Intent i = new Intent(getBaseContext(), ExerciseList.class);
i.putExtra(WorkoutDbAdapter.KEY_ROW_ID, id);
i.putExtra("workoutName", woName);
startActivityForResult(i, SET_WORKOUT);
}
})
.setNegativeButton("Back", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
dialog.show();
}
};
}
然后我有一个数据库适配器,在SQLite中创建表'workouts'。
这是WorkoutDbAdapter代码:
public class WorkoutDbAdapter {
//=========================================================================
// Member Variables
public static final String DATABASE_TABLE = "workouts";
public static final String KEY_ROW_ID = "_id";
public static final String KEY_WORKOUT_EMAIL = "email";
public static final String KEY_WORKOUT_NAME = "workout_name";
public static final String KEY_WORKOUT_CREATED_DATE = "created_date";
public static final String KEY_WORKOUT_TYPE = "type";
// string query that creates the table in the database
static final String CREATE_TABLE_WORKOUTS = ("CREATE TABLE " + DATABASE_TABLE + "("+ KEY_ROW_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ KEY_WORKOUT_EMAIL + " TEXT NOT NULL,"
+ KEY_WORKOUT_NAME + " TEXT NOT NULL,"
+ KEY_WORKOUT_CREATED_DATE + " TEXT NOT NULL,"
+ KEY_WORKOUT_TYPE + " TEXT NOT NULL);");
private final Context m_ClassContext; // reference to the current class
private DatabaseHelper mDbHelper; // reference the database helper class
private SQLiteDatabase mDb; // reference to the SQLiteDatabase class
// class constructor
public WorkoutDbAdapter(Context ctx){
this.m_ClassContext = ctx;
}
// open the database with a reference to this class
public WorkoutDbAdapter open() throws SQLException{
this.mDbHelper = new DatabaseHelper(m_ClassContext);
this.mDb = mDbHelper.getWritableDatabase();
return this;
}
// create a work out
public long createWorkout(String email, String workoutName, String workoutDate, String workoutType){
ContentValues values = new ContentValues();
values.put(KEY_WORKOUT_EMAIL, email);
values.put(KEY_WORKOUT_NAME, workoutName);
values.put(KEY_WORKOUT_CREATED_DATE, workoutDate);
values.put(KEY_WORKOUT_TYPE, workoutType);
long insertValue = this.mDb.insert(DATABASE_TABLE, null, values);
return insertValue;
}
// delete a Work out
public boolean deleteWorkout(long rowId){
return this.mDb.delete(DATABASE_TABLE, KEY_ROW_ID + "=" +rowId,null) > 0;
}
//**My error was here**
// fetch allWorkouts for this user
public Cursor fetchAllWorkouts(String email){
return this.mDb.query(DATABASE_TABLE, new String[]{KEY_ROW_ID,
KEY_WORKOUT_EMAIL, KEY_WORKOUT_NAME, KEY_WORKOUT_CREATED_DATE, KEY_WORKOUT_TYPE},
KEY_WORKOUT_EMAIL+ "='" +email+"'",null,null,null,null); // i removed a null and it works
}
// fetch a work out
public Cursor fetchWorkout(long rowId) throws SQLException{
Cursor mCursor = mDb.query(true, DATABASE_TABLE,new String[]{KEY_ROW_ID,
KEY_WORKOUT_EMAIL,KEY_WORKOUT_NAME,KEY_WORKOUT_CREATED_DATE, KEY_WORKOUT_TYPE}, KEY_ROW_ID + "="+rowId, null,null,null,null,null);
if(mCursor != null){
mCursor.moveToFirst();
}
return mCursor;
}
public boolean updateWorkout(long rowId, String email, String workoutName, String createdDate, String workoutType){
ContentValues values = new ContentValues();
values.put(KEY_WORKOUT_EMAIL, email);
values.put(KEY_WORKOUT_NAME, workoutName);
values.put(KEY_WORKOUT_CREATED_DATE, createdDate);
values.put(KEY_WORKOUT_TYPE, workoutType);
return this.mDb.update(DATABASE_TABLE, values, KEY_ROW_ID +"="+rowId, null)> 0;
}
// close the database
public void close(){
if(mDbHelper != null){
mDbHelper.close();
mDb.close();
}
}
}
答案 0 :(得分:0)
您可能遇到数据库代码问题,游标大小为0.观察logcat是否存在失败的SQL查询并调试游标大小。