我在listView中单击项目后,同时尝试删除listView和数据库中的对象。我想在删除项目之前打开一个警告对话框。我已经阅读了许多关于此的堆栈溢出问题,例如:
how can i delete an item from listview and also database
delete a specific item from listview stored in database in android application
Delete item from database - ListView - Android
还有很多人......我发现他们都没有帮助。有什么建议吗?
Main_Activity:
public class New_Recipe extends AppCompatActivity {
Button add, done;
EditText Recipe_Name, Recipe_Item, Recipe_Steps;
String search;
WebView webView;
DatabaseHelper databaseHelper;
ListView ItemsList;
Context context = this;
SQLiteDatabase sqLiteDatabase;
Cursor cursor;
RecipeListAdapter listAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new__recipe);
setTitle("New Recipe");
add = (Button) findViewById(R.id.button2);
done = (Button) findViewById(R.id.button);
Recipe_Name = (EditText) findViewById(R.id.editText);
Recipe_Item = (EditText) findViewById(R.id.editText2);
Recipe_Steps = (EditText) findViewById(R.id.editText3);
webView = (WebView) findViewById(R.id.webView);
ItemsList = (ListView) findViewById(R.id.listView);
listAdapter = new RecipeListAdapter(getApplicationContext(), R.layout.recipe_textview);
context = this;
AddData();
}
//When the add button is pressed
public void AddData() {
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String item = Recipe_Item.getText().toString();
databaseHelper = new DatabaseHelper(context);
sqLiteDatabase = databaseHelper.getWritableDatabase();
databaseHelper.addItems(item, sqLiteDatabase);
Toast.makeText(getBaseContext(), "Item inserted", Toast.LENGTH_LONG).show();
databaseHelper.close();
ItemsList.setAdapter(listAdapter);
databaseHelper = new DatabaseHelper(getApplicationContext());
sqLiteDatabase = databaseHelper.getReadableDatabase();
cursor = databaseHelper.getItems(sqLiteDatabase);
if (cursor.moveToFirst()) {
do {
String items;
items = cursor.getString(0);
RecipeDataProvider dataProvider = new RecipeDataProvider(items);
listAdapter.add(dataProvider);
} while (cursor.moveToNext());
}
}
});
}
public void onSearch(View v) {
search = "Recipes";
webView.loadUrl("https://www.google.com/search?q=" + search);
}
@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_new__recipe, 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_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
DatabaseHelper类:
public class DatabaseHelper extends SQLiteOpenHelper {
public static final int DATABASE_VERSION = 5;
public static final String DATABASE_NAME = "Recipes.db";
public static final String CREATE_QUERRY = "create table " + RecipeContract.RecipeEntry.TABLE_NAME + "( _ID INTEGER PRIMARY KEY, NAME text, ITEMS text, STEPS text)";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
Log.d("Recipe Database", "Database should be made!");
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_QUERRY);
Log.d("Recipe Database", "Table should be made!");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void addItems(String items, SQLiteDatabase db)
{
ContentValues values = new ContentValues();
values.put(RecipeContract.RecipeEntry.COL2,items);
db.insert(RecipeContract.RecipeEntry.TABLE_NAME,null,values);
Log.e("Recipe Database","Item should be added to the table!");
}
public Cursor getItems(SQLiteDatabase db)
{
Cursor cursor;
String[] projections = {RecipeContract.RecipeEntry.COL2};
cursor = db.query(RecipeContract.RecipeEntry.TABLE_NAME,projections,null,null,null,null,null);
return cursor;
}
ListViewAdapter:
public class RecipeListAdapter extends ArrayAdapter {
List list = new ArrayList();
public RecipeListAdapter(Context context, int resource) {
super(context, resource);
}
static class LayoutHandler
{
TextView ITEM;
}
@Override
public void add(Object object) {
super.add(object);
list.add(object);
}
@Override
public int getCount() {
return super.getCount();
}
@Override
public Object getItem(int position) {
return super.getItem(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
LayoutHandler layoutHandler;
if (row == null)
{
LayoutInflater layoutInflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = layoutInflater.inflate(R.layout.recipe_textview,parent,false);
layoutHandler = new LayoutHandler();
layoutHandler.ITEM = (TextView) row.findViewById(R.id.tx_items);
row.setTag(layoutHandler);
} else {
layoutHandler = (LayoutHandler) row.getTag();
}
RecipeDataProvider dataProvider = (RecipeDataProvider) this.getItem(position);
layoutHandler.ITEM.setText(dataProvider.getItems().toString());
return row;
}
编辑:我更新的Main_Activity类
public class New_Recipe extends AppCompatActivity {
Button add, done;
EditText Recipe_Name, Recipe_Item, Recipe_Steps;
String search;
WebView webView;
DatabaseHelper databaseHelper;
ListView ItemsList;
Context context = this;
SQLiteDatabase sqLiteDatabase;
Cursor cursor;
RecipeListAdapter listAdapter;
List<New_Recipe> list = new ArrayList<>();
private int id;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new__recipe);
setTitle("New Recipe");
add = (Button) findViewById(R.id.button2);
done = (Button) findViewById(R.id.button);
Recipe_Name = (EditText) findViewById(R.id.editText);
Recipe_Item = (EditText) findViewById(R.id.editText2);
Recipe_Steps = (EditText) findViewById(R.id.editText3);
webView = (WebView) findViewById(R.id.webView);
ItemsList = (ListView) findViewById(R.id.listView);
listAdapter = new RecipeListAdapter(getApplicationContext(), R.layout.recipe_textview);
context = this;
AddData();
ItemsList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Object getSelectedItem = list.get(position);
databaseHelper.deleteItem(getSelectedItem);
listAdapter.deleteitem(getSelectedItem);
return true;
}
});
}
public New_Recipe(){
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
答案 0 :(得分:1)
您可以使用此方法从DatabaseHelper类中删除行:
public void deleteItem(Object item) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_NAME, KEY_ID + " = ?",
new String[]{String.valueOf(item.getId())});
db.close();
}
然后你还需要一个从适配器中删除对象的方法:
public void deleteItem(Object item) {
listArray.remove(item);
notifyDataSetChanged();
}
现在你可以在这样的活动中调用所有这个方法:
让我们假设您要通过单击列表项上的长按来删除所有这些:
listView.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int pos, long id) {
Object getSelectedItem = arrayList.get(pos);
dataBaseHelper.deleteItem(getSelectedItem);
adapter.deleteItem(getSelectedItem);
return true;
}
});
<强>更新:强>
在尝试调用方法getId()
之前,您需要在类中创建该方法。所以你需要在你的类中创建get和setter方法,如下所示:
一步一步:
将您的班级New_Recipe
更改为MainActivity
。如果您收到错误,只需突出显示该错误,您将看到&#34;重命名文件&#34;。
在主要课程之外创建一个新课程,并将其命名为NewRecipe
:
public class NewRecipe {
private int id;
private String title;
public NewRecipe() {
// empty constructor
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
现在你可以做我之前解释的所有事情。
答案 1 :(得分:0)
您需要DatabaseHelper类中的函数delete
public boolean delete(long rowId) {
return db.delete(TABLE_NAME, KEY_ROWID + "=" + rowId, null) > 0;
}
您需要在活动deleteItem
()中使用与addItems()
public void deleteItem(){
//Based on some logic,find the rowID of the table which needs to be deleted and call delete function of DatabaseHelper class.
}
在ListViewAdapter中,您需要定义一个方法delete
public void delete(Object object) {
list.remove(object);
//To update the ListView in Android
this.notifyDataSetChanged();
}