Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] java – Execute MainActivity method inside recyclerView

Posted on November 11, 2022 By

Solution 1 :

runOnUiThread does not give you access to any underlying view, it just runs on a thread that is responsible for rendering UI.

The idea behind it is correct though. You need to have some way to reference an element from the activity.

Create some interface with a callback function and create an instance in the activity.

interface OnItemClickListener {
    void onItemClick(/* Here have an argument that you want to pass from RecyclerView*/);
}

Activity implementation:

private OnItemClickListener mListener = new OnItemClickListener () {
    @Override
    public void onItemClick(String message) { // e.g. message
        //setImage();
    }
};

And just pass it in the constructor of the adapter and store it.

new *****Adapter(..., listener)

class ****Adapter {

    private OnItemClickListener mListener;

And just invoke it in the onClickListener that you set there:

holder.itemView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        listener.onItemClick("message");
    }
});

Problem :

I have a Layout as an image and a recyclerView below the image, I want the image to change when the user clicks on one of the elements in the list.

I tried to do it this way, but it didn’t work:

Method to change the image that is in MainActivity

`

    public void setImagem (){

    runOnUiThread(new Runnable() {

        public void run() {

            imageView.setImageResource(R.drawable.cascatinha);

        }
    });

}

`
onClick method inside the recyclerView

`

        holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            MainActivity mainActivity = new MainActivity();
            mainActivity.setImagem();


        }
    });

`

When I click on one of the elements, the app closes and this error message appears:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference

Comments

Comment posted by Tyler V

You should

READ  [FIXED] Kotlin + MockIto + Android Instrumentation Test
Powered by Inline Related Posts
Android Tags:android, android-recyclerview, java

Post navigation

Previous Post: Do you have to pay for MyNetDiary?
Next Post: [FIXED] Simple Cucumber Test is not running on Sample Android Project

Related Posts

[FIXED] java – How can i make my spinner value set to the last value i selected before leaving the page when i come back in kotlin Android
[FIXED] android – Apply multiple style on TextView Android
[FIXED] java – is it possible to retrieve texts and images from the same SQLite database Android
[FIXED] java – Save attributes of object Android
[FIXED] java – How to manually validate EditText? Android
[FIXED] android – How to use MutableStateFlow to search item in list Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • What is a good substitute for condensed milk?
  • ¿Como el cuerpo te avisa cuando te va a dar un infarto?
  • What is the luxury brand of Jaguar?
  • Who is Big poppa baseball player?
  • What material are foam runners?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme