Currently I am working with my recycler adapter and i have opened Edit dialog box on list item and its getting edited successfully changes were saved , but i want to reflect the changes instantly in list and navigate back to viewContact fragment i've tried dissmiss listner but it didnt working. note: I have only xml file for dialogBox there is no Other class that inflates layou or do something for dialogbox all code from building dialogbox and operating on it is present in this file itself.
recyclerAdapter File
package com.devesh.jetpacknavigationcomponent;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.RecyclerView;
import com.devesh.jetpacknavigationcomponent.database.DBHelper;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.HashMap;
public class StudentAdapter extends RecyclerView.Adapter<StudentAdapter.RowViewHolder> {
Context context;
ArrayList<HashMap<String, String>> studentList;
String up_rollno,up_name,up_chemistry,up_physics,up_math;
EditText edit_name,edit_rollno,edit_chemistry,edit_physics,edit_maths;
Button cancel_btn,saveEdit_btn;
public StudentAdapter(Context context, ArrayList<HashMap<String, String>> studentList)
{
this.studentList=studentList;
this.context=context;
}
@Override
public StudentAdapter.RowViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.row,parent,false);
StudentAdapter.RowViewHolder viewHolder=new StudentAdapter.RowViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(StudentAdapter.RowViewHolder holder, @SuppressLint("RecyclerView") int position) {
holder.srNo.setText(studentList.get(position).get("ID"));
holder.name.setText(studentList.get(position).get("NAME"));
holder.roll_no.setText(studentList.get(position).get("ROLLNO"));
holder.total_marks.setText(studentList.get(position).get("TOTAL_MARKS"));
holder.percentage.setText(studentList.get(position).get("PERCENTAGE"));
holder.grade.setText(studentList.get(position).get("GRADE"));
holder.delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DBHelper db = new DBHelper(v.getContext());
db.deleteStudent(studentList.get(position).get("ID"));
Navigation.findNavController(v).navigate(R.id.viewContact);
Toast.makeText(v.getContext(), "Student Deleted " + studentList.get(position).get("ID"),Toast.LENGTH_SHORT).show();
}
});
holder.update.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Name Clicked " + studentList.get(position).get("NAME"), Toast.LENGTH_SHORT).show();
AlertDialog.Builder builder = new AlertDialog.Builder(v.getRootView().getContext());
View dialogView = LayoutInflater.from(v.getRootView().getContext()).inflate(R.layout.edit_row, null);
edit_rollno = dialogView.findViewById(R.id.rollno_edtrow);
edit_chemistry = dialogView.findViewById(R.id.chemistry_edtrow);
edit_physics = dialogView.findViewById(R.id.physics_edtrow);
edit_maths = dialogView.findViewById(R.id.math_edtrow);
edit_name = dialogView.findViewById(R.id.name_edtrow);
edit_rollno.setText(studentList.get(position).get("ROLLNO"));
edit_name.setText(studentList.get(position).get("NAME"));
edit_chemistry.setText(studentList.get(position).get("CHEMISTRY"));
edit_physics.setText(studentList.get(position).get("PHYSICS"));
edit_maths.setText(studentList.get(position).get("MATH"));
cancel_btn = dialogView.findViewById(R.id.edit_cancel_btn);
saveEdit_btn = dialogView.findViewById(R.id.saveedit_btn);
cancel_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//TODO: Navigate back to viewContact Without Making Changes
builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
dialog.dismiss();
}
});
}
});
saveEdit_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
up_name = edit_name.getText().toString();
up_rollno = edit_rollno.getText().toString();
up_chemistry = edit_chemistry.getText().toString();
up_physics = edit_physics.getText().toString();
up_math = edit_maths.getText().toString();
int id = Integer.parseInt(studentList.get(position).get("ID"));
int rollNo = Integer.parseInt(up_rollno);
double Chem = Double.parseDouble(up_chemistry);
double Phy = Double.parseDouble(up_physics);
double Math = Double.parseDouble(up_math);
double totalMarks = Chem + Phy + Math;
double percentage = totalMarks * 100 / 300;
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(2);
String percent = nf.format(percentage);
String grade = "";
if (percentage > 50 && percentage < 60) {
grade = "C+";
} else if (percentage > 60 && percentage < 80) {
grade = "B+";
} else if (percentage > 80 && percentage < 100) {
grade = "A+";
}
DBHelper db = new DBHelper(v.getContext());
db.updateStudent(id, rollNo, up_name, Chem, Phy, Math, totalMarks, Double.parseDouble(percent), grade);
// TODO : Here i want to Refresh database and navigate Back To ViewContact Fragment
}
});
builder.setView(dialogView);
builder.setCancelable(true);
builder.show();
}
});
}
@Override
public int getItemCount() {
return studentList.size();
}
public class RowViewHolder extends RecyclerView.ViewHolder {
TextView srNo, name, roll_no, total_marks, percentage, grade;
Button delete, update;
public RowViewHolder(@NonNull View itemView) {
super(itemView);
this.srNo = itemView.findViewById(R.id.srno_tv);
this.name = itemView.findViewById(R.id.name_tv);
this.roll_no = itemView.findViewById(R.id.rollno_tv);
this.total_marks = itemView.findViewById(R.id.totalMarks_tv);
this.percentage = itemView.findViewById(R.id.percentage_tv);
this.grade = itemView.findViewById(R.id.grade_tv);
this.delete = itemView.findViewById(R.id.delete_row_btn);
this.update = itemView.findViewById(R.id.update_row_btn);
}
}
}
// void reftresh() {
// Intent i = new Intent(context.getApplicationContext(), StudentAdapter.class);
// i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// context.getApplicationContext().startActivity(i);
// }