我有一个recyclerview,其中包含一个由用户插入的sqlite数据库中保存的项目列表,当按下recyclerview项目时,会出现一个对话框,打开时会显示删除按钮,我希望删除按钮删除该条目来自sqlite数据库,因此从recyclerview中删除。我已经使用删除按钮创建了对话框和recyclerview,但我不知道要在onClick中放入删除按钮的内容。
适配器
<ul>
{% for elem in your_list %}
<li>{{ elem }}</li>
{% endfor %}
</ul>
带有对话框的
的Java类public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private final ArrayList<String> dataSet;
private final ArrayList<String> dataSet2;
private final ArrayList<String> dataSet3;
private ItemClickListener clickListener;
String myListPreference;
Context ctx;
class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
final TextView pound;
final TextView mTextView;
final TextView textView;
final TextView categoryText;
final CardView cardView;
ViewHolder(View v) {
super(v);
SharedPreferences sharedPreferences = android.support.v7.preference.PreferenceManager.getDefaultSharedPreferences(ctx);
myListPreference = sharedPreferences.getString("CurrencyType", "£");
pound = (TextView) v.findViewById(R.id.pound);
mTextView = (TextView) v.findViewById(R.id.favourite_textView);
categoryText = (TextView) v.findViewById(R.id.category_text);
textView = (TextView) v.findViewById(R.id.verse_text);
cardView = (CardView) v.findViewById(R.id.cardview);
pound.setText(""+ myListPreference);
v.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (clickListener != null) clickListener.onClick(view, getAdapterPosition());
}
}
public MyAdapter(ArrayList<String> myDataset,ArrayList<String> myDataSet2,ArrayList<String> myDataset3, Context ctx) {
this.dataSet = myDataset;
this.dataSet2 = myDataSet2;
this.dataSet3 = myDataset3;
this.ctx = ctx;
}
@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.rvlayout, parent, false);
return new MyAdapter.ViewHolder(view);
}
@Override
public void onBindViewHolder(MyAdapter.ViewHolder holder, int position) {
holder.mTextView.setText(dataSet.get(position));
holder.textView.setText(dataSet2.get(position));
holder.categoryText.setText(dataSet3.get(position));
}
@Override
public int getItemCount() {
return dataSet.size();
}
public void setClickListener(ItemClickListener itemClickListener) {
this.clickListener = itemClickListener;
}
}
删除数据库助手中的方法
public class tab2income extends Fragment implements ItemClickListener {
private static final String TAG = "tab2income";
DatabaseHelper mDatabaseHelper;
private RecyclerView mListView;
View rootView;
Cursor incomedata;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.tab2income, container, false);
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mListView = rootView.findViewById(R.id.ListViewincome);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
mListView.setLayoutManager(layoutManager);
mDatabaseHelper = new DatabaseHelper(getActivity());
populateListView();
}
private void populateListView() {
Log.d(TAG, "populateListView: Displaying data in the ListView.");
ArrayList<String> arrayList = new ArrayList<>();
ArrayList<String> arrayList2 = new ArrayList<>();
ArrayList<String> arrayList3 = new ArrayList<>();
MyAdapter myAdapter = new MyAdapter(arrayList,arrayList2,arrayList3, getContext());
mListView.setAdapter(myAdapter);
myAdapter.setClickListener(this);
incomedata = mDatabaseHelper.getincomeData();
if(incomedata.moveToFirst()){
do {
arrayList.add(incomedata.getString(incomedata.getColumnIndex(DatabaseHelper.INCOME_AMOUNT)));
arrayList2.add(incomedata.getString(incomedata.getColumnIndex(DatabaseHelper.INCOME_DATE)));
arrayList3.add(incomedata.getString(incomedata.getColumnIndex(DatabaseHelper.INCOME_CATEGORY)));
}
while (incomedata.moveToNext());
}
myAdapter.notifyDataSetChanged();
}
@Override
public void onDestroy() {
super.onDestroy();
incomedata.close();
}
@Override
public void onClick(View view, int i) {
int csrpos = incomedata.getPosition();
incomedata.moveToPosition(i);
displayNoteDate(
incomedata.getString(incomedata.getColumnIndex(DatabaseHelper.INCOME_NOTES)),
incomedata.getString(incomedata.getColumnIndex(DatabaseHelper.INCOME_DATE)),
incomedata.getString(incomedata.getColumnIndex(DatabaseHelper.INCOME_CATEGORY)),
incomedata.getString(incomedata.getColumnIndex(DatabaseHelper.INCOME_ACCOUNT)));
incomedata.moveToPosition(csrpos);
}
public void displayNoteDate(String noteContent, String dateValue,String category,String account) {
MaterialDialog.Builder builder= new MaterialDialog.Builder(getActivity())
.title("Income Information")
.content("Date: "+ dateValue+
"\nCategory: "+category+
"\nAccount: "+account+
"\nNote: "+noteContent)
.positiveText("close")
.negativeText("delete")
.onPositive(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
}
})
.onNegative(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
}
});
builder.show();
}
}
答案 0 :(得分:1)
在displayNoteDate()
中进行以下更改private void displayNoteDate(int position,String noteContent, String dateValue,String category,String account) {
MaterialDialog.Builder builder= new MaterialDialog.Builder(getActivity())
.title("Income Information")
.content("Date: "+ dateValue+
"\nCategory: "+category+
"\nAccount: "+account+
"\nNote: "+noteContent)
.positiveText("close")
.negativeText("delete")
.onPositive(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
}
})
.onNegative(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
long id = mDatabaseHelper.getId(position); // get item id from database
mDatabaseHelper.deleteItem(id); // delete item
populateListView(); // refresh list items
}
});
builder.show();
}
在recyclerview内部点击
@Override
public void onClick(View view, int i) {
int csrpos = incomedata.getPosition();
incomedata.moveToPosition(i);
displayNoteDate(i,
incomedata.getString(incomedata.getColumnIndex(DatabaseHelper.INCOME_NOTES)),
incomedata.getString(incomedata.getColumnIndex(DatabaseHelper.INCOME_DATE)),
incomedata.getString(incomedata.getColumnIndex(DatabaseHelper.INCOME_CATEGORY)),
incomedata.getString(incomedata.getColumnIndex(DatabaseHelper.INCOME_ACCOUNT)));
incomedata.moveToPosition(csrpos);
}
在数据库类
中添加此方法public long getId(int position) {
String query = "SELECT _id" + " FROM " + TABLE_NAME;
Cursor cursor = getDatabase().rawQuery(query, null);
cursor.moveToPosition(position);
long id = cursor.getLong(cursor.getColumnIndex("_id"));
cursor.close();
return id;
}
public void delete(long id) {
getWritableDatabase.delete(TABLE_NAME, "_id = " + id, null);
}