S
S
suda9112016-02-16 22:10:40
Java
suda911, 2016-02-16 22:10:40

Click handler not working in RecyclerView?

I just figured out how RecyclerView.Adapter works, the next step was the click handler. Read how they do it. For me, it was of great interest that everyone does them differently: they put the handler in one method, then in another.
In general, the bottom line is that by clicking on the menu item in the RecyclerView, you need to go to another Activity , and pass the value ( _id, I don’t display this value in the RecycleView, it’s stored in my list) by which it will be possible from the database take all the data and display it on the form.
I read that this can be done in the adapter and found one of the easiest solutions, but it doesn’t work.

public class RVAdapter extends RecyclerView.Adapter<RVAdapter.PersonViewHolder> {




    public static class PersonViewHolder extends RecyclerView.ViewHolder  {
        CardView cv;
        TextView personName;
        TextView personAge;
        PersonViewHolder(View itemView) {
            super(itemView);
            cv = (CardView)itemView.findViewById(R.id.cv);
            personName = (TextView)itemView.findViewById(R.id.person_name);
            personAge = (TextView)itemView.findViewById(R.id.person_cell);

        }

    }


    List<MyItem> items;
    RVAdapter(List<MyItem> items){
        this.items = items;
    }

    @Override
    public PersonViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cardview, viewGroup, false);
        PersonViewHolder pvh = new PersonViewHolder(v);
        return pvh;
    }

    @Override
    public void onBindViewHolder(RVAdapter.PersonViewHolder personViewHolder, int i) {

         final int idvadaptere = items.get(i).idvliste;   // вот то значение которое я хочу передать вместе с Intent
        personViewHolder.personName.setText(items.get(i).name);
        personViewHolder.personAge.setText(String.valueOf(items.get(i).cell));
        personViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Context context = v.getContext();
                Intent ddddd = new Intent(context, Main2Activity.class);
                ddddd.putExtra("ddddddd", idvadaptere);
                context.startActivity(ddddd);


            }
        });

    }


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


    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
    }

}

Here immediately LogCat /
02-16 22:02:22.065 32618-32618/com.example.pro.appi E/AndroidRuntime: FATAL EXCEPTION: main
02-16 22:02:22.065 32618-32618/com.example.pro.appi E/AndroidRuntime: Process: com.example.pro.appi, PID: 32618
02-16 22:02:22.065 32618-32618/com.example.pro.appi E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.pro.appi/com.example.pro.appi.Main2Activity}: android.content.res.Resources$NotFoundException: String resource ID #0x2
02-16 22:02:22.065 32618-32618/com.example.pro.appi E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2316)
02-16 22:02:22.065 32618-32618/com.example.pro.appi E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2376)
02-16 22:02:22.065 32618-32618/com.example.pro.appi E/AndroidRuntime:     at android.app.ActivityThread.access$800(ActivityThread.java:147)
02-16 22:02:22.065 32618-32618/com.example.pro.appi E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281)
02-16 22:02:22.065 32618-32618/com.example.pro.appi E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
02-16 22:02:22.065 32618-32618/com.example.pro.appi E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:135)
02-16 22:02:22.065 32618-32618/com.example.pro.appi E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5253)
02-16 22:02:22.065 32618-32618/com.example.pro.appi E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
02-16 22:02:22.065 32618-32618/com.example.pro.appi E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)
02-16 22:02:22.065 32618-32618/com.example.pro.appi E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
02-16 22:02:22.065 32618-32618/com.example.pro.appi E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
02-16 22:02:22.065 32618-32618/com.example.pro.appi E/AndroidRuntime:  Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x2
02-16 22:02:22.065 32618-32618/com.example.pro.appi E/AndroidRuntime:     at android.content.res.Resources.getText(Resources.java:284)
02-16 22:02:22.065 32618-32618/com.example.pro.appi E/AndroidRuntime:     at android.widget.TextView.setText(TextView.java:4176)
02-16 22:02:22.065 32618-32618/com.example.pro.appi E/AndroidRuntime:     at com.example.pro.appi.Main2Activity.onCreate(Main2Activity.java:133)
02-16 22:02:22.065 32618-32618/com.example.pro.appi E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:5975)
02-16 22:02:22.065 32618-32618/com.example.pro.appi E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
02-16 22:02:22.065 32618-32618/com.example.pro.appi E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
02-16 22:02:22.065 32618-32618/com.example.pro.appi E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2376) 
02-16 22:02:22.065 32618-32618/com.example.pro.appi E/AndroidRuntime:     at android.app.ActivityThread.access$800(ActivityThread.java:147) 
02-16 22:02:22.065 32618-32618/com.example.pro.appi E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281) 
02-16 22:02:22.065 32618-32618/com.example.pro.appi E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102) 
02-16 22:02:22.065 32618-32618/com.example.pro.appi E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:135) 
02-16 22:02:22.065 32618-32618/com.example.pro.appi E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5253) 
02-16 22:02:22.065 32618-32618/com.example.pro.appi E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method) 
02-16 22:02:22.065 32618-32618/com.example.pro.appi E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372) 
02-16 22:02:22.065 32618-32618/com.example.pro.appi E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
02-16 22:02:22.065 32618-32618/com.example.pro.appi E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 

And yes, I asked a question not so long ago and I was told that the whole essence of the error that occurs in the application can be found in LogCat'. I read that it’s impossible to launch the activity, but how to solve the problem .. If it doesn’t make it difficult, tell me where the error is indicated in it now and how you can decrypt it in general so that you don’t go out with such questions, but figure it out yourself.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
georgeci, 2016-02-17
@suda911

Read carefully:
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x2
...
at android.widget.TextView.setText(TextView.java:4176)
at com.example.pro.appi.Main2Activity. onCreate(Main2Activity.java:133)
You assign TextView.setText(#0x2) in Main2Activity, that is, you pass a number, the setText method with a numeric parameter searches for a string in the resources. Doesn't find it and crashes, outputting a gorgeous StackTrace.
You should be like this:

int number = 5;
TextView.setText(Integer.toString(number))

M
Makhach Imangazaliev, 2016-02-17
@ImangazalievM

How to set OnItemClickListener and OnItemLongClickListener for RecycleView? - java-help.ru/onitemclicklistener-and-onitemlongcli...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question