Wednesday 22 April 2015

Simple usage of sqlite with android.//sqlite 간단한 사용법 -3 delete sqlite

Introduce how to use sqlite with Android - Delete saved URL.




















1. popup_list.xml


...
<Button
        android:id="@+id/bDelete"
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:layout_gravity="center"
        android:layout_toRightOf="@+id/col2tv"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:background="@drawable/delete" />
...

2. WebviewActivity.java


...
private void initiatePopupWindow() {
...
Button b = (Button) v.findViewById(R.id.bDelete);
b.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
new AlertDialog.Builder(ctx)
.setTitle("Script entry")
.setMessage(
"Are you sure you want to delete this script?")
.setPositiveButton(
android.R.string.yes,
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog,
int which) {
// continue with delete
// no idea how to get this
// id.
String currentid = ((TextView) v
.findViewById(R.id.coltv))
.getText()
.toString();
Log.d("clickedid",
currentid);
DatabaseOperations dop = new DatabaseOperations(
ctx);
dop.deleteScript(dop,
currentid);
pwindo.dismiss();
Toast.makeText(
ctx,
"selected script was successfully deleted",
Toast.LENGTH_LONG)
.show();
}
})
.setNegativeButton(
android.R.string.no,
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog,
int which) {
// do nothing
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
// TODO Auto-generated method stub
}
});
...

3. DatabaseOperations.java


...
public void deleteScript(DatabaseOperations dop, String rowId) {
Log.d("db selected id?", rowId);
sql = dop.getWritableDatabase();
sql.execSQL("DELETE FROM  urlhistory where id='"+ rowId +"'");
}
...

No comments:

Post a Comment