Reputation: 185
As you can see I have the dialog made and displaynotedate is used to read other data associated with the amount and is read in the dialog box:
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ListFragment;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.database.Cursor;
import android.util.Log;
import android.widget.AdapterView;
import android.widget.ListView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.widget.Toast;
import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import java.util.ArrayList;
public class tab3expense extends Fragment {
private static final String TAG = "tab3expense";
DatabaseHelper mDatabaseHelper;
private ListView mListView;
View rootView;
Cursor expensedata;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.tab3expense, container, false);
return rootView;
}
@Override
public void onActivityCreated( Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mListView = (ListView) rootView.findViewById(R.id.listViewexpense);
mDatabaseHelper = new DatabaseHelper(getActivity());
populateListView();
}
private void populateListView() {
Log.d(TAG, "populateListView: Displaying data in the ListView.");
expensedata = mDatabaseHelper.getexpenseData();
SimpleCursorAdapter sca = new SimpleCursorAdapter(getActivity(), android.R.layout.simple_list_item_1, expensedata, new String[]{DatabaseHelper.EXPENSE_AMOUNT}, new int[]{android.R.id.text1}, 0);
mListView.setAdapter(sca);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
int csrpos = expensedata.getPosition();
expensedata.moveToPosition(i);
displayNoteDate(
expensedata.getString(expensedata.getColumnIndex(DatabaseHelper.EXPENSE_NOTES)),
expensedata.getString(expensedata.getColumnIndex(DatabaseHelper.EXPENSE_DATE)));
expensedata.moveToPosition(csrpos);
}
});
}
@Override
public void onDestroy() {
super.onDestroy();
expensedata.close();
}
public void displayNoteDate(String noteContent, String dateValue) {
MaterialDialog.Builder builder= new MaterialDialog.Builder(getActivity())
.title("Expense Information")
.content("Note: "+noteContent+"\nDate: "+ dateValue)
.positiveText("edit")
.negativeText("delete")
.neutralText("close")
.onPositive(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
Toast.makeText(getActivity(),"EDIT",Toast.LENGTH_LONG).show();
}
})
.onNeutral(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
Toast.makeText(getActivity(),"CLOSE",Toast.LENGTH_LONG).show();
}
})
.onNegative(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
Toast.makeText(getActivity(),"DELETE",Toast.LENGTH_LONG).show();
}
});
builder.show();
}
}
This is my database helper:
package com.dharquissandas.budget;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.view.View;
import android.util.Log;
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String TAG = "DatabaseHelper";
public static final String DATABASE_NAME = "budget.db";
public static final String TABLE_NAME = "expense_table";
public static final String TABLE_NAME2 = "income_table";
public static final String COL_1 = "ID";
public static final String COL_2 = "ID";
public static final String EXPENSE_AMOUNT = "AMOUNT";
public static final String EXPENSE_DATE = "DATE";
public static final String EXPENSE_NOTES = "NOTES";
public static final String INCOME_AMOUNT = "AMOUNT";
public static final String INCOME_DATE = "DATE";
public static final String INCOME_NOTES = "NOTES";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 3);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, AMOUNT INTEGER,DATE INTEGER,NOTES TEXT)");
db.execSQL("create table " + TABLE_NAME2 + " (ID INTEGER PRIMARY KEY AUTOINCREMENT,AMOUNT INTEGER,DATE INTEGER,NOTES TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME2);
onCreate(db);}
public boolean insertexpenseData(String amount_expense, String date_expense, String notes_expense) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(EXPENSE_AMOUNT, amount_expense);
contentValues.put(EXPENSE_DATE, date_expense);
contentValues.put(EXPENSE_NOTES, notes_expense);
long result = db.insert(TABLE_NAME, null, contentValues);
if (result == -1)
return false;
else
return true;
}
public boolean insertincomeData(String amount_income, String date_income, String notes_income) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(INCOME_AMOUNT, amount_income);
contentValues.put(INCOME_DATE, date_income);
contentValues.put(INCOME_NOTES, notes_income);
long result = db.insert(TABLE_NAME2, null, contentValues);
if (result == -1)
return false;
else
return true;
}
public Cursor getexpenseItemID(String name){
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT " + COL_1 + " FROM " + TABLE_NAME + " WHERE " + EXPENSE_AMOUNT + " = '" + name + "'";
Cursor data = db.rawQuery(query, null);
return data;
}
public Cursor getincomeItemID(String name){
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT " + COL_2 + " FROM " + TABLE_NAME2 + " WHERE " + INCOME_AMOUNT + " = '" + name + "'";
Cursor data = db.rawQuery(query, null);
return data;
}
public Cursor getexpenseData() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("select * from " + TABLE_NAME, null);
return res;
}
public Cursor getincomeData() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("select * from " + TABLE_NAME2, null);
return res;
}
public boolean updateexpenseData(String id, String amount, String date, String notes) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_1, id);
contentValues.put(EXPENSE_AMOUNT, amount);
contentValues.put(EXPENSE_DATE, date);
contentValues.put(EXPENSE_NOTES, notes);
db.update(TABLE_NAME, contentValues, "_id = ?", new String[]{id});
return true;
}
public boolean updateincomeData(String id, String amount, String date, String notes) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_2, id);
contentValues.put(INCOME_AMOUNT, amount);
contentValues.put(INCOME_DATE, date);
contentValues.put(INCOME_NOTES, notes);
db.update(TABLE_NAME2, contentValues, "_id = ?", new String[]{id});
return true;
}
public Integer deleteexpenseData(String id) {
SQLiteDatabase db = this.getWritableDatabase();
return db.delete(TABLE_NAME, "ID = ?", new String[]{id});
}
public Integer deleteincomeData(String id) {
SQLiteDatabase db = this.getWritableDatabase();
return db.delete(TABLE_NAME2, "ID = ?", new String[]{id});
}
}
What code should I add to the delete button for it to delete the row in the database. I know that I have to somehow get the position of the listview and use the id to delete it, but I don't know how to. I would also like to remove the item from the list view and the database when the delete button is pressed. Can someone help me?
Upvotes: 1
Views: 81
Reputation: 57073
1) Change
public void displayNoteDate(String noteContent, String dateValue)
to
public void displayNoteDate(String noteContent, String dateValue, long noteId)
This will be used to accept the id of the note to be deleted.
2) Change
displayNoteDate(
expensedata.getString(expensedata.getColumnIndex(DatabaseHelper.EXPENSE_NOTES)),
expensedata.getString(expensedata.getColumnIndex(DatabaseHelper.EXPENSE_DATE)));
to
displayNoteDate(
expensedata.getString(expensedata.getColumnIndex(DatabaseHelper.EXPENSE_NOTES)),
expensedata.getString(expensedata.getColumnIndex(DatabaseHelper.EXPENSE_DATE)),
l);
This will now pass the id to the displayNoteDate
method. l is the id as obtained from the cursor by the adapter.
3) Change
.onNegative(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
Toast.makeText(getActivity(),"DELETE",Toast.LENGTH_LONG).show();
}
});
to
.onNegative(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
Toast.makeText(getActivity(),"DELETE",Toast.LENGTH_LONG).show();
deleteexpenseData(Long.toString(noteId));
expensedata = mDatabaseHelper.getexpenseData();
sca.swapCursor(expensedata);
}
});
This will use the deleteexepnseData
method to delete the respective row, rebuild the cursor and then tell the adpater to use the new cursor. (sca.onNotifyDatasetChanged();
could be used instead of sca.swapCursor(expensedata);
).
Note there appears to be some inconsistency with ID column naming or you have two columns both with the id. COL1 and COL2 appear to name it ID. A CursorAdpater
requires a column named _id. The two update methods use _id and ID. However the two delete methods use ID.
Note this is untested, so there may be some typos.
Upvotes: 1