正如您所看到的,我已经创建了对话框,并且displaynotedate用于读取与金额相关的其他数据,并在对话框中读取:
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();
}
}
这是我的数据库助手:
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});
}
}
我应该在删除按钮中添加什么代码才能删除数据库中的行。我知道我必须以某种方式获取listview的位置并使用id删除它,但我不知道如何。我还想在按下删除按钮时从列表视图和数据库中删除该项目。有人能帮助我吗?
答案 0 :(得分:1)
1)改变
public void displayNoteDate(String noteContent, String dateValue)
到
public void displayNoteDate(String noteContent, String dateValue, long noteId)
这将用于接受要删除的备注的ID。
2)改变
displayNoteDate(
expensedata.getString(expensedata.getColumnIndex(DatabaseHelper.EXPENSE_NOTES)),
expensedata.getString(expensedata.getColumnIndex(DatabaseHelper.EXPENSE_DATE)));
到
displayNoteDate(
expensedata.getString(expensedata.getColumnIndex(DatabaseHelper.EXPENSE_NOTES)),
expensedata.getString(expensedata.getColumnIndex(DatabaseHelper.EXPENSE_DATE)),
l);
现在将ID传递给displayNoteDate
方法。 l是适配器从光标获得的id。
3)改变
.onNegative(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
Toast.makeText(getActivity(),"DELETE",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();
deleteexpenseData(Long.toString(noteId));
expensedata = mDatabaseHelper.getexpenseData();
sca.swapCursor(expensedata);
}
});
这将使用deleteexepnseData
方法删除相应的行,重建光标,然后告诉adpater使用新光标。 (sca.onNotifyDatasetChanged();
可以代替sca.swapCursor(expensedata);
)。
请注意,似乎与ID列命名存在一些不一致,或者您有两列都带有ID。 COL1和COL2似乎将其命名为ID。 CursorAdpater
需要名为 _id 的列。这两种更新方法使用 _id 和 ID 。但是,这两种删除方法都使用 ID 。
请注意,这是未经测试的,因此可能存在一些拼写错误。