Reputation: 45
I've been struggling with this issue for a while. I have an activity (SerieActivity
) where I allow the user to select various items and give them values. The items are stored in an ArrayList<Variable>
, where Variable
is a class that I've defined. The items are displayed in a RecyclerView
. This works just fine. Upon clicking a button in that activity, the user is sent to another activity (Result
) that displays the exact same items from the ArrayList
in SerieActivity
, with the respective values that the user has input.
In my Result
activity I have created an instance of the SerieActivity
class and then made an ArrayList
to store the values of the original ArrayList
from SerieActivity
. However it seems like the RecyclerView from my Result
class doesn't get populated with data. What could be going wrong?
Here's my code:
Variable
class
public class Variable {
boolean known;
String unit, name;
double value;
}
RVAdapterSerie
- the adapter for the RecyclerView used in the SerieActivity
class
public class RVAdapterSerie extends RecyclerView.Adapter<RVAdapterSerie.ViewHolder> {
private Context context;
private ArrayList<Variable> variableList;
public RVAdapterSerie(Context context, ArrayList<Variable> variableList) {
this.context = context;
this.variableList = variableList;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.recycler_item, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(RVAdapterSerie.ViewHolder holder, int position) {
Variable v = variableList.get(position);
holder.setItems(v);
}
@Override
public int getItemCount() {
return variableList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private CheckBox checkBox;
private EditText editText;
private TextView textView;
public ViewHolder(View itemView) {
super(itemView);
checkBox = itemView.findViewById(R.id.value_cb);
editText = itemView.findViewById(R.id.value_et);
textView = itemView.findViewById(R.id.value_tv);
}
public void setItems (Variable variable) {
checkBox.setText(variable.getName());
editText.setText(String.format("%.3f", variable.getValue()));
textView.setText(variable.getUnit());
}
}
}
SerieActivity
class
public class SerieActivity extends AppCompatActivity {
public ArrayList<Variable> mList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_serie);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
RecyclerView recyclerView = findViewById(R.id.serie_rv);
RVAdapterSerie rvAdapterSerie = new RVAdapterSerie(this, mList);
recyclerView.setAdapter(rvAdapterSerie);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
populateList();
rvAdapterSerie.notifyDataSetChanged();
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.serie_fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(SerieActivity.this, Result.class);
intent.putExtra("tag", "serie");
startActivity(intent);
}
});
}
public void populateList() {
mList.add(new Variable(false, "dummyunit", "dummyname", 0));
mList.add(new Variable(false, "diofishafuio", "ghfuef", 0));
mList.add(new Variable(false, "diofishdfesffafuio", "ghfuef", 0));
//add dummy items
}
}
RVResultAdapter
- adapter for the RecyclerView in Result
class
public class RVResultAdapter extends RecyclerView.Adapter<RVResultAdapter.ViewHolder> {
private Context context;
private ArrayList<Variable> variableList;
public RVResultAdapter(Context context, ArrayList<Variable> variableList) {
this.context = context;
this.variableList = variableList;
}
@Override
public RVResultAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.result_item, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(RVResultAdapter.ViewHolder holder, int position) {
Variable v = variableList.get(position);
holder.setItems(v);
}
@Override
public int getItemCount() {
return variableList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView textView1;
TextView textView2;
TextView textView3;
public ViewHolder(View itemView) {
super(itemView);
textView1 = itemView.findViewById(R.id.result_name_tv);
textView2 = itemView.findViewById(R.id.result_value_tv);
textView3 = itemView.findViewById(R.id.result_unit_tv);
}
public void setItems (Variable variable) {
textView1.setText(variable.getName());
textView2.setText(String.format("%.3f", variable.getValue()));
textView3.setText(variable.getUnit());
}
}
}
Result
class
public class Result extends AppCompatActivity {
public ArrayList<Variable> rList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
Intent intent = getIntent();
String tag = intent.getExtras().getString("tag");
if (tag.equals("serie")) {
SerieActivity serieActivity = new SerieActivity();
rList = serieActivity.mList;
}
RecyclerView recyclerView = findViewById(R.id.result_rv);
RVResultAdapter rvResultAdapter = new RVResultAdapter(this, rList);
recyclerView.setAdapter(rvResultAdapter);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
rvResultAdapter.notifyDataSetChanged();
}
}
My problem is with the RecyclerView in Result
. It shows the blue "end of content" things when I scroll up and down, so I guess the RecyclerView is there, but doesn't get any data. Any help is greatly appreciated. I am quite new to Android and Java programming, so I guess it must be a dumb thing that I just don't see.
Upvotes: 0
Views: 72
Reputation: 27
just set the recyclerview code like this first initialize adapter then set layout manager and then set to adapter to recyclerview.
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mAdapter = new MoviesAdapter(movieList);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(mAdapter);
prepareMovieData();
Upvotes: 0
Reputation: 542
So you calling SerieActivity
constructor in Results
activity and execute populateList()
inside onCreate()
. You shouldn't create activities in this way because onCreate()
will be never called. Instead of this you should pass data using putExtra
and use Parcelable
interface.
Upvotes: 0
Reputation: 59004
What are you doing dude. How can you get list from a new object of SerieActivity.class
?
if (tag.equals("serie")) { SerieActivity serieActivity = new SerieActivity(); rList = serieActivity.mList; }
This is totally wrong way. If you want to pass list between activities. You can use parcelable methods putExtra
and getExtra
.
Assuming that your List is a list of strings make data an ArrayList<String>
and use intent.putStringArrayListExtra("data", data)
Here is a skeleton of the code you need:
First you need to create a Parcelable object class, see the example
public class Student implements Parcelable {
int id;
String name;
public Student(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel dest, int arg1) {
// TODO Auto-generated method stub
dest.writeInt(id);
dest.writeString(name);
}
public Student(Parcel in) {
id = in.readInt();
name = in.readString();
}
public static final Parcelable.Creator<Student> CREATOR = new Parcelable.Creator<Student>() {
public Student createFromParcel(Parcel in) {
return new Student(in);
}
public Student[] newArray(int size) {
return new Student[size];
}
};
}
ArrayList<Student> arraylist = new ArrayList<Student>();
Intent intent = new Intent(this, SecondActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("mylist", arraylist);
intent.putExtras(bundle);
this.startActivity(intent);
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Bundle bundle = getIntent().getExtras();
ArrayList<Student> arraylist = bundle.getParcelableArrayList("mylist");
}
Upvotes: 1