我从Listview中的数据库中获取数据。我需要在用户点击它时删除此行。 我通过从Stackoverflow参考来尝试它,似乎一切正常但数据库没有更新。 请帮忙!!!
这是我的DatabaseAdapter1.java的代码
package com.example.utkarsh.stopwatch;
import android.content.ContentValues;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DatabaseAdapter1 {
DatabaseHelper1 helper;
public DatabaseAdapter1(Context context)
{
helper = new DatabaseHelper1(context);
}
static class DatabaseHelper1 extends SQLiteOpenHelper
{
private static final String DATABASE_NAME = "db_database1.db";
private static final int DATABASE_VERSION = 1;
public static final String TABLE_NAME = "db_table11";
public static final String UID = "_id";
public static final String TIMESTR = "timeInStr";
private static final String DROP_TABLE = "DROP TABLE IF EXISTS " + TABLE_NAME;
private static final String TABLE_CREATE =
"CREATE TABLE " + TABLE_NAME + " (" +
UID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
TIMESTR + " VARCHAR(255) " +
");";
private Context context;
public DatabaseHelper1(Context context)
{
super(context , DATABASE_NAME , null , DATABASE_VERSION);
this.context = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
try {
Log.v("qwerty" , "before onCreate");
db.execSQL(TABLE_CREATE);
Log.v("qwerty" , "after onCreate");
} catch (SQLException e) {
Log.v("qwerty" , "onCreate Error \n" + e);
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(DROP_TABLE);
onCreate(db);
}
}
public long insertData(String timeInMS)
{
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(DatabaseHelper1.TIMESTR, timeInMS );
long id = db.insert(DatabaseHelper1.TABLE_NAME , null , contentValues);
return id;
}
}
这是我的ShowAllTime.java的代码
package com.example.utkarsh.stopwatch;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
/**
* Created by utkarsh on 6/4/15.
*/
public class ShowAllTime extends Activity {
DatabaseAdapter1.DatabaseHelper1 dbHelper1;
DatabaseAdapter.DatabaseHelper dbHelper;
SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_all);
openDB();
populateDB();
registerListClickCallbacks();
}
private void openDB() {
dbHelper1 = new DatabaseAdapter1.DatabaseHelper1(this);
db = dbHelper1.getWritableDatabase();
}
private void populateDB() {
String [] column = {DatabaseAdapter1.DatabaseHelper1.UID , DatabaseAdapter1.DatabaseHelper1.TIMESTR};
Cursor cursor = db.query(DatabaseAdapter1.DatabaseHelper1.TABLE_NAME, column, null, null, null, null, DatabaseAdapter1.DatabaseHelper1.UID + " DESC");
startManagingCursor(cursor);
//setup mapping from cursor
String [] fromFieldNames = new String []
{DatabaseAdapter1.DatabaseHelper1.TIMESTR };
int [] toViewIds = new int[]
{R.id.itemLayout_textView };
//create adapter to map columns
SimpleCursorAdapter myCursorAdapter = new SimpleCursorAdapter(
this,
R.layout.item_layout,
cursor,
fromFieldNames,
toViewIds
);
//set my adapter to list view
ListView myList = (ListView) findViewById(R.id.listView);
myList.setAdapter(myCursorAdapter);
}
private void registerListClickCallbacks() {
final ListView myList = (ListView) findViewById(R.id.listView);
myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View viewClicked, final int position, long idInDB) {
AlertDialog.Builder ab;
ab = new AlertDialog.Builder(ShowAllTime.this);
ab.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Closing App")
.setMessage("Do you really wanna delete?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(ShowAllTime.this, "DELETE THIS SHIT at pos " + position ,Toast.LENGTH_LONG).show();
SQLiteDatabase db = dbHelper1.getWritableDatabase();
db.delete(DatabaseAdapter1.DatabaseHelper1.TABLE_NAME, DatabaseAdapter1.DatabaseHelper1.UID + "=" +position,null);
populateDB();
}
})
.setNegativeButton("No", null)
.show();
}
});
}
}
//reference taken from
//https://www.youtube.com/watch?v=gaOsl2TtMHs
答案 0 :(得分:1)
请试试这个:
db.delete(DatabaseAdapter1.DatabaseHelper1.TABLE_NAME, DatabaseAdapter1.DatabaseHelper1.UID + "=?", new String[] { String.valueOf(position) });
答案 1 :(得分:1)
我这样做的一种方法是使用删除和更新方法:
public void deleteCredit(long id) {
SQLiteDatabase database = this.getReadableDatabase();
String deleteQuery = "DELETE FROM " + Constants.CREDIT_TABLE_NAME + " WHERE id =" + id;
database.execSQL(deleteQuery);
}
然后是更新方法:
public boolean postCreditDeleteUpdate(String creditorId, double amountOwed) {
SQLiteDatabase database = this.getWritableDatabase();
String filter = Constants.CREDITOR_TABLE_ID +"= '" +creditorId +"'";
ContentValues contentValues = new ContentValues();
int newTotalCredits = getCreditor(creditorId).getTotalCredits() - 1;
double newAmountOwed = getCreditor(creditorId).getTotalAmountOwed() - amountOwed;
contentValues.put(Constants.CREDITOR_TABLE_TOTAL_CREDITS, newTotalCredits);
contentValues.put(Constants.CREDITOR_TABLE_TOTAL_AMOUNT_OWED, newAmountOwed);
return database.update(Constants.CREDITOR_TABLE_NAME, contentValues, filter, null) > 0;
}
我现在在我的Fragment(或Activity)中调用此代码,如下所示:
DatabaseManager databaseManager;
...
databaseManager.deleteCredit(creditId);
databaseManager.postCreditDeleteUpdate(creditorPhone, amountOwed);
我希望你明白这个想法?