当前位置: 首页 > 知识库问答 >
问题:

当按下dialog delete按钮时,如何从数据库中删除listview项?

邹野
2023-03-14

如您所见,我制作了对话框,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});
}


}

共有1个答案

闾丘淇
2023-03-14

1)变化

public void displayNoteDate(String noteContent, String dateValue) 

public void displayNoteDate(String noteContent, String dateValue, long noteId)

这将用于接受要删除的便笺的id。

        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);
            .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);

                }
            });

注意:与ID列命名似乎有些不一致,或者您有两列都使用ID。COL1和COL2似乎将其命名为id。cursoradpater需要名为_id的列。两个更新方法使用_id和id。但是,这两个delete方法使用ID。

注这是未经测试的,所以可能会有一些错别字。

 类似资料:
  • 我需要从ListView中永久删除一个项目,然后从数据库中删除。我有一个DatabaseHandler.java类,它的delete函数为: 注意,函数deleteContact具有contact类型的contact变量。

  • 我正在用C#WPF做一个项目,它包含在一个客户机管理中,在那里我添加了新的客户机,在一个txt文档中有save,我有一个listview,在那里我可以看到所有添加的客户机,但现在我想实现一个delete按钮,在那里我从listview中选择一个客户机,并可以删除它。如果我现在做的正确,如果有人能帮我…,…谢谢。

  • 我有一个嵌套的列表框(主对象列表中的对象集合),它需要删除基础项。 删除项时,我可以重置主列表的itemssource,但主列表将有大量项,每次从其基础集合中移除项时,主滚动条也将重置,这使得用户愿意以非常痛苦的方式杀死我。 我的问题:如何找到项的容器,单击了哪个按钮,如何找到项本身,以便杀死@&!$*&#$@(诅咒拟声词)? 以下是列表的XAML示例: 我的应该怎么样?

  • 我有一个带有searchView图标的操作栏。我点击searchView图标,出现softInputMode键盘,我的ListView出现用于搜索。但是,当您关闭searchView时,searchView会关闭,但我无法让ListView在searchView关闭时也关闭。 下面是我在activity_maps中的ListView代码。xml 地图ctivity.java 所以最初当MapsAc

  • 如何使用按钮onClick从ListView中删除所有内容?当我尝试“fullCourseList.clear();”时,我无法添加更多课程,只有再次访问页面后,页面才会刷新

  • 我无法弄清楚每三个条目的like按钮是如何以及为什么被按下的,从我实际按下的like按钮开始,但(幸运的是)只有一个,而且也调用了正确的API。 请帮忙。