startActivityForResult within an adapter?

0

Good evening guys.

My scenario is as follows: In Activity A, I populate a list through a RecycleView with an ArrayList returned from my WebService. The layout of this list has an image, title, and button for each item. The button plays the role of follow / unfollow, that is when it is clicked (it changes its icon and saved 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. Okay, so good, working perfectly.

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

I thought of using startActivityForResult, but I can not call it inside my adapter which is where I handle the onclick of the button and the image. And I would not like to reload the list on ActivityA's onResume.

Does anyone have an idea?

    
asked by anonymous 15.07.2015 / 01:56

3 answers

3

Although you can not call startActivityForResult() inside your adapter , you can register Activity A as listener observer ) adapter , so when the image is clicked the adapter tells Activity , which in turn calls startActivityForResult() . >

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

When you return to Activity A , you can update the content of the RecyclerView in onActivityResult() .

    
15.07.2015 / 02:46
0

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

and in the Activity of this screen create the

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

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

    
15.07.2015 / 14:58
0

It's a bit late but it might still be useful.

On the onClick method of the 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 startActivityForResult with RecyclerView.

    
03.04.2016 / 20:42