So I'm following a tutorial about a very basic SQLite database on Android. The code shows NO ERRORS and I can open the application when I execute it. But it's supposed to show the list in my database but it doesn't ... and I don't understand why.
main.xml:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical"
xmlns:android="http://ift.tt/nIICcg">
<TextView android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="@string/hello_world"/>
<TextView android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="@+id/contentlist"/>
</LinearLayout>
Main_Activity:
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class AndroidSQLite extends Activity {
private SQLiteAdapter mySQLiteAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView listContent = (TextView)findViewById(R.id.contentlist);
mySQLiteAdapter = new SQLiteAdapter(this);
mySQLiteAdapter.openToWrite();
mySQLiteAdapter.deleteAll();
mySQLiteAdapter.insert("ABCDE");
mySQLiteAdapter.insert("FGHIJK");
mySQLiteAdapter.insert("1234567");
mySQLiteAdapter.insert("890");
mySQLiteAdapter.insert("Testing");
mySQLiteAdapter.close();
mySQLiteAdapter = new SQLiteAdapter(this);
mySQLiteAdapter.openToRead();
String contentRead = mySQLiteAdapter.queueAll();
mySQLiteAdapter.close();
listContent.setText(contentRead);
}
}
Adapter.java:
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
public class SQLiteAdapter {
public static final String MYDATABASE_NAME = "MY_DATABASE";
public static final String MYDATABASE_TABLE = "MY_TABLE";
public static final int MYDATABASE_VERSION = 1;
public static final String KEY_CONTENT = "Content";
private static final String SCRIPT_CREATE_DATABASE =
"create table " + MYDATABASE_TABLE + " ("
+ KEY_CONTENT + " text not null);";
private SQLiteHelper sqLiteHelper;
private SQLiteDatabase sqLiteDatabase;
private Context context;
public SQLiteAdapter(Context c){
context = c;
}
public SQLiteAdapter openToRead() throws android.database.SQLException {
sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
sqLiteDatabase = sqLiteHelper.getReadableDatabase();
return this;
}
public SQLiteAdapter openToWrite() throws android.database.SQLException {
sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
sqLiteDatabase = sqLiteHelper.getWritableDatabase();
return this;
}
public void close(){
sqLiteHelper.close();
}
public long insert(String content){
ContentValues contentValues = new ContentValues();
contentValues.put(KEY_CONTENT, content);
return sqLiteDatabase.insert(MYDATABASE_TABLE, null, contentValues);
}
public int deleteAll(){
return sqLiteDatabase.delete(MYDATABASE_TABLE, null, null);
}
public String queueAll(){
String[] columns = new String[]{KEY_CONTENT};
Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, columns,
null, null, null, null, null);
String result = "";
int index_CONTENT = cursor.getColumnIndex(KEY_CONTENT);
for(cursor.moveToFirst(); !(cursor.isAfterLast()); cursor.moveToNext()){
result = result + cursor.getString(index_CONTENT) + "\n";
}
return result;
}
public class SQLiteHelper extends SQLiteOpenHelper {
public SQLiteHelper(Context context, String name,
CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(SCRIPT_CREATE_DATABASE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
}
Aucun commentaire:
Enregistrer un commentaire