Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Android Fragments Introducing Fragments Handling Clicks

Callback interface method isn't working for me! It isn't able to recognize which view is clicked? Please have a look.

Here's is my MainActivity:

    public class MainActivity extends AppCompatActivity implements List_Fragment.onRecipeSelectedInterface {

        private final String TAG = getClass().getSimpleName();

        @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        List_Fragment savedFragment = (List_Fragment) getFragmentManager().findFragmentById(R.id.placeHolder);
        if(savedFragment==null){

            List_Fragment fragment = new List_Fragment();
            FragmentTransaction transaction = getFragmentManager().beginTransaction();
            transaction.add(R.id.placeHolder,fragment);
            transaction.commit();
        }

    }
    @Override
    public void onRecipeSelected(int index) {

        Log.d(TAG, "It's being clicked!");
        Toast.makeText(this, Recipes.names[index], Toast.LENGTH_SHORT).show();
    }
}

My Class which has implemented Fragment

public class List_Fragment extends Fragment {

    public interface onRecipeSelectedInterface{
        void onRecipeSelected(int index);
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.recipe_layout, container, false);
        onRecipeSelectedInterface listener = (onRecipeSelectedInterface) getActivity();
        RecyclerView recyclerView = view.findViewById(R.id.recyclerView);
        Adapter adapter = new Adapter(view.getContext(), listener);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setAdapter(adapter);
        return view;
    }
}

Adapter Class

public class Adapter extends RecyclerView.Adapter<Adapter.Holder>{


    private final List_Fragment.onRecipeSelectedInterface listener;
    private Context context;

    public Adapter(Context context, List_Fragment.onRecipeSelectedInterface listener) {
        this.listener = listener;
        this.context = context;
    }

    @NonNull
    @Override
    public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_items,parent,false);

        return new Holder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull Holder holder, final int position) {

        ((Holder)holder).bindView(position);
    }

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

    class Holder extends RecyclerView.ViewHolder implements View.OnClickListener{

        TextView textView;
        ImageView imageView;
        int index;
        void bindView(int position){
            textView.setText(Recipes.names[position]);
            imageView.setImageResource(Recipes.resourceIds[position]);
            index = position;
            Log.d("TAG", index + "");
        }
        Holder(View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.nameTextView);
            imageView = itemView.findViewById(R.id.imageView);
        }

        @Override
        public void onClick(View v) {

            listener.onRecipeSelected(index);
        }
    }
}