I have a recycler view which is populated with a list if information.
When I click on an item, I want it to populate the fields on the activity. I'm unsure how to do this from the adapter.
Can someone help me please?
MainActivity.java
:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.btnadd);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), AddActivity.class);
startActivity(i);
}
});
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mAdapter = new MovieAdapter(getApplicationContext(), movieList);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(mAdapter);
prepareMovieData();
}
Adapter Class:
@Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
final Movies movie = moviesList.get(position);
holder.title.setText(movie.getTitle());
holder.genre.setText(movie.getGenre());
holder.year.setText(String.valueOf(movie.getYear()));
if (selected_position == position) {
holder.itemView.setBackgroundColor(Color.LTGRAY);
} else {
holder.itemView.setBackgroundColor(Color.WHITE);
}
holder.itemView.setOnClickListener(v -> {
//if (position == RecyclerView.NO_POSITION) return;
notifyItemChanged(selected_position);
selected_position = position;
notifyItemChanged(selected_position);
});
}
Views I want to fill in activity_main.xml
:
...
<LinearLayout
android:id="@+id/llname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/txtmov"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Movie" />
<EditText
android:id="@+id/etmov"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:id="@+id/llgenre"
android:layout_below="@+id/llname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:id="@+id/txtgnr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Genre" />
<EditText
android:id="@+id/etgnr"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:id="@+id/llyear"
android:layout_below="@+id/llgenre"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/txtyr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Year " />
<EditText
android:id="@+id/etyr"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number" />
</LinearLayout>
...
Screenshot:
I thought of different ways to this, one was that I create an adapter myself, and then construct it in the mainactivity, like using the moviesAdapter I made. I tried this but it didn't work then I thought to myself there must be an easier way to do it but I'm struggling to find anything.
Stack also wouldn't let me post cause it says there is too much code, so if you need anything else just give me a shout. I left the parts I thought are important.
Thank you in advance!