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;
}
}
2
Answers
You need to change the following things in your code:
Use ViewHolder for data bind. More safe