我有一个RecyclerView
和Sqlite
的应用程序,我使用DialogFragment
将数据插入到表中,当我点击RecyclerView
上的项目中的longclick时,我显示另一个Dialog
有一个删除按钮,但是我无法抓住我点击它的项目的ID,任何想法?
这是删除方法:
public void deleteARow(int idA)
{
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TODO_TABLE, id + "=" + idA, null);
db.close();
}
这是RecyclerView适配器:
public void onBindViewHolder(final RecyclerAdapter.ViewHolder holder, final int position) {
final ToDoModule appShowModule = dbList.get( position );
holder.Title.setText(dbList.get(position).getActionTitle());
holder.Desc.setText(dbList.get(position).getActionDesc());
holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
FragmentTransaction fragmentTransaction = ((AppCompatActivity)context).getSupportFragmentManager().beginTransaction();
actionOption actionOption = new actionOption();
actionOption.show(fragmentTransaction,"fragment_edit_name");
return true;
}
});}
这是删除按钮单击
public void onClick(View view) {
ToDoModule to = new ToDoModule();
db.deleteARow(to.getId());
Toast.makeText(getContext(), "delete", Toast.LENGTH_SHORT).show();
}
这是从db table
获取数据的方法 public List<ToDoModule> getDataFromDB(){
List<ToDoModule> modelList = new ArrayList<ToDoModule>();
String query = "select * from "+ TODO_TABLE;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query,null);
if (cursor.moveToFirst()){
do {
ToDoModule model = new ToDoModule();
String Title = cursor.getString(cursor.getColumnIndex(COLUMN_Title));
String Description = cursor.getString(cursor.getColumnIndex(COLUMN_Des));
int ID = (cursor.getColumnIndex(id));
model.setActionTitle(Title);
model.setActionDesc(Description);
model.setId(ID);
modelList.add(model);
}while (cursor.moveToNext());
}
return modelList;
}
答案 0 :(得分:0)
我正在使用这种方式来检测click和longClick事件:
recyclerView.addOnItemTouchListener(new RecyclerViewClickManager(context,
recyclerView, new RecyclerViewClickManager.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
//this is single click
//if you want to get viewholder here :
MyViewHolder viewHolder=(MyViewHolder) recyclerViewNotes.getChildViewHolder(view);
//also you have position so you can get your item id by position
}
@Override
public void onItemLongClick(View view, int position) {
//this is long click
}
}));
RecyclerViewClickManager类:
public class RecyclerViewClickManager implements RecyclerView.OnItemTouchListener {
//this is the click interface for both click and longClick
public interface OnItemClickListener {
void onItemClick(View view, int position);
void onItemLongClick(View view, int position);
}
private OnItemClickListener mListener;
private GestureDetector mGestureDetector;
private RecyclerView mRecyclerView;
public RecyclerViewClickManager(Context context, final RecyclerView recyclerView, OnItemClickListener listener) {
mListener = listener;
mRecyclerView=recyclerView;
mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
@Override
public void onLongPress(MotionEvent e) {
View childView = recyclerView.findChildViewUnder(e.getX(), e.getY());
//here if you want to get ViewHolder too :
YourViewHolder viewholder=(YourViewHolder) recyclerView.findContainingViewHolder(childView);
if (childView != null && mListener != null) {
mListener.onItemLongClick(childView, recyclerView.getChildAdapterPosition(childView));
}
}
});
}
@Override
public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
View childView = view.findChildViewUnder(e.getX(), e.getY());
if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
mListener.onItemClick(childView, view.getChildAdapterPosition(childView));
}
return false;
}
@Override
public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) {
}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
}
您也可以点这个链接:recycler-view-item-click-handler
此SO问题也适用于引用:recyclerview-onclick