我的问题如下:
它是在控制台上写的。
[2013-02-24 14:30:40 - AdatBpro] Android Launch!
[2013-02-24 14:30:40 - AdatBpro] adb is running normally.
[2013-02-24 14:30:40 - AdatBpro] Performing hu.AB.example.adatbpro.MainActivity activity launch
[2013-02-24 14:30:40 - AdatBpro] Automatic Target Mode: Unable to detect device compatibility. Please select a target device.
[2013-02-24 14:30:47 - AdatBpro] Launch canceled!
[2013-02-24 14:31:04 - AdatBpro] emulator-5554 disconnected! Cancelling 'hu.AB.example.adatbpro.MainActivity activity launch'!
[2013-02-24 14:42:15 - AdatBpro] ------------------------------
[2013-02-24 14:42:15 - AdatBpro] Android Launch!
[2013-02-24 14:42:15 - AdatBpro] adb is running normally.
[2013-02-24 14:42:15 - AdatBpro] Performing hu.AB.example.adatbpro.MainActivity activity launch
[2013-02-24 14:42:16 - AdatBpro] Automatic Target Mode: launching new emulator with compatible AVD 'AVD412'
[2013-02-24 14:42:16 - AdatBpro] Launching a new emulator with Virtual Device 'AVD412'
[2013-02-24 14:42:16 - Emulator] WARNING: Data partition already in use. Changes will not persist!
[2013-02-24 14:42:16 - Emulator] WARNING: SD Card image already in use: C:\Users\Edit\.android\avd\AVD412.avd/sdcard.img
[2013-02-24 14:42:16 - Emulator] WARNING: Cache partition already in use. Changes will not persist!
[2013-02-24 14:42:16 - AdatBpro] New emulator found: emulator-5556
[2013-02-24 14:42:16 - AdatBpro] Waiting for HOME ('android.process.acore') to be launched...
[2013-02-24 14:43:32 - AdatBpro] HOME is up on device 'emulator-5556'
[2013-02-24 14:43:32 - AdatBpro] Uploading AdatBpro.apk onto device 'emulator-5556'
[2013-02-24 14:43:33 - AdatBpro] Installing AdatBpro.apk...
[2013-02-24 14:44:30 - AdatBpro] Success!
程序正在运行并且数据库打开,但是当第一个模拟器什么也没写时。我再次尝试运行。第一个模拟器在那里打开并打开第二个模拟器并在第二个模拟器上写入所有内容。
它只在模拟器上制作。
它不在电话上运行。
为什么要做这个?我做错了什么?
我找不到解决方案。
公共类 DbConstants {
public static final String DATABASE_NAME = "tar.db";
public static final int DATABASE_VERSION = 1;
public static String DATABASE_CREATE_ALL = Todo.DATABASE_CREATE;
public static String DATABASE_DROP_ALL = Todo.DATABASE_DROP;
public static class Todo {
// public static final String DATABASE_NAME = "data.db";
public static final String DATABASE_TABLE = "todo";
public static final String KEY_ROWID = "_id";
public static final String KEY_KI = "ki";
public static final String DATABASE_CREATE = "create table if not exists "
+ DATABASE_TABLE
+ "("
+ KEY_ROWID
+ " integer primary key autoincrement, "
+ KEY_KI
+ " text );";
public static final String DATABASE_DROP = "drop table if exists"
+ DATABASE_TABLE + "; ";
private long id;
private String ki;
public Todo(String aKi)
{
ki = aKi;
}
@Override
public String toString() {
return ki;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getKi() {
return ki;
}
public void setKi(String ki) {
this.ki = ki;
}
}
}
公共类 MainActivity 扩展 ListActivity {
私有 TodoCursorAdapter 适配器;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
TodoDbLoader dbLoader = new TodoDbLoader(getApplicationContext());
dbLoader.open();
Cursor c = dbLoader.fetchAll();
if (c.getCount() == 0) {
dbLoader.createTodo(new Todo("ÉN"));
dbLoader.createTodo(new Todo("Sári"));
dbLoader.createTodo(new Todo("Saci"));
c = dbLoader.fetchAll();
}
c.moveToFirst();
Adapter = new TodoCursorAdapter(getApplicationContext(), c);
setListAdapter(Adapter);
}
}
公共类 DatabaseHelper 扩展 SQLiteOpenHelper {
public DatabaseHelper(Context context, String name){
super(context, name, null, DbConstants.DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(DbConstants.DATABASE_CREATE_ALL);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL(DbConstants.DATABASE_DROP_ALL);
db.execSQL(DbConstants.DATABASE_CREATE_ALL);
}
}
公共类 TodoDbLoader{
private Context ctx;
private DatabaseHelper dbHelper;
private SQLiteDatabase mDb;
public TodoDbLoader(Context ctx){
this.ctx= ctx;
}
public void open()throws SQLException{
dbHelper = new DatabaseHelper(ctx,DbConstants.DATABASE_NAME);
mDb = dbHelper.getWritableDatabase();
dbHelper.onCreate(mDb);
}
public void close(){
dbHelper.close();
}
public long createTodo(Todo todo){
ContentValues values = new ContentValues();
values.put(DbConstants.Todo.KEY_KI, todo.getKi());
return mDb.insert(DbConstants.Todo.DATABASE_TABLE, null, values);
}
public boolean deleteTodo(long rowId){
return mDb.delete(
DbConstants.Todo.DATABASE_TABLE,
DbConstants.Todo.KEY_ROWID + "=" + rowId, null) >
0;
}
public boolean deleteAllTodo(){
return mDb.delete(DbConstants.Todo.DATABASE_TABLE , null, null) > 0;
}
public boolean updateProduct(long rowId, Todo newTodo){
ContentValues values = new ContentValues();
values.put(DbConstants.Todo.KEY_KI, newTodo.getKi());
return mDb.update(DbConstants.Todo.DATABASE_TABLE,
values,
DbConstants.Todo.KEY_ROWID + "=" + rowId, null) >
0;
}
public Cursor fetchAll(){
return mDb.query(DbConstants.Todo.DATABASE_TABLE,
new String[]{ DbConstants.Todo.KEY_ROWID,
DbConstants.Todo.KEY_KI,
}, null, null, null, null, DbConstants.Todo.KEY_KI);
}
public Todo fetchTodo(long rowId){
Cursor c = mDb.query(DbConstants.Todo.DATABASE_TABLE,
new String[]{ DbConstants.Todo.KEY_ROWID,
DbConstants.Todo.KEY_KI,
},DbConstants.Todo.KEY_ROWID + "=" + rowId,
null, null, null, DbConstants.Todo.KEY_KI);
if (c.moveToFirst())
return getTodoByCursor(c);
return null;
}
public static Todo getTodoByCursor(Cursor c){
return new Todo(
c.getString(c.getColumnIndex(DbConstants.Todo.KEY_KI))
);
}
}
公共类 TodoCursorAdapter 扩展 CursorAdapter {
public TodoCursorAdapter(Context context, Cursor c){
super(context, c, false);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
final LayoutInflater inflater = LayoutInflater.from(context);
View row = inflater.inflate(R.layout.todorow, null);
bindView(row, context, cursor);
return row;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView KiTV = (TextView) view.findViewById(R.id.textViewKi);
Todo todo = TodoDbLoader.getTodoByCursor(cursor);
KiTV.setText(todo.getKi());
}
@Override
public Todo getItem(int position) {
getCursor().moveToPosition(position);
return TodoDbLoader.getTodoByCursor(getCursor());
}
}