无法从Adapter类检索数据

时间:2018-10-20 18:02:28

标签: android listview android-recyclerview android-arrayadapter recycler-adapter

我的目的是通过电子邮件通知用户。
尽管我的意图是可以很好地检索数据,但在发送给发件人的邮件中却说null

获取数据总是返回我null
请帮助我检索数据。

这是我的适配器类:

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布局。
我想通过适配器类中的电子邮件发送这些详细信息:

<?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

1 个答案:

答案 0 :(得分:0)

我解决了我的问题 如此简单,诀窍在于getAdapterPosition 我在itemView和notify按钮之间做了一些更改: 点击通知按钮后,它会通过意图向我发送数据

 @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);
        }