我正在尝试构建这两个应用中遇到此问题。当我启动RecyclerView时,应用程序崩溃了。我已经将我的适配器代码与多个来源进行了比较,所以我不相信问题就在那里。我无法确定问题所在。
这只是一个非常简单的清单。每行100行,带有数字,编辑文本,复选框和按钮
checklist.java
public class checkList extends AppCompatActivity {
RecyclerView mRecyclerView;
private RecyclerView.Adapter myAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_check_list);
numbers[] mNumbers = new numbers[100];
mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
for (int i = 0; i<mNumbers.length; i++) {
mNumbers[i].setInt(i + 1);
}
myAdapter = new myAdapter(mNumbers);
mRecyclerView.setAdapter(myAdapter);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
}
}
numbers.java
public class numbers {
private int mInt;
public int getInt() {
return mInt;
}
public void setInt(int anInt) {
mInt = anInt;
}
}
adapter.java
public class myAdapter extends RecyclerView.Adapter<myAdapter.ViewHolder> {
private numbers[] mNumbers;
public myAdapter (numbers[] numbers) {
mNumbers = numbers;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_slot, parent, false);
ViewHolder vh = new ViewHolder(v);
return vh;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.bindNumbers(mNumbers[position]);
}
@Override
public int getItemCount() {
return mNumbers.length;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
TextView numberText;
EditText checklistText;
Button deleteButton;
CheckBox checkDisBox;
public ViewHolder(View itemView) {
super(itemView);
numberText = (TextView) itemView.findViewById(R.id.number);
checklistText = (EditText) itemView.findViewById(R.id.editText);
deleteButton = (Button) itemView.findViewById(R.id.deleteButton);
checkDisBox = (CheckBox) itemView.findViewById(R.id.checkBox);
}
public void bindNumbers(numbers numbers) {
numberText.setText(numbers.getInt() + "");
}
}
}