startActivityForResult inside an Adapter?

Asked

Viewed 366 times

0

Good night, you guys.

My scenario is the following: In Activity A, I fill a list through a Recycleview with an Arraylist returned from my Webservice. The Layout of this list has an image, a title and a button for each item. The button plays the role of follow/unfollow, that is when it is clicked (mute its icon and save the status via webservice), it follows or stops following another user from the list. When I click on the image, I go to the user profile for the list item. Ok, so far so good, working perfectly.

When I enter the User profile (Activity B), there is also the follow/unfollow button. So, basically I want that if the follow/unfollow button is clicked on the user profile (Activity B), and the person returns to the user list (Activity A) the value of the button for the item is updated in the list.

I thought about using startActivityForResult, but I can’t call it inside my Adapter which is where I treat the onclick of the button and image. And I wouldn’t want to reload the list in Activity A’s onResume.

Does anyone have any idea ?

3 answers

3


Although you can’t call the startActivityForResult() within your Adapter, you can register the Activity A as Listener (Observer) of Adapter, so that when the image is clicked the Adapter informs the Activity, which in turn calls startActivityForResult().

The way to do this is very simple, just go to Activity A as a parameter in the Adapter and invoke a public method of the same when the image is clicked. Within the implementation of this public method you call the Activity B.

On returning to the Activity A, you can update the content of Recyclerview in onActivityResult().

  • Man, that’s exactly what I did. Thanks for the reply :)

0

It’s a little late but it might still be useful.

In the method onClick viewholder

@Override
public void onClick(View view) {
    Intent data = new Intent(view.getContext(), OtherActivity.class);
    data.putExtra("POSITION", getAdapterPosition());
    ((Activity) view.getContext()).startActivityForResult(data, 1);
}

Example sending data to another Activity using the startActivityForResult with Recyclerview.

0

In the xml of your button put android:onClick="nameEvento"

and in the Activity of this screen create the method

public void nomeEvento(View botao){
    //faça o que quiser aqui
   //startActivityForResult...
}

So every time any of the buttons is clicked the "windname method will be executed"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.