0

I got an adapter and I got an action which open a "app camera", then I capture the image. And from here it got to execute a function called onActivityResult(requestCode: Int, resultCode: Int, data: Intent?), but I can't override the method because I can't extend AppCompatActivity().

Any help?

Pedro Relvas
  • 678
  • 7
  • 19
  • which method you want to be override? – sasikumar Mar 06 '19 at 15:06
  • You should start and handle the intent from the adapters' parent activity, then update your list and call notifyDataSetChanged(); on adapter (don't forget to cast it) still from the host activity – ArteFact Mar 06 '19 at 15:20

1 Answers1

1

You can't, in Java classes can't inherit from two classes at the same time. Only one. Considering your adapter already extends RecyclerView.Adapter then you can't.

The recommended approach is to use callback. A callback is an interface that is implements on the host activity or fragment and pass as an argument to the other class (the adapter on this case). Your adapter will hold a reference to the callback, so when the view is clicked then the reference will be called triggering the implementation on the activity or fragment.

You can see a detailed explanation here

  1. When the click happens, from the activity start the intent to the camera
  2. After the photo is taken inside onActivityResult on the activity pass the result to the adapter
  3. The adapter need a method for receiving the result, it should be something like this
public void addPhoto(SomeObject object){
    yourData.add(object);
    notifyDataseChanged();
}
  1. The code inside onActivityResult should be something like this
SomeObject objet = data... //you have to get the data and transform it to your format
adaper.addPhoto(object)
cutiko
  • 9,887
  • 3
  • 45
  • 59