0

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:

want

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!

olfek
  • 3,210
  • 4
  • 33
  • 49
Erbez
  • 321
  • 5
  • 17

3 Answers3

1

In adapter class:

@Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
    final Movies movie = moviesList.get(position);

    ...

    holder.itemView.setTag(movie)

    holder.itemView.setOnClickListener(v -> {

        ...

        MainActivity.updateView((Movies)v.getTag())
    });
}

In MainActivity.java

public static void updateView(Movies movies){

    // Update view by using `findViewById`
}

EDIT

This example should help you solve

error: non-static method findViewById(int) cannot be referenced from a static context

MainActivity.java:

public class MainActivity extends AppCompatActivity {

    private static LinearLayout linearLayout = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        linearLayout = findViewById(R.id.root_view);
        updateView("Hello World! 2");
    }

    public static void updateView(String text){
        if(linearLayout==null) return;
        ((TextView) linearLayout.findViewById(R.id.tv)).setText(text);
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/root_view"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</LinearLayout>
olfek
  • 3,210
  • 4
  • 33
  • 49
  • I cant set anything, it keeps giving me a static error for example: `EditText et1 = (EditText) findViewById(R.id.etmov);` gives me an error like this: `error: non-static method findViewById(int) cannot be referenced from a static context` – Erbez Sep 09 '18 at 11:37
  • Store the views in a variable thats available for the whole class. – olfek Sep 09 '18 at 11:40
  • Either store the context in a variable, or store the parent view. You can then do `view.findViewById` or `context.findViewById` – olfek Sep 09 '18 at 11:40
  • @Erbez Give me a few minutes, I'll provide an example. – olfek Sep 09 '18 at 11:41
  • I will wait, Thank you for your help – Erbez Sep 09 '18 at 11:42
  • Thank you, very well done, simple and efficient, gets job done – Erbez Sep 09 '18 at 11:57
  • 1
    https://stackoverflow.com/questions/15614130/static-member-views-in-activity-android – prashant17 Sep 13 '18 at 07:13
  • @prashant17 Yes, that is a problem, but this was the quickest way of doing it. The alternative would be either your answer, or passing the context of `MainActivity.java` to the adapter. – olfek Sep 13 '18 at 07:18
1

you can do this :

1- First create one interface like this

public interface OnMovieClickListener {

      void onClick(int movieId);

}

2- Second your activity should implement this interface

3- In your ViewHolder Constructor you should do someting like this :

ViewHolder(View itemView) {
      itemView.setOnClickListener(new OnClickListener() {
              public void onClick() {
                      listener.onClick(this.getLayoutPosition());
              }
      }
}

and your adapter's constructor is like this :

private OnMovieClickListener listener;
public adapter(OnMovieClickListener listener) {
         this.listener = listener
}

please note that your viewholder inner class should not be static. finally pass your activity to your adapter ;

Adapter adapter = new Adapter(this);
0

first Make Movieclass parcelable

in onBindViewHolder

    holder.itemView.setTag(movie);
    holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Movie movie = (movie) view.getTag();

                Intent intent = new Intent(MainActivity.BROADCAST_ACTION);
                intent.putExtra("ITEM_DATA", movie);
                LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
            }
        });

In your activity

public static final String BROADCAST_ACTION = "BROADCAST_ACTION";

BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction() != null &&
                intent.getAction().equals(BROADCAST_ACTION)) {
            Bundle bundle = intent.getExtras();
            if (bundle!=null){
                Movie movie = bundle.getParcelable("ITEM_DATA");
                //Use movie for setting data to your layout
            }
        }
    }
};


@Override
protected void onResume() {
    super.onResume();
    LocalBroadcastManager.getInstance(
            MainActivity.this).registerReceiver(broadcastReceiver, new IntentFilter(BROADCAST_ACTION));
}

@Override
protected void onPause() {
    super.onPause();
    LocalBroadcastManager.getInstance(MainActivity.this).unregisterReceiver(broadcastReceiver);
}
prashant17
  • 1,520
  • 3
  • 14
  • 23