0

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();

    }


Okid 62
  • 3
  • 2
  • [`getIntent()`](https://developer.android.com/reference/android/app/Activity#getIntent()) is an `Activity`s method, isn't available in adapter. if you want to pass this `Intent` to adapter then pass it in constructor – snachmsm Jan 25 '22 at 20:31
  • @snachmsm how do I do that? – Okid 62 Jan 25 '22 at 20:33
  • show how do you initiate adapter – snachmsm Jan 25 '22 at 20:34
  • I followed a tutorial, and it said to use this in the Main Activity: `recyclerAdapter adapter = new recyclerAdapter(subjectList); RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext()); rvSubject.setLayoutManager(layoutManager); rvSubject.setItemAnimator(new DefaultItemAnimator()); rvSubject.setAdapter(adapter);` – Okid 62 Jan 25 '22 at 20:37
  • follow answer by @Narendra_Nath, in short `new Intent(this, recyclerAdapter.class)` line doesn't have sense and won't distribute data straight to `recyclerAdapter`. besides that it isn't even used further, besides setting some data for it, yet another `Intent` is running by `startActivity`. read about `Intent`s, how to use them to pass data between **Context extending classes** only (adapters aren't extending `Context`) – snachmsm Jan 25 '22 at 20:40

1 Answers1

0

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

Narendra_Nath
  • 4,578
  • 3
  • 13
  • 31