App is crashing when i click on any item in recyclerview

108
May 11, 2022, at 03:30 AM

I was creating a recycler view and trying to create a toast when any item is clicked on using interface(i'm doing for first time). But my app is crashing when i click any item in recycler view.

Here is my main java file code.

package com.example.creatingrecyclerview;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements newsOnCkicked {
    RecyclerView r;
    String arr[]={"1","2","3","4","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        r=findViewById(R.id.rv);
        r.setLayoutManager(new LinearLayoutManager(this));
        CustomAdapter c=new CustomAdapter(arr,this);
        r.setAdapter(c);
    }
    @Override
    public void onClick(View view) {
        
    }
    @Override
    public String onItemClicked(String s) {
        Toast.makeText(this, "Item clicked is "+ s, Toast.LENGTH_SHORT).show();
        return null;
    }
}

And this is my customadapter java code.

package com.example.creatingrecyclerview;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> {
    private String[] localDataSet;
    private newsOnCkicked k;
    /**
     * Provide a reference to the type of views that you are using
     * (custom ViewHolder).
     */
    public static class ViewHolder extends RecyclerView.ViewHolder {
        private final TextView textView;
        public ViewHolder(View view) {
            super(view);
            // Define click listener for the ViewHolder's View
            textView = (TextView) view.findViewById(R.id.textView2);
        }
        public TextView getTextView() {
            return textView;
        }
    }
    /**
     * Initialize the dataset of the Adapter.
     *
     * @param dataSet String[] containing the data to populate views to be used
     * by RecyclerView.
     */
    public CustomAdapter(String[] dataSet,newsOnCkicked k) {
        localDataSet = dataSet;
        this.k=k;
    }
    // Create new views (invoked by the layout manager)
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        // Create a new view, which defines the UI of the list item
        View view = LayoutInflater.from(viewGroup.getContext())
                .inflate(R.layout.mylayout, viewGroup, false);
        RecyclerView.ViewHolder v=new ViewHolder(view);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View vi) {
                k.onItemClicked(localDataSet[v.getAdapterPosition()]);
            }
        });
        return new ViewHolder(view);
    }
    // Replace the contents of a view (invoked by the layout manager)
    @Override
    public void onBindViewHolder(ViewHolder viewHolder, final int position) {
        // Get element from your dataset at this position and replace the
        // contents of the view with that element
        viewHolder.getTextView().setText(localDataSet[viewHolder.getAdapterPosition()]);
    }
    // Return the size of your dataset (invoked by the layout manager)
    @Override
    public int getItemCount() {
        return localDataSet.length;
    }
}
interface newsOnCkicked extends View.OnClickListener {
    String onItemClicked(String s);
}

Please help me find the error in the code. It will be great:)

Answer 1

You have to set you click listener with viewHolder . copy the code below and paste then run

     public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> {
    private String[] localDataSet;
    private newsOnCkicked k;
    /**
     * Provide a reference to the type of views that you are using
     * (custom ViewHolder).
     */
    public static class ViewHolder extends RecyclerView.ViewHolder {
        private final TextView textView;
        public ViewHolder(View view) {
            super(view);
            // Define click listener for the ViewHolder's View
            textView = (TextView) view.findViewById(R.id.textView2);
        }
        public TextView getTextView() {
            return textView;
        }
    }
    /**
     * Initialize the dataset of the Adapter.
     *
     * @param dataSet String[] containing the data to populate views to be used
     * by RecyclerView.
     */
    public CustomAdapter(String[] dataSet,newsOnCkicked k) {
        localDataSet = dataSet;
        this.k=k;
    }
    // Create new views (invoked by the layout manager)
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        // Create a new view, which defines the UI of the list item
        View view = LayoutInflater.from(viewGroup.getContext())
                .inflate(R.layout.mylayout, viewGroup, false);
        return new ViewHolder(view);
    }
    // Replace the contents of a view (invoked by the layout manager)
    @Override
    public void onBindViewHolder(ViewHolder viewHolder, final int position) {
        // Get element from your dataset at this position and replace the
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View vi) {
                k.onItemClicked(localDataSet[viewHolder.getAdapterPosition()]);
            }
        });
        viewHolder.getTextView().setText(localDataSet[viewHolder.getAdapterPosition()]);
    }
    // Return the size of your dataset (invoked by the layout manager)
    @Override
    public int getItemCount() {
        return localDataSet.length;
    }
}
interface newsOnCkicked extends View.OnClickListener {
    String onItemClicked(String s);
}

if still get problem comment below with error log.

Rent Charter Buses Company
READ ALSO
Kotlin: Subscribe to Firebase Cloud Messaging(FCM) Topic

Kotlin: Subscribe to Firebase Cloud Messaging(FCM) Topic

I've used the Android (Java) way of doing things before for subscribing to a topic

116
Lib Gdx what is the error and how to solve it?

Lib Gdx what is the error and how to solve it?

When I create a bundle, I get this errorWhen I create an apk or run directly from Android Studio, there are no errors

103
Android multi module project

Android multi module project

I'm trying to setup a multi-module project with bottom navigation fragments and hiltIs there any github project I can use to start my project faster?

144