0

I'm trying to read an element from the list shown below (using onClick), with the data being read from a JSON file, the data itself only holds strings. However, I'm having trouble actually registering the array elements to the onClick procedure.

I have absolutely no experience with JSON and am trying to familiarise myself with reading a JSON file and storing it in an Array (which I've done), but I then want to click the individual fields and make it do something.

below is what I have so far:

    public void onBindViewHolder(Adapter.ViewHolder holder, int position) {

    final ItemFruit itemFruit = fruitList.get(position);


    holder.tvFruit.setText(itemFruit.getFruit());
    holder.tvPrice.setText(itemFruit.getPrice());
    holder.linearLayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            //when item is clicked, TOAST displays the field name

        }
    });

}

Image of array: array of fruit

Any help whatsoever would be greatly appreciated. Thanks in advance

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
JamesSon
  • 1
  • 1

1 Answers1

2

put this inside onclick

 Toast.makeText(adapterContext, itemFruit.getFruit(), 
   Toast.LENGTH_LONG).show();

Here adapterContext you have to maintain at adapter level.

Create global variable

   Context adapterContext

on onCreateViewHolder initialize it

 adapterContext = parent.getContext();

and then use it.

Ankit Aman
  • 999
  • 6
  • 15