问题遇到的现象和发生背景
完全不知道发生了什么……
问题相关代码,请勿粘贴截图
RecordActivity
public class RecordActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private FloatingActionButton mBtnAdd;
private List<Note> mNotes;
private MyDatabaseHelper myDatabaseHelper;
private MyAdapter mMyAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_record);
initData();
initEvent();
mRecyclerView = findViewById(R.id.recyclerview);
}
private void initEvent() {
mMyAdapter = new MyAdapter(this,mNotes);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(linearLayoutManager);
mRecyclerView.setAdapter(mMyAdapter);
}
private void initData() {
mNotes = new ArrayList<>();
for (int i = 0;i<2;i++){
Note note = new Note();
note.setTitle("标题"+i);
note.setContent("内容"+i);
note.setCreatedTime(getCurrentTimeFormat());
mNotes.add(note);
}
}
private String getCurrentTimeFormat(){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY年MM月dd HH:mm:ss");
Date date = new Date();
return simpleDateFormat.format(date);
}
public void add(View view) {
Intent intent = new Intent(RecordActivity.this, AddActivity.class);
startActivity(intent);
}
}
这个是MyAdapter
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private List<Note> mBeanList;
private LayoutInflater mLayoutInflater;
private Context mContext;
public MyAdapter(Context context,List<Note> mBeanList){
this.mBeanList = mBeanList;
this.mContext = context;
mLayoutInflater = LayoutInflater.from(mContext);
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = mLayoutInflater.inflate(R.layout.item_layout, parent, false);
MyViewHolder myViewHolder = new MyViewHolder(view);
return myViewHolder;
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
Note note = mBeanList.get(position);
holder.mTvTitle.setText(note.getTitle());
holder.mTvContent.setText(note.getContent());
holder.mTvTime.setText(note.getCreatedTime());
holder.rlContainer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(mContext, EditActivity.class);
intent.putExtra("note", note);
mContext.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return 0;
}
class MyViewHolder extends RecyclerView.ViewHolder{
TextView mTvTitle;
TextView mTvContent;
TextView mTvTime;
ViewGroup rlContainer;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
this.mTvTitle = itemView.findViewById(R.id.tv_title);
this.mTvContent = itemView.findViewById(R.id.tv_content);
this.mTvTime = itemView.findViewById(R.id.tv_time);
this.rlContainer = itemView.findViewById(R.id.rl_item_container);
}
}
}
这个是activty_record
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.mrwork.record.RecordActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/add"
android:layout_gravity="right|bottom"
android:clickable="true"
android:onClick="add"
android:layout_margin="20dp"
/>
</FrameLayout>
尝试过的方法
mRecyclerView初始化了
运行结果及报错内容
这是logcat的报错……
补充了一下RecordActivity和activity_record的代码
感谢……