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