我的列表视图活动,这里是我想查看列表,最终我会过滤它,现在在某个阶段我可以添加活动从列表中获取行并进行编辑吗?
public class CBFilter extends Activity{
ListView RecipeNames;
Cursor cursor;
SimpleCursorAdapter adapter;
CBDataBaseHelper DH;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
RecipeNames = (ListView) findViewById(R.id.List1);
adapter = new SimpleCursorAdapter (this, 0, cursor, null, null);
CBDataBaseHelper data = new CBDataBaseHelper(this);
data.open();
cursor = data.query();
startManagingCursor(cursor);
String[] from = { CBDataBaseHelper.KEY_NAME};
int[] to = { R.id.recipeText};
adapter = new SimpleCursorAdapter(this, R.layout.row, cursor, from, to);
RecipeNames.setAdapter(adapter);
}
public void CreateNew(View view){
Intent myIntent = new Intent(this, CBCreate.class);
startActivity(myIntent);
}
}
我的DatabaseHelper类,以帮助编辑我的数据库
public class CBDataBaseHelper {
public static final String KEY_ROWID = "_id";
public static final String KEY_NAME = "Recipe_Name";
public static final String KEY_CATEGORY = "Recipe_Category";
public static final String KEY_DESCRIPTION = "Recipe_Description";
public static final String KEY_ROWID2 = "_id";
public static final String KEY_NAME2 = "Ingredient_Name";
private static final String DATABASE_NAME = "Recipedb";
private static final String DATABASE_TABLE = "RecipeData";
//private static final String DATABASE_TABLE2 = "IngredientData";
private static final int DATABASE_VERSION = 1;
private DBHelper myHelper;
private final Context mycontext;
private SQLiteDatabase mydatabase;
private static class DBHelper extends SQLiteOpenHelper {
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + DATABASE_TABLE + "( " + KEY_ROWID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_NAME
+ " TEXT NOT NULL, " + KEY_CATEGORY + " TEXT NOT NULL,"
+ KEY_DESCRIPTION + " TEXT NOT NULL" + ")");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
}
public CBDataBaseHelper(Context c){
mycontext = c;
}
public CBDataBaseHelper open() throws SQLException{
myHelper = new DBHelper(mycontext);
mydatabase = myHelper.getWritableDatabase();
return this;
}
public void close(){
myHelper.close();
}
public long createEntry(String name){
ContentValues cv = new ContentValues();
cv.put(KEY_NAME, name);
return mydatabase.insert(DATABASE_TABLE, null, cv);
}
public Cursor query() {
// Open Database
String[] columns = new String[]{KEY_ROWID, KEY_NAME, KEY_CATEGORY, KEY_DESCRIPTION};
Cursor cursor = mydatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
return cursor;
}
}
我的创建Record类,现在我有一个try catch来查看这个insert命令是否会起作用并返回true所以我不知道这是否是我获取行等的方式。
public class CBCreate extends Activity {
EditText EditRecipe;
Button Rname;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.create); // sets the content view to main
EditRecipe = (EditText) findViewById(R.id.updateRecipe);
Rname = (Button) findViewById(R.id.RecipeName);
//Rname.setOnClickListener(this);
}
public void doIt(View view) {
boolean didItWork = true;
try {
String Name = EditRecipe.getText().toString();
CBDataBaseHelper entry = new CBDataBaseHelper(CBCreate.this);
entry.open();
entry.createEntry(Name);
Dialog d = new Dialog(this);
d.setTitle(Name);
TextView tv = new TextView(this);
tv.setText(Name);
d.setContentView(tv);
d.show();
entry.close();
} catch (Exception e ) {
didItWork = false;
Dialog d = new Dialog(this);
String error = e.toString();
d.setTitle("dang it!");
TextView tv = new TextView(this);
tv.setText(error);
d.setContentView(tv);
d.show();
} finally {
if (didItWork){
Dialog d = new Dialog(this);
d.setTitle("Heck ya!");
TextView tv = new TextView(this);
tv.setText("Success");
d.setContentView(tv);
d.show();
}
}
}
public void goBack(View view){
Intent myIntent = new Intent(this, CBFilter.class);
startActivity(myIntent);
}
}
创建记录并返回过滤器活动后,没有带lisview的记录
谢谢Stefan
答案 0 :(得分:0)
当您返回到过滤器活动时,请通过调用适配器上的notifydatasetchanged告诉listview刷新。