Recycler View item click with custom dialog box
Recycler View item click with custom dialog box
To create a Recycler View, follow this post
Recycler view library in Gradle file
app-level build.Gradle
implementation 'com.android.support:recyclerview-v7:29.0.0'
Then we create a Recycler view in the XML file.
XML file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="2"
android:padding="10dp"
tools:context=".MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Roll No"
android:id="@+id/student_roll"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name"
android:id="@+id/student_name"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Std"
android:id="@+id/student_std"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add Student"
android:id="@+id/add_btn"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/student_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:scrollbars="vertical"/>
</LinearLayout>
</LinearLayout>
Then we create an adapter class that extends the recycler view.
Here, we use item click and custom dialog box, also we use update and delete query of firebase.
adapter class
package studio.harpreet.firebaseui;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import com.google.firebase.database.FirebaseDatabase;
import java.util.List;
public class Student_ListAdaptor extends RecyclerView.Adapter<Student_ListAdaptor.Student_Model_Holder>
{
Context mctx;
List<StudentModel> student_list;
public Student_ListAdaptor(Context mctx, List<StudentModel> student_list) {
this.mctx = mctx;
this.student_list = student_list;
}
@NonNull
@Override
public Student_Model_Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.student_custom_list,parent,false);
return new Student_Model_Holder(view);
}
@Override
public void onBindViewHolder(@NonNull Student_Model_Holder holder, int position) {
StudentModel studentModel = student_list.get(position);
final LinearLayout layout = new LinearLayout(mctx);
layout.setOrientation(LinearLayout.VERTICAL);
final EditText roll_et = new EditText(mctx);
roll_et.setHint("RollNo");
roll_et.setText(studentModel.rollno);
final EditText name_et = new EditText(mctx);
name_et.setHint("Name");
name_et.setText(studentModel.name);
final EditText std_et = new EditText(mctx);
std_et.setHint("Std");
std_et.setText(studentModel.std);
layout.addView(roll_et);
layout.addView(name_et);
layout.addView(std_et);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//If you face an error of Illegal state exception the specified child already has a parent
//you must call removeview() from child's parent first
// Then add these two lines only
//This error happens when you click on recycler view item and cancel
//and then again click on that item
ViewGroup parent = (ViewGroup) v.getParent();
parent.removeView(v);
//Now if you again click on any item of recycler view you will not face any problem
AlertDialog.Builder builder = new AlertDialog.Builder(mctx);
builder.setTitle("What to do")
.setMessage("Message")
.setView(layout)
.setPositiveButton("Update", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
FirebaseDatabase.getInstance().getReference("Student/3foBhgUVsWNdyS8ouc0HK1mgjy03")
.child(studentModel.getId()).child("name").setValue(name_et.getText().toString().trim());
}
}).setNegativeButton("Delete", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
FirebaseDatabase.getInstance().getReference("Student/3foBhgUVsWNdyS8ouc0HK1mgjy03")
.child(studentModel.getId()).removeValue();
}
}).setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
builder.setCancelable(true);
}
}).show();
}
});
holder.rolltextview.setText(String.format("Roll No: %s", studentModel.rollno));
holder.nametextview.setText(String.format("Name: %s", studentModel.name));
holder.stdtextview.setText(String.format("Std: %s", studentModel.std));
}
@Override
public int getItemCount() {
return student_list.size();
}
public class Student_Model_Holder extends RecyclerView.ViewHolder {
TextView rolltextview,nametextview,stdtextview;
public Student_Model_Holder(@NonNull View itemView) {
super(itemView);
rolltextview = itemView.findViewById(R.id.roll_textview);
nametextview = itemView.findViewById(R.id.name_textview);
stdtextview = itemView.findViewById(R.id.std_textview);
}
}
}
Subscribe to Harpreet studio on Youtube
Like Harpreet Studio on Facebook
Follow me on Instagram
No comments