skip to Main Content

I am just practicing the RecyclerView. when I run the App my App crashed I check the log but i cant understand the problem can some help me im waiting for someone responce?

public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> {

    private model[] localDataSet;
    private TextView name,number;
    public static class ViewHolder extends RecyclerView.ViewHolder {
        private final TextView textView;

        public ViewHolder(View view) {
            super(view);
            textView = (TextView) view.findViewById(R.id.name);
        }

        public TextView getTextView() {
            return textView;
        }
    }
    public CustomAdapter(model[] dataSet) {
        localDataSet = dataSet;
    }
    @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.contacx, viewGroup, false);
        view.findViewById(R.id.name);
        view.findViewById(R.id.number);
        return new ViewHolder(view);
    }

    // Replace the contents of a view (invoked by the layout manager)
    @Override
    public void onBindViewHolder(ViewHolder viewHolder, final int position) {


        name.setText(localDataSet[position].getName());
        number.setText(localDataSet[position].getPhone());
    }
    @Override
    public int getItemCount() {
        return localDataSet.length;
    }
}

enter image description here

2

Answers


  1. You need to change the following things in your code:

    public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> {
    
    private model[] localDataSet;
    //remove this from here.
    //private TextView name,number;
    public static class ViewHolder extends RecyclerView.ViewHolder {
        //This should be from the layout you have passed in ViewHolder method. in your case from R.layout.contacx. This layout should have two textview with ids:- name and phone respectively.
        private final TextView name, phone;
    
        public ViewHolder(View view) {
            super(view);
            name = view.findViewById(R.id.name);
            phone = view.findViewById(R.id.phone);
        }
    }
    public CustomAdapter(model[] dataSet) {
        localDataSet = dataSet;
    }
    @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.contacx, viewGroup, false);
        //remvoe this from here.
        //view.findViewById(R.id.name);
        //view.findViewById(R.id.number);
        return new ViewHolder(view);
    }
    
    // Replace the contents of a view (invoked by the layout manager)
    @Override
    public void onBindViewHolder(ViewHolder viewHolder, final int position) {
    
       //here you should call those view to bind your data like:
        viewHolder.name.setText(localDataSet[position].getName());
        viewHolder.phone.setText(String.valueOf(localDataSet[position].getPhone()));
    }
    @Override
    public int getItemCount() {
        return localDataSet.length;
     }
    }
    
    Login or Signup to reply.
  2. Use ViewHolder for data bind. More safe

    public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> {
        private model[] localDataSet;
    
        public static class ViewHolder extends RecyclerView.ViewHolder {
            private final TextView name, number;
            public ViewHolder(View view) {
                super(view);
                name = view.findViewById(R.id.name);
                number = view.findViewById(R.id.phone);
            }
    
            public void bindData(model data) {
                name.setText(data.getName());
                number.setText(String.valueOf(data.getPhone()));
            }
        }
    
        public CustomAdapter(model[] dataSet) {
            localDataSet = dataSet;
        }
    
        @Override
        public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
            /* No Need
              View view = LayoutInflater.from(viewGroup.getContext())
                    .inflate(R.layout.contacx, viewGroup, false);
              view.findViewById(R.id.name);
              view.findViewById(R.id.number);
             */
            return new ViewHolder(LayoutInflater.from(viewGroup.getContext())
                    .inflate(R.layout.contacx, viewGroup, false););
        }
    
        @Override
        public void onBindViewHolder(ViewHolder viewHolder, final int position) {
            /*
            name.setText(localDataSet[position].getName());
            number.setText(localDataSet[position].getPhone());
             */
            viewHolder.bindData(localDataSet[position]);
        }
    
        @Override
        public int getItemCount() {
            return localDataSet.length;
        }
    
       
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search