我想在我的数据库中显示记录列表。但我得到一个空白的数据页面。这是主要活动。我认为我的问题在
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
//Log.d("Reading: ", "Inside onclick listener Loaded all questions..");
Toast.makeText(getApplicationContext(),
((TextView) v).getText(), Toast.LENGTH_SHORT).show();
// Log.d("Reading: ", "Toast Loaded all questions..");
}
这种方法。以下是我的主要活动。
public class MainActivity extends Activity {
private GridView gridView;
public static ArrayList<String> ArrayofName = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DatabaseHandler db = new DatabaseHandler(this);
// Reading all contacts
Log.d("Reading: ", "Reading all questions..");
List<addtodb> contacts = db.getAllQuestions();
for (addtodb cn : contacts) {
String log = "Id: "+cn.getID()+" ,Name: " + cn.getName() + " ,Answer: " + cn.getPhoneNumber();
// Writing Contacts to log
Log.d("Name: ", log);
}
db.getAllQuestions();
gridView = (GridView) findViewById(R.id.gridView1);
//Log.d("Reading: ", "Gridview Loaded all questions..");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, ArrayofName);
//Log.d("Reading: ", "Layout Loaded all questions..");
gridView.setAdapter(adapter);
Log.d("Reading: ", "gridview Loaded all questions..");
gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
//Log.d("Reading: ", "Inside onclick listener Loaded all questions..");
Toast.makeText(getApplicationContext(),
((TextView) v).getText(), Toast.LENGTH_SHORT).show();
// Log.d("Reading: ", "Toast Loaded all questions..");
}
});
}
}
My database handler class..
public class DatabaseHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "questions";
// Contacts table name
private static final String TABLE_QUESTIONS = "questions";
// Contacts Table Columns names
public static final String KEY_ID = "id";
public static final String KEY_NAME = "questions";
public static final String KEY_ANSWER = "answer";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_TABLE = "CREATE TABLE " + TABLE_QUESTIONS + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
+ KEY_ANSWER + " TEXT" + ")";
db.execSQL(CREATE_TABLE);
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_QUESTIONS);
// Create tables again
onCreate(db);
}
// Adding new contact
long addQuestion(String Question, String Answer) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, Question); // question Name
values.put(KEY_ANSWER, Answer); // answer
// Inserting Row
return db.insert(TABLE_QUESTIONS, null, values);
//db.close(); // Closing database connection
}
addtodb getQuestion(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_QUESTIONS, new String[] { KEY_ID,
KEY_NAME, KEY_ANSWER }, KEY_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
addtodb question = new addtodb(Integer.parseInt(cursor.getString(0)),
cursor.getString(1), cursor.getString(2));
return question;
}
public Cursor GetQuestionsByName(String inputText) throws SQLException {
// Log.w(TAG, inputText);
Cursor cursor = null;
SQLiteDatabase db = this.getReadableDatabase();
if (inputText == null || inputText.length () == 0) {
cursor = db.query(TABLE_QUESTIONS, new String[] {KEY_ID,
KEY_NAME, KEY_ANSWER},
null, null, null, null, null);
}
else {
cursor = db.query(true, TABLE_QUESTIONS, new String[] {KEY_ID,
KEY_NAME, KEY_ANSWER},
KEY_NAME + " like '%" + inputText + "%'", null,
null, null, null, null);
}
if (cursor != null) {
cursor.moveToFirst();
}
return cursor;
}
public List<addtodb> getAllQuestions() {
List<addtodb> QuestionList = new ArrayList<addtodb>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_QUESTIONS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor != null)
cursor.moveToFirst();
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
addtodb question = new addtodb();
question.setID(Integer.parseInt(cursor.getString(0)));
question.setName(cursor.getString(1));
question.setPhoneNumber(cursor.getString(2));
// Adding contact to list
QuestionList.add(question);
} while (cursor.moveToNext());
}
//System.out.print("Hello");
// return contact list
//return cursor;
return QuestionList; //to get the questions as a list
}
// Updating single contact
public int updateQuestion(addtodb question) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, question.getName());
values.put(KEY_ANSWER, question.getPhoneNumber());
// updating row
return db.update(TABLE_QUESTIONS, values, KEY_ID + " = ?",
new String[] { String.valueOf(question.getID()) });
}
// Deleting single contact
public void deleteContact(addtodb question) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_QUESTIONS, KEY_ID + " = ?",
new String[] { String.valueOf(question.getID()) });
db.close();
}
// Getting contacts Count
public int getContactsCount() {
String countQuery = "SELECT * FROM " + TABLE_QUESTIONS;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
cursor.close();
// return count
return cursor.getCount();
}
}
这是我的logcat。 02-28 23:44:21.577:W / ActivityThread(16086):应用程序com.example.database1正在等待
for the debugger on port 8100...
02-28 23:44:21.597: I/System.out(16086): Sending WAIT chunk
02-28 23:44:21.627: I/dalvikvm(16086): Debugger is active
02-28 23:44:21.807: I/System.out(16086): Debugger has connected
02-28 23:44:21.807: I/System.out(16086): waiting for debugger to settle...
02-28 23:44:22.008: I/System.out(16086): waiting for debugger to settle...
02-28 23:44:22.208: I/System.out(16086): waiting for debugger to settle...
02-28 23:44:22.416: I/System.out(16086): waiting for debugger to settle...
02-28 23:44:22.620: I/System.out(16086): waiting for debugger to settle...
02-28 23:44:22.817: I/System.out(16086): waiting for debugger to settle...
02-28 23:44:23.017: I/System.out(16086): waiting for debugger to settle...
02-28 23:44:23.232: I/System.out(16086): waiting for debugger to settle...
02-28 23:44:23.428: I/System.out(16086): waiting for debugger to settle...
02-28 23:44:23.637: I/System.out(16086): debugger has settled (1421)
02-28 23:44:24.546: D/Reading:(16086): Reading all questions..
02-28 23:44:24.826: D/Name:(16086): Id: 1 ,Name: Question1 ,Answer: answer1
02-28 23:44:24.856: D/Name:(16086): Id: 2 ,Name: Question2 ,Answer: answer2
02-28 23:44:24.856: D/Name:(16086): Id: 3 ,Name: Question3 ,Answer: answer3
02-28 23:44:24.866: D/Name:(16086): Id: 4 ,Name: Question4 ,Answer: answer4
02-28 23:44:24.876: D/Name:(16086): Id: 5 ,Name: Question5 ,Answer: answer5
02-28 23:44:24.876: D/Name:(16086): Id: 6 ,Name: Question6 ,Answer: answer6
02-28 23:44:24.896: D/Name:(16086): Id: 7 ,Name: Question7 ,Answer: answer7
02-28 23:44:24.896: D/Name:(16086): Id: 8 ,Name: Question8 ,Answer: answer8
02-28 23:44:24.906: D/Name:(16086): Id: 9 ,Name: Question9 ,Answer: answer9
02-28 23:44:24.906: D/Name:(16086): Id: 10 ,Name: Question10 ,Answer: answer10
02-28 23:44:25.046: D/Reading:(16086): gridview Loaded all questions..