[問題] SimpleCursorAdapter 無法執行
目前在寫SQLite的程式,
但在做SimpleCursorAdapter時,
無法將資料顯示,
照書上範本修改還是不行,
不知是否那裡有問題,
以下是程式碼:
取得資料:
public class mySQLiteOpenHelper extends SQLiteOpenHelper
{
private final static String DATABASE_NAME = "todo_db.db";
private final static int DATABASE_VERSION = 1;
private final static String TABLE_NAME = "todo_table";
public final static String FIELD_id = "id";
public final static String FIELD_TEXT = "context";
private SQLiteDatabase db;
public mySQLiteOpenHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
db = this.getWritableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db)
{
String sql = "CREATE TABLE " + TABLE_NAME + " (" + FIELD_id
+ " INTEGER primary key autoincrement, " + " " + FIELD_TEXT + " text)";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
String sql = "DROP TABLE IF EXISTS " + TABLE_NAME;
db.execSQL(sql);
onCreate(db);
}
public Cursor select()
{
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_NAME, null, null, null, null, null, null);
return cursor;
}
public long insert(String text)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues args = new ContentValues();
args.put(FIELD_id, 2);
args.put(FIELD_TEXT, text);
return db.insert(TABLE_NAME, null, args);
}
public void delete(int id)
{
SQLiteDatabase db = this.getWritableDatabase();
String where = FIELD_id + " = ?";
String[] whereValue =
{ Integer.toString(id) };
ContentValues c= new ContentValues();
db.delete(TABLE_NAME, "id=?", new String[]{"2"});
}
public void update(int id, String text)
{
/*
SQLiteDatabase db = this.getWritableDatabase();
String where = FIELD_id + " = ?";
String[] whereValue =
{ Integer.toString(id) };
*/
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL("update todo_table set context='C' where id=2");
}
}
主程式:
public class GDD02 extends Activity
{
private mySQLiteOpenHelper gaaSQLiteOpenHelper;
private Cursor myCursor;
private ListView myListView;
private EditText myEditText;
private int _id;
protected final static int MENU_ADD = Menu.FIRST;
protected final static int MENU_EDIT = Menu.FIRST + 1;
protected final static int MENU_DELETE = Menu.FIRST + 2;
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
super.onOptionsItemSelected(item);
switch (item.getItemId())
{
case MENU_ADD:
this.addTodo();
break;
case MENU_EDIT:
this.editTodo();
break;
case MENU_DELETE:
this.deleteTodo();
break;
}
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
super.onCreateOptionsMenu(menu);
menu.add(0, MENU_ADD, 0, "MENU_ADD");
menu.add(0, MENU_EDIT, 0, "MENU_EDIT");
menu.add(0, MENU_DELETE, 0, "MENU_DELETE");
return true;
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myListView = (ListView) this.findViewById(R.id.myListView);
myEditText = (EditText) this.findViewById(R.id.myEditText);
gaaSQLiteOpenHelper = new mySQLiteOpenHelper(this);
myCursor = gaaSQLiteOpenHelper.select();
startManagingCursor(myCursor);
//這是資料庫欄位
String[] columns = new String[]{gaaSQLiteOpenHelper.FIELD_TEXT};
//要顯示的元件
int[] to = new int[]{R.id.listView1};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.list
,myCursor, columns, to );
gaaSmyListView.setAdapter(adapter);
}
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 61.227.145.245
→
08/29 16:48, , 1F
08/29 16:48, 1F
AndroidDev 近期熱門文章
PTT數位生活區 即時熱門文章