无法从适配器 class 检索数据
Can't retrieve the data from Adapter class
我的意图是通过电子邮件通知用户。
虽然我的意图是检索数据工作正常,但它在邮件中说我 null
发件人
一直在获取数据returns我null
。
请帮我找回数据。
这是我的适配器 class:
public class adminDetailsAdapter extends RecyclerView.Adapter<adminDetailsAdapter.adminViewHolder>{
private Context context;
private List<storeStudentDetails> studentDetailsList;
private Button notifyBtn;
private String Name;
private String RollNumber;
private String Dept;
private String bookName;
private String FineAmt;
private String EmailId;
public adminDetailsAdapter(Context context, List<storeStudentDetails> studentDetailsList) {
this.context = context;
this.studentDetailsList = studentDetailsList;
}
@NonNull
@Override
public adminViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new adminViewHolder(
LayoutInflater.from(context).inflate(R.layout.admin_view_details, parent, false)
);
}
@Override
public void onBindViewHolder(@NonNull adminViewHolder holder, int position) {
storeStudentDetails mStoreDetails = studentDetailsList.get(position);
Name = mStoreDetails.getStudentName();
RollNumber = mStoreDetails.getRollNo();
Dept = mStoreDetails.getDept();
bookName = mStoreDetails.getBook();
FineAmt = String.valueOf(mStoreDetails.getFine());
EmailId = mStoreDetails.getEmail();
holder.studName.setText(mStoreDetails.getStudentName());
holder.email.setText( mStoreDetails.getEmail());
holder.rollNum.setText(mStoreDetails.getRollNo());
holder.bookName.setText( mStoreDetails.getBook());
holder.fine.setText("Fine:" + mStoreDetails.getFine());
holder.dept.setText(mStoreDetails.getDept());
}
String message = generate(Name,RollNumber,Dept,bookName,FineAmt,EmailId);
private String generate(String name ,String rollNumber , String department , String book , String Fine , String email){
name = Name ;
rollNumber = RollNumber;
department = Dept;
book = bookName;
Fine = FineAmt ;
email = EmailId;
String Generatemessage = name + rollNumber + "\n"+
department + "\n" + book + "\n" +
Fine + "\n" + email + '\n' ;
return Generatemessage;
}
@Override
public int getItemCount() {
return studentDetailsList.size();
}
class adminViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
TextView studName,rollNum,bookName,dept,fine,email;
public adminViewHolder(View itemView) {
super(itemView);
studName=itemView.findViewById(R.id.studentName_prof);
rollNum = itemView.findViewById(R.id.rollNumber_prof);
email = itemView.findViewById(R.id.email_prof);
bookName = itemView.findViewById(R.id.bookName_prof);
fine = itemView.findViewById(R.id.fineAmt_prof);
dept = itemView.findViewById(R.id.department_prof);
notifyBtn = itemView.findViewById(R.id.notify);
itemView.setOnClickListener(this);
notifyBtn.setOnClickListener(this);
}
@Override
public void onClick(View view) {
storeStudentDetails sd = studentDetailsList.get(getAdapterPosition());
Intent intent = new Intent(context,update_details_activity.class);
intent.putExtra("details",sd);
context.startActivity(intent);
switch (view.getId()){
//why switch in future it might be useful
case R.id.notify:
notifyStudent();
break;
}
}
private void notifyStudent(){
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:"));
intent.putExtra(Intent.EXTRA_TEXT,message);
PackageManager pm = context.getPackageManager();
if (intent.resolveActivity(pm) != null) {
context.startActivity(intent);
}
}
}
}
这是我用来显示数据的 XML 布局。
我想在适配器 class:
中通过电子邮件发送这些详细信息
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/studentName_prof"
android:text="Student name :"
android:textSize="15sp"
android:textStyle="bold"
android:textColor="@color/black"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/email_prof"
android:text="Email Id :"
android:textSize="15sp"
android:textColor="@color/black"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/rollNumber_prof"
android:text="rollNumber :"
android:textSize="15sp"
android:textColor="@color/black"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/department_prof"
android:text="Department :"
android:textSize="15sp"
android:textColor="@color/black"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/bookName_prof"
android:text="Borrowed Book :"
android:textSize="15sp"
android:textColor="@color/black"/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/fineAmt_prof"
android:text="fine Amount:"
android:textSize="15sp"
android:textColor="@color/black"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="NOTIFY"
android:id="@+id/notify"
android:background="@drawable/gradient_button"/>
</RelativeLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
请注意,作为参考,我的 github repo is。
我解决了我的问题
如此简单,诀窍在于 getAdapterPosition
我在 itemView 和通知按钮之间做了一些更改:
单击通知按钮后,它会通过 intent
向我发送数据
@Override
public void onClick(View view) {
storeStudentDetails sd = studentDetailsList.get(getAdapterPosition());
switch (view.getId()){
//why switch in future it might be useful
case R.id.notify:
String name = sd.getStudentName();
String rollNumber = sd.getRollNo();
String department = sd.getDept();
String book = sd.getBook();
String Fine = String.valueOf(sd.getFine());
String email = sd.getEmail();
String Generatemessage = "Name : " + name +"\n" +
"Roll Number : " + rollNumber + "\n" +
"Department : "+ department + "\n" +
"Book : "+ book + "\n" +
"Fine : "+ Fine + "\n" ;
Intent mailIntent = new Intent(Intent.ACTION_SENDTO);
mailIntent.setData(Uri.parse("mailto:" + sd.getEmail()));
mailIntent.putExtra(Intent.EXTRA_SUBJECT,"Pay immediately - " + sd.getFine());
mailIntent.putExtra(Intent.EXTRA_TEXT,Generatemessage);
PackageManager pm = context.getPackageManager();
if (mailIntent.resolveActivity(pm) != null) {
context.startActivity(mailIntent);
}
break;
default:
Intent intent = new Intent(context,update_details_activity.class);
intent.putExtra("details",sd);
context.startActivity(intent);
}
我的意图是通过电子邮件通知用户。
虽然我的意图是检索数据工作正常,但它在邮件中说我 null
发件人
一直在获取数据returns我null
。
请帮我找回数据。
这是我的适配器 class:
public class adminDetailsAdapter extends RecyclerView.Adapter<adminDetailsAdapter.adminViewHolder>{
private Context context;
private List<storeStudentDetails> studentDetailsList;
private Button notifyBtn;
private String Name;
private String RollNumber;
private String Dept;
private String bookName;
private String FineAmt;
private String EmailId;
public adminDetailsAdapter(Context context, List<storeStudentDetails> studentDetailsList) {
this.context = context;
this.studentDetailsList = studentDetailsList;
}
@NonNull
@Override
public adminViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new adminViewHolder(
LayoutInflater.from(context).inflate(R.layout.admin_view_details, parent, false)
);
}
@Override
public void onBindViewHolder(@NonNull adminViewHolder holder, int position) {
storeStudentDetails mStoreDetails = studentDetailsList.get(position);
Name = mStoreDetails.getStudentName();
RollNumber = mStoreDetails.getRollNo();
Dept = mStoreDetails.getDept();
bookName = mStoreDetails.getBook();
FineAmt = String.valueOf(mStoreDetails.getFine());
EmailId = mStoreDetails.getEmail();
holder.studName.setText(mStoreDetails.getStudentName());
holder.email.setText( mStoreDetails.getEmail());
holder.rollNum.setText(mStoreDetails.getRollNo());
holder.bookName.setText( mStoreDetails.getBook());
holder.fine.setText("Fine:" + mStoreDetails.getFine());
holder.dept.setText(mStoreDetails.getDept());
}
String message = generate(Name,RollNumber,Dept,bookName,FineAmt,EmailId);
private String generate(String name ,String rollNumber , String department , String book , String Fine , String email){
name = Name ;
rollNumber = RollNumber;
department = Dept;
book = bookName;
Fine = FineAmt ;
email = EmailId;
String Generatemessage = name + rollNumber + "\n"+
department + "\n" + book + "\n" +
Fine + "\n" + email + '\n' ;
return Generatemessage;
}
@Override
public int getItemCount() {
return studentDetailsList.size();
}
class adminViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
TextView studName,rollNum,bookName,dept,fine,email;
public adminViewHolder(View itemView) {
super(itemView);
studName=itemView.findViewById(R.id.studentName_prof);
rollNum = itemView.findViewById(R.id.rollNumber_prof);
email = itemView.findViewById(R.id.email_prof);
bookName = itemView.findViewById(R.id.bookName_prof);
fine = itemView.findViewById(R.id.fineAmt_prof);
dept = itemView.findViewById(R.id.department_prof);
notifyBtn = itemView.findViewById(R.id.notify);
itemView.setOnClickListener(this);
notifyBtn.setOnClickListener(this);
}
@Override
public void onClick(View view) {
storeStudentDetails sd = studentDetailsList.get(getAdapterPosition());
Intent intent = new Intent(context,update_details_activity.class);
intent.putExtra("details",sd);
context.startActivity(intent);
switch (view.getId()){
//why switch in future it might be useful
case R.id.notify:
notifyStudent();
break;
}
}
private void notifyStudent(){
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:"));
intent.putExtra(Intent.EXTRA_TEXT,message);
PackageManager pm = context.getPackageManager();
if (intent.resolveActivity(pm) != null) {
context.startActivity(intent);
}
}
}
}
这是我用来显示数据的 XML 布局。
我想在适配器 class:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/studentName_prof"
android:text="Student name :"
android:textSize="15sp"
android:textStyle="bold"
android:textColor="@color/black"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/email_prof"
android:text="Email Id :"
android:textSize="15sp"
android:textColor="@color/black"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/rollNumber_prof"
android:text="rollNumber :"
android:textSize="15sp"
android:textColor="@color/black"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/department_prof"
android:text="Department :"
android:textSize="15sp"
android:textColor="@color/black"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/bookName_prof"
android:text="Borrowed Book :"
android:textSize="15sp"
android:textColor="@color/black"/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/fineAmt_prof"
android:text="fine Amount:"
android:textSize="15sp"
android:textColor="@color/black"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="NOTIFY"
android:id="@+id/notify"
android:background="@drawable/gradient_button"/>
</RelativeLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
请注意,作为参考,我的 github repo is。
我解决了我的问题 如此简单,诀窍在于 getAdapterPosition 我在 itemView 和通知按钮之间做了一些更改: 单击通知按钮后,它会通过 intent
向我发送数据 @Override
public void onClick(View view) {
storeStudentDetails sd = studentDetailsList.get(getAdapterPosition());
switch (view.getId()){
//why switch in future it might be useful
case R.id.notify:
String name = sd.getStudentName();
String rollNumber = sd.getRollNo();
String department = sd.getDept();
String book = sd.getBook();
String Fine = String.valueOf(sd.getFine());
String email = sd.getEmail();
String Generatemessage = "Name : " + name +"\n" +
"Roll Number : " + rollNumber + "\n" +
"Department : "+ department + "\n" +
"Book : "+ book + "\n" +
"Fine : "+ Fine + "\n" ;
Intent mailIntent = new Intent(Intent.ACTION_SENDTO);
mailIntent.setData(Uri.parse("mailto:" + sd.getEmail()));
mailIntent.putExtra(Intent.EXTRA_SUBJECT,"Pay immediately - " + sd.getFine());
mailIntent.putExtra(Intent.EXTRA_TEXT,Generatemessage);
PackageManager pm = context.getPackageManager();
if (mailIntent.resolveActivity(pm) != null) {
context.startActivity(mailIntent);
}
break;
default:
Intent intent = new Intent(context,update_details_activity.class);
intent.putExtra("details",sd);
context.startActivity(intent);
}