我对编程和安卓比较陌生,我正在开发一个小记事本应用程序。我从网上的教程中学习了如何使用 SQLite,但是出了点问题……当我运行应用程序时,它一直告诉我名称中没有列……但是有!我究竟做错了什么?我检查了几十次语法,我像教程一样一一做。我知道这个问题不止一次在这里,但是在阅读答案时,我仍然找不到解决我的具体问题的方法。基本上我只是添加了 2 个带有标题和文本的注释,并希望在 ListView 的主要活动中显示它们,但由于数据库问题没有显示任何内容。
非常感谢您的帮助:) 提前谢谢
这是课程:
import java.util.ArrayList;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseManager extends SQLiteOpenHelper{
private static final String DATABASE_NAME = "notesDb";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "notes";
private static final String NOTE_ID = "_id";
private static final String NOTE_TITLE = "title";
private static final String NOTE_TEXT = "text";
private static final String NOTE_CREATION_DATE = "creationDate";
public DatabaseManager(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_NOTES_TABLE = "CREATE TABLE "+TABLE_NAME+"(" + NOTE_ID +" INTEGER PRIMARY KEY,"+ NOTE_TITLE + "TEXT,"
+ NOTE_TEXT + "TEXT," + NOTE_CREATION_DATE + "TEXT" + ")";
db.execSQL(CREATE_NOTES_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS" + TABLE_NAME);
onCreate(db);
}
public void addNote(Note note){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(NOTE_TITLE, note.getTitle());
values.put(NOTE_TEXT, note.getText());
values.put(NOTE_CREATION_DATE, note.getCurrentTime());
db.insert(TABLE_NAME, null, values);
db.close();
}
public void deleteNote(Note note){
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_NAME, NOTE_ID + "?=",new String[]{String.valueOf(note.getId())} );
db.close();
}
public String[] listNotes(){
ArrayList<String> tempArray = new ArrayList<String>();
String[]notesArray = new String[0];
String sqlQuery = "SELECT * FROM " + TABLE_NAME;
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery(sqlQuery,null);
if(c.moveToFirst()){
do{
tempArray.add(c.getString(c.getColumnIndex(NOTE_TITLE)));
}
while(c.moveToNext());
}
c.close();
notesArray = tempArray.toArray(notesArray);
return notesArray;
}
}
笔记对象:
import java.util.Calendar;
public class Note {
private int id;
private String text;
private String title;
private String creationTime;
private String currentTime;
public Note(){
}
public String getCurrentTime(){
return getDayOfMonth()+"/"+getMonth()+"/"+getYear()+" "+getDay()+", "+getHour()+":"+getMinutes();
}
public String getMonth(){
int currentMonth = Calendar.getInstance().get(Calendar.MONTH);
String month = new String();
switch(currentMonth){
case 0:
month = "01";
break;
case 1:
month = "02";
break;
case 3:
month = "04";
break;
case 4:
month = "05";
break;
case 5:
month = "06";
break;
case 6:
month = "07";
break;
case 7:
month = "08";
break;
case 8:
month = "09";
break;
case 9:
month = "10";
break;
case 10:
month = "11";
break;
case 11:
month = "12";
break;
}
return month;
}
public String getDayOfMonth(){
int currentDayOfMonth = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
String dayOfMonth = new String();
if(currentDayOfMonth<10){
dayOfMonth = "0"+currentDayOfMonth;
}
else{
dayOfMonth = ""+currentDayOfMonth;
}
return dayOfMonth;
}
public String getYear(){
String year = ""+Calendar.getInstance().get(Calendar.YEAR);
return year;
}
public String getDay(){
int currentDay = Calendar.getInstance().get(Calendar.DAY_OF_WEEK);
String day = new String();
switch(currentDay){
case 1:
day = "Sun";
break;
case 2:
day = "Mon";
break;
case 3:
day = "Tue";
break;
case 4:
day = "Wed";
break;
case 5:
day = "Thu";
break;
case 6:
day = "Fri";
break;
case 7:
day = "Sat";
break;
}
return day;
}
public String getHour(){
int currentHour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
String hour = new String();
if(currentHour<10){
hour = "0"+currentHour;
}
else{
hour = ""+currentHour;
}
return hour;
}
public String getMinutes(){
int currentMinutes = Calendar.getInstance().get(Calendar.MINUTE);
String minutes = new String();
if(currentMinutes<10){
minutes = "0"+currentMinutes;
}
else{
minutes = ""+currentMinutes;
}
return minutes;
}
public void setText(String text){
this.text = text;
}
public String getText(){
return this.text;
}
public void setTitle(String title){
this.title = title;
}
public String getTitle(){
return this.title;
}
public void setId(int id){
this.id = id;
}
public int getId(){
return this.id;
}
}
主要活动:
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DatabaseManager db = new DatabaseManager(this);
Note n1 = new Note();
Note n2 = new Note();
n1.setTitle("n1 title");
n1.setText("n1 text");
n2.setTitle("n2 title");
n2.setText("n2 text");
db.addNote(n1);
db.addNote(n2);
String[]notes = db.listNotes();
ArrayAdapter<String>adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,notes);
ListView lv = (ListView)findViewById(R.id.notes_list);
lv.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
和xml文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/board"
android:scaleType="fitXY"/>
<ImageButton
android:id="@+id/add_note"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:src="@drawable/note"
android:background="@android:color/transparent"/>
<ListView
android:id="@+id/notes_list"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentTop="true"/>