Dialog with multiple buttons(cancalButton, deleteButton) is showed when click button in recyclerview item.
So I made two listener(cancelListener, deleteListener).
Show dialog is successful and cancelListener works well, But I can't do what I want in delteListener.
I wanna delete data in firebase when user click delete button in dialog.
So I have to know clicked recyclerview item's position to find data. (Because I can find data by getting key from array.get(position).getTimestamp.
How can I get item position in deleteListener?
public class AdapterExplorePlace extends RecyclerView.Adapter<AdapterExplorePlace.ViewHolder> implements Pin_BottomSheetDialog.BottomSheetListener {
ArrayList<ItemExplore> array;
Context context;
private AdapterExplorePlace.RecyclerViewClickListener listener;
private DialogDeletePin dialog;
DatabaseReference pinRef;
FirebaseAuth mAuth;
String currentPage;
@ColorInt int starColor;
@ColorInt int startextColor;
public AdapterExplorePlace(ArrayList<ItemExplore> array, Context context, AdapterExplorePlace.RecyclerViewClickListener listener) {
this.array = array;
this.context =context;
this.listener = listener;
}
@NonNull
@Override
public AdapterExplorePlace.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
SharedPreferences sharedPreferences = context.getSharedPreferences("file", MODE_PRIVATE);
starColor = sharedPreferences.getInt("starColor", 0);
startextColor = sharedPreferences.getInt("startextColor", 0);
currentPage = sharedPreferences.getString("currentPage", "");
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.item_explore_place,parent,false);
mAuth = FirebaseAuth.getInstance();
pinRef = FirebaseDatabase.getInstance().getReference("pinData").child(mAuth.getUid()).child(currentPage);
return new AdapterExplorePlace.ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull AdapterExplorePlace.ViewHolder holder, int position) {
if(array.get(position).getThumbnail() == null){
holder.explorePlaceImage.setImageResource(R.drawable.defalut_place_image);
}
else{
Glide.with(context)
.load(array.get(position).getThumbnail())
.centerCrop()
.dontAnimate()
.into(holder.explorePlaceImage);
}
holder.explorePlaceRegion.setText(String.valueOf(array.get(position).getPlaceAddress()));
holder.explorePlaceCategory.setText(String.valueOf(array.get(position).getPlaceCategory()));
holder.explorePlaceName.setText(String.valueOf(array.get(position).getPlaceName()));
holder.explorePlaceStyle.setText(String.valueOf(array.get(position).getPlaceStyle()));
holder.explorePlaceVisitedMember.setText(String.valueOf(array.get(position).getMember()));
holder.explorePlaceStyle.setTextColor(starColor);
holder.timestamp=String.valueOf(array.get(position).getTimestamp());
Drawable styleDrawable = ContextCompat.getDrawable(context,R.drawable.rounded);
styleDrawable.setColorFilter(new PorterDuffColorFilter(starColor, PorterDuff.Mode.SRC_IN));
holder.explorePlaceStyle.setBackground(styleDrawable);
pinRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for(DataSnapshot pinCheckSnap : snapshot.getChildren()){
Log.d("####Icon Check", String.valueOf(String.valueOf(pinCheckSnap.getValue()).contains(holder.timestamp)));
if(String.valueOf(pinCheckSnap.getValue()).contains(holder.timestamp)){
holder.btnExplorePlacePin.setBackgroundResource(R.drawable.ic_scrap_active);
holder.btnExplorePlacePin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openDialog(position);
}
});
break;
}
else {
holder.btnExplorePlacePin.setBackgroundResource(R.drawable.ic_scrap_inactive);
holder.btnExplorePlacePin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bundle args = new Bundle();
args.putString("timestamp", holder.timestamp);
Pin_BottomSheetDialog bottomSheet = new Pin_BottomSheetDialog();
bottomSheet .setArguments(args);
bottomSheet.show(((FragmentActivity)context).getSupportFragmentManager(), "pinBottomSheet");
}
});
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
@Override
public int getItemCount() {
return array.size();
}
@Override
public void onButtonClicked(String text) {
}
public interface RecyclerViewClickListener{
void onClick(View v, int position);
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
ImageView explorePlaceImage;
TextView explorePlaceRegion;
TextView explorePlaceCategory;
TextView explorePlaceName;
TextView explorePlaceStyle;
TextView explorePlaceVisitedMember;
String timestamp;
ImageButton btnExplorePlacePin;
AdapterView.OnItemClickListener onItemClickListener;
public ViewHolder(@NonNull View itemView) {
super(itemView);
explorePlaceImage =itemView.findViewById(R.id.explorePlaceImage);
explorePlaceRegion =itemView.findViewById(R.id.explorePlaceRegion);
explorePlaceCategory =itemView.findViewById(R.id.explorePlaceCategory);
explorePlaceName =itemView.findViewById(R.id.explorePlaceName);
explorePlaceStyle =itemView.findViewById(R.id.explorePlaceStyle);
explorePlaceVisitedMember =itemView.findViewById(R.id.explorePlaceVisitedMember);
btnExplorePlacePin =itemView.findViewById(R.id.btnExplorePlacePin);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
listener.onClick(itemView, getAdapterPosition());
}
void setItemClickListener(AdapterView.OnItemClickListener onItemClickListener) {
this.onItemClickListener = onItemClickListener;
}
}
private void openDialog(final int position){
dialog = new DialogDeletePin(context,cancelListener, deleteListener);
dialog.setCancelable(true);
dialog.getWindow().setGravity(Gravity.CENTER);
dialog.show();
}
View.OnClickListener cancelListener = new View.OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
}
};
View.OnClickListener deleteListener = new View.OnClickListener() {
public void onClick(View v) {
//I need to know item position for deleting data.
Log.d("####dialog","click delete");
dialog.dismiss();
}
};
}
DeleteDialogPin
class DialogDeletePin extends Dialog {
private Button btnCancelDeletePin;
private Button btnDeletePin;
private View.OnClickListener cancelListener;
private View.OnClickListener deleteListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 다이얼로그 외부 화면 흐리게 표현
WindowManager.LayoutParams lpWindow = new WindowManager.LayoutParams();
lpWindow.flags = WindowManager.LayoutParams.FLAG_DIM_BEHIND;
lpWindow.dimAmount = 0.6f;
getWindow().setAttributes(lpWindow);
setContentView(R.layout.dialog_delte_pin);
btnCancelDeletePin = findViewById(R.id.btnCancelDeletePin);
btnCancelDeletePin.setOnClickListener(cancelListener);
btnDeletePin = findViewById(R.id.btnDeletePin);
btnDeletePin.setOnClickListener(deleteListener);
//클릭이벤트
}
public DialogDeletePin(Context context, View.OnClickListener canceltListener, View.OnClickListener deleteListener) {
super(context, android.R.style.Theme_Translucent_NoTitleBar);
this.cancelListener = canceltListener;
this.deleteListener = deleteListener;
}
}
Since your method private void openDialog(final int position)
has the position information. One of the way you can do it is just by creating a class for deleteListemer (DeleteListenerWithItemPos) and then pass the position
as an argument at the constructor.
An example of how to implement is shown as follows:
private void openDialog(final int position){
deleteListener = new DeleteListenerWithItemPos(position);
dialog = new DialogDeletePin(context,cancelListener, deleteListener);
dialog.setCancelable(true);
dialog.getWindow().setGravity(Gravity.CENTER);
dialog.show();
}
View.OnClickListener deleteListener;
public static class DeleteListenerWithItemPos implements View.OnClickListener() {
private int itemPos;
public DeleteListenerWithItemPos(int itemPos){
this.itemPos = itemPos;
}
public void onClick(View v) {=
itemPos; //get itemPos here
Log.d("####dialog","click delete");
dialog.dismiss();
}
}