-1

i'm write code for OnClickListener in RecyclerView and i want put data from json. data from json its work but i cant bring the data to other activity.

this is a new android studio version is 3.4.1.

this code in caritenda.

public void onClick(View view, int position) {

        Tenda ambil = tendaList.get(position);
        String alamat = ambil.getAlamat();
        String telepon = ambil.getPhone();
        String img = ambil.getImageUrl();
        String lokasi = ambil.getLokasi();
        Intent i = new Intent(this, PilihTenda.class);
        i.putExtra(Key_RegisterActivity,new String[]{alamat,telepon, img, lokasi});
        startActivity(i);
    }

this my adapter

    itemView.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View view) {
                   listener.onClick(view, getAdapterPosition());
               }
           });
        }
    }

this is a error.

    E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.mydome, PID: 8658
    java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
        at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
        at java.util.ArrayList.get(ArrayList.java:308)
        at com.example.mydome.ui.Caritenda.onClick(Caritenda.java:95)
        at com.example.mydome.adapters.TendaAdapter$MyViewHolder$1.onClick(TendaAdapter.java:78)
        at android.view.View.performClick(View.java:5212)
        at android.view.View$PerformClick.run(View.java:21214)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5619)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:853)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:737)
Markus Kauppinen
  • 3,025
  • 4
  • 20
  • 30

2 Answers2

0
Tenda ambil = tendaList.get(position); 

Here you are getting exception.

Correct your code as below

public void onClick(View view, int position) {

    if(tendaList.size > 0){
       Tenda ambil = tendaList.get(position);
       String alamat = ambil.getAlamat();
       String telepon = ambil.getPhone();
       String img = ambil.getImageUrl();
       String lokasi = ambil.getLokasi();
       Intent i = new Intent(this, PilihTenda.class);
       i.putExtra(Key_RegisterActivity,new String[]{alamat,telepon, img, lokasi});
       startActivity(i);
    }
}
Hardik Bambhania
  • 1,732
  • 15
  • 25
  • AVOID nesting! It's bad practice for simple functions. Instead, use `if (tendaList.isEmpty()) { return; }` // rest of the function` – Zun Jul 23 '19 at 07:43
0

Method getAdapterPosition doesn't work like that. See: getAdapterPosition() not returning position of item in RecyclerView

Better way would be to set click listener on viewholder itself and you could also pass the reference to the object from the list that was clicked.

public final class TendaAdapter extends RecyclerView.Adapter<TendaAdapter.ViewHolder> {

    private final List<Tenda> tendaList;
    private final ClickListener listener;

    public TendaAdapter(List<Tenda> tendaList, ClickListener listener) {
        super();
        this.tendaList = tendaList;
        this.listener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        final View itemView = // create your itemView here
        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        final Tenda ambil = tendaList.get(position);
        holder.bind(ambil, listener);
    }

    @Override
    public int getItemCount() {
        return tendaList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
        }

        public void bind(Tenda tenda, ClickListener listener) {
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.onClick(tenda);
                }
            });
        }
    }

    interface ClickListener {
        void onClick(Tenda tenda);
    }
}
TheKarlo95
  • 1,144
  • 9
  • 17