我正在制作一个酒单应用程序。我也试图在其中使用SQLite,我很难掌握它。
我有很多活动,但现在有两项与此相关。
我目前遇到的麻烦是当我在列表中添加葡萄酒时,当我回到MainActivity时,列表不显示新添加的Wine,直到我关闭应用程序并返回它。 / p>
我该如何解决这个问题?
这是我的主要方法:
public class MainActivity extends AppCompatActivity {
DatabaseHelper mDatabaseHelper;
ListView listView;
ArrayAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intentGoToAdd = new Intent(MainActivity.this, AddActivity.class);
startActivity(intentGoToAdd);
}
});
mDatabaseHelper = new DatabaseHelper(this);
arrayListAndAdapter();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long l) {
SQLiteDatabase db = mDatabaseHelper.getReadableDatabase();
//Which columns I want to select
String[] projection = {
WineContract.WineEntry.COLUMN_ID,
WineContract.WineEntry.COLUMN_WINE_NAME,
WineContract.WineEntry.COLUMN_WINE_PRICE,
WineContract.WineEntry.COLUMN_WINE_DESCRIPTION};
Cursor cursor = db.query(WineContract.WineEntry.TABLE_NAME, projection,null,null,null,null,null);
cursor.moveToPosition(position);
Intent intentGoToDetails = new Intent(MainActivity.this,DetailsActivity.class);
String name = cursor.getString(cursor.getColumnIndex(WineContract.WineEntry.COLUMN_WINE_NAME));
String price = cursor.getString(cursor.getColumnIndex(WineContract.WineEntry.COLUMN_WINE_PRICE));
String description = cursor.getString(cursor.getColumnIndex(WineContract.WineEntry.COLUMN_WINE_DESCRIPTION));
intentGoToDetails.putExtra("NAME", name);
intentGoToDetails.putExtra("PRICE", price);
intentGoToDetails.putExtra("DESCRIPTION", description);
startActivity(intentGoToDetails);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_deleteDB) {
mDatabaseHelper.deleteDatabase();
return true;
}
return super.onOptionsItemSelected(item);
}
public void arrayListAndAdapter(){
SQLiteDatabase db = mDatabaseHelper.getReadableDatabase();
//Which columns I want to select
String[] projection = {
WineContract.WineEntry.COLUMN_ID,
WineContract.WineEntry.COLUMN_WINE_NAME,
WineContract.WineEntry.COLUMN_WINE_PRICE,
WineContract.WineEntry.COLUMN_WINE_DESCRIPTION};
Cursor cursor = db.query(WineContract.WineEntry.TABLE_NAME, projection,null,null,null,null,null);
ArrayList<String> mArrayList = new ArrayList<String>();
while(cursor.moveToNext()) {
mArrayList.add(cursor.getString(cursor.getColumnIndex(WineContract.WineEntry.COLUMN_WINE_NAME))); //add the item
}
listView = (ListView) findViewById(R.id.list);
adapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1, mArrayList);
listView.setAdapter(adapter);
}
}
以下是AddActivity:
public class AddActivity extends AppCompatActivity {
EditText editWineName, editWinePrice, editWineDescription;
DatabaseHelper mDatabaseHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
mDatabaseHelper = new DatabaseHelper(this);
editWineName = (EditText) findViewById(R.id.editWineName);
editWinePrice = (EditText) findViewById(R.id.editWinePrice);
editWineDescription = (EditText) findViewById(R.id.editWineDescription);
}
public void toastMessage(String message){
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_add, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_deleteDB) {
long result = mDatabaseHelper.addWine(editWineName.getText().toString(), editWinePrice.getText().toString(), editWineDescription.getText().toString() );
if (result!=-1){
toastMessage(editWineName.getText().toString() + " was added to the list.");
finish();
}else{
toastMessage("Error");
}
return true;
}
return super.onOptionsItemSelected(item);
}
}
如有必要,这是我的DatabaseHelper类:
public class DatabaseHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "wines.db";
private static final String SQL_CREATE =
"CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_WINE_NAME + " TEXT, " +
COLUMN_WINE_PRICE + " TEXT, " +
COLUMN_WINE_DESCRIPTION + " TEXT);";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public long addWine(String name, String price, String description){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_WINE_NAME, name);
values.put(COLUMN_WINE_PRICE, price);
values.put(COLUMN_WINE_DESCRIPTION, description);
return db.insert(TABLE_NAME,null, values);
}
public void deleteDatabase(){
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_NAME,null,null);
}
}
答案 0 :(得分:1)
您的活动仅创建一次,因此onCreate仅在第一次调用。你想要的是每次MainActivity恢复时数据都是新鲜的。如果重写onResume方法并放入arrayListAndAdapter()方法和setOnItemClickListener,您将获得所需的效果。所以这是新代码(您可以从onCreate中删除此代码以防止查询两次):
@Override
protected void onResume() {
super.onResume();
arrayListAndAdapter();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long l) {
SQLiteDatabase db = mDatabaseHelper.getReadableDatabase();
//Which columns I want to select
String[] projection = {
WineContract.WineEntry.COLUMN_ID,
WineContract.WineEntry.COLUMN_WINE_NAME,
WineContract.WineEntry.COLUMN_WINE_PRICE,
WineContract.WineEntry.COLUMN_WINE_DESCRIPTION};
Cursor cursor = db.query(WineContract.WineEntry.TABLE_NAME, projection,null,null,null,null,null);
cursor.moveToPosition(position);
Intent intentGoToDetails = new Intent(MainActivity.this,DetailsActivity.class);
String name = cursor.getString(cursor.getColumnIndex(WineContract.WineEntry.COLUMN_WINE_NAME));
String price = cursor.getString(cursor.getColumnIndex(WineContract.WineEntry.COLUMN_WINE_PRICE));
String description = cursor.getString(cursor.getColumnIndex(WineContract.WineEntry.COLUMN_WINE_DESCRIPTION));
intentGoToDetails.putExtra("NAME", name);
intentGoToDetails.putExtra("PRICE", price);
intentGoToDetails.putExtra("DESCRIPTION", description);
startActivity(intentGoToDetails);
}
});