简体   繁体   中英

can't use Intent inside a RecyclerView Adapter

I am making an app in which you keep track of your grades. Currently, I am working on a system for adding subjects. So, I want to display the subjects on a RecyclerView. So, I have an activity for adding the subject, which gets the name of the subject, stores it in a arraylist, sends the arraylist to the RecyclerView Adapter and displays the Subject name as an item.

The issue I am facing is that, I can't seem to transfer the value of the arraylist with an Intent. I get an error in the Adapter, specifically when I try to use a command called getIntent() .

Here is the code in RecyclerAdapter.java file:


import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

public class recyclerAdapter extends RecyclerView.Adapter<recyclerAdapter.MyViewHolder> {

   private ArrayList<String> subjectList;

   public recyclerAdapter(ArrayList<String> subjectList){
       this.subjectList = subjectList;
   }

   public class MyViewHolder extends RecyclerView.ViewHolder{
       private TextView subjectName;

       public MyViewHolder(final View view){
           super(view);
           subjectName = view.findViewById(R.id.txtSubjectName);

           Intent addedSubject = getIntent(); //I get the error in this line, specifically getIntent()
           subjectList = addedSubject.getStringArrayListExtra("Subject");
       }
   }

   @NonNull
   @Override
   public recyclerAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
       View subjectView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_subjects, parent, false);
       return new MyViewHolder(subjectView);
   }

   @Override
   public void onBindViewHolder(@NonNull recyclerAdapter.MyViewHolder holder, int position) {
       String subject = subjectList.get(position);
       holder.subjectName.setText(subject);
   }

   @Override
   public int getItemCount() {
       return subjectList.size();
   }
}

Here's the code in the activity in which you add the subject name:


import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.Toast;

import java.util.ArrayList;

public class addSubject extends AppCompatActivity {

    private ArrayList<Subjects> subjectList;
    private boolean subjectAdded = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getSupportActionBar().hide();
        setContentView(R.layout.activity_add_subject);
        subjectList = new ArrayList<>();
    }

    public void addSubject (View view){
        EditText editSubjectName = findViewById(R.id.editSubjectName);
        Intent backToMain = new Intent(this, MainActivity.class);
        Intent toAdapter = new Intent(this, recyclerAdapter.class);
        String subjectName = editSubjectName.getText().toString();

        if (!subjectName.equals("")){
            subjectList.add(new Subjects(subjectName));
            toAdapter.putExtra("Subject", subjectList);
            startActivity(backToMain);
        }
        else
            Toast.makeText(getApplicationContext(),"Invalid values, try again",Toast.LENGTH_SHORT).show();

    }


Why not reinitialize the recyclerView with a new set of subjectList . `binding.rv.adapter=recyclerAdapter(//new subject list)

Since it is an Activity specific method it's not available inside the adapter.

However if your goal is to update data there are more elegant solutions to this like notifyDataSet() or using methods inside the adapter.

However if you have a complicated implementation this answer on Passing data from an activity back to recyclerview adapter might help.

Ps: Make your class names capital. it's a coding norm

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM