0

The editTask method is overriden at TaskListActivity Class which implements the OnEditTask interface.
Why are we using the context and casting it to OnEditTask interface?
What principle of interface or java am I missing here?

This is a code from the book "Android app development for Dummies".

RecyclerView Adapter
1]

Full RecyclerView Adapter Code

package com.example.manis.tasks.adapter;

import android.content.Context;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.manis.tasks.activity.R;
import com.example.manis.tasks.interfaces.OnEditTask;
import com.squareup.picasso.Picasso;


public class TaskListAdapter extends RecyclerView.Adapter<TaskListAdapter.ViewHolder>
{
    static String[] fakeData = new String[]
            {
                "One","Two","Three","Four","Five","Ah....ah..ah"
            };

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
    CardView v = (CardView) LayoutInflater.from(parent.getContext()).inflate(R.layout.card_task, parent, false);
    return new ViewHolder(v);
}

@Override
public void onBindViewHolder(ViewHolder viewHolder, final int position)
{
    final Context context = viewHolder.titleView.getContext();
    viewHolder.titleView.setText(fakeData[position]);
    Log.v("myTag",fakeData[position].toString());

   Picasso.with(context)
           .load(getImageUrlForTask(position))
           .into(viewHolder.imageView);

    viewHolder.cardView.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            ((OnEditTask)context).editTask(position);
        }
    });
}

@Override
public int getItemCount()
{
    return fakeData.length;
}

static class ViewHolder extends RecyclerView.ViewHolder
{
    CardView cardView;
    TextView titleView;
    ImageView imageView;

    public ViewHolder(CardView card)
    {
        super(card);
        cardView=card;
        titleView = (TextView) card.findViewById(R.id.text1);
        imageView = (ImageView) card.findViewById(R.id.image);
    }
}

public static String getImageUrlForTask(long taskId){
    return "http://lorempixel.com/600/400/cats/?fakeId=" + taskId;
}

}

enter image description here

enter image description here

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
mhrzn
  • 379
  • 1
  • 4
  • 18

1 Answers1

1

editTask method is overriden at TaskListActivity Class which implements the OnEditTask interface

And that explains why it needs cast. You can't call that method otherwise

It could have been cast beforehand, like on this line

final Context context = viewHolder.titleView.getContext();

Could have instead been

final OnEditTask onEditTask = (OnEditTask) viewHolder.titleView.getContext();

but the Context variable is needed elsewhere (for Picasso)

Picasso.with(context)
       .load(getImageUrlForTask(position))
       .into(viewHolder.imageView);
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • can u explain a little bit more.. why not create a TaskListActivity obj n call the method directly ? – mhrzn Aug 31 '16 at 15:59
  • 2
    Because making `new Activity()` at any point is a bad idea as it creates an unmanaged activity. The `Activity` class extends the `Context` class, so casting the context in some cases is necessary. – OneCricketeer Aug 31 '16 at 16:42
  • suppose i cast beforehand as u said final OnEditTask onEditTask = (OnEditTask) viewHolder.titleView.getContext(); and call the method like this onEditTask.editTask(1) pass it an long id, but the interface contain a empty method body. how this work ? how the method on Activity class is called ? how a the app know to call Activity method ? :D still not understanding ..sorry – mhrzn Aug 31 '16 at 16:58
  • Then Picasso has no `context` because I don't think you can cast the interface back to a `Context` – OneCricketeer Aug 31 '16 at 16:59
  • Why cast back to onEditTask i can also cast to TaskListActivity ? – mhrzn Aug 31 '16 at 17:12
  • I don't understand where you are lost... Yes, you could cast to `TaskListActivity`, but that assumes that the Adapter will only be used by a `TaskListActivity` class, and no others. It is probably better to cast to the interface because then you are not dependent upon the implementation. It is simply a preference of low coupling. – OneCricketeer Aug 31 '16 at 17:17