有一组学生名单,每一行都显示了一张图片、姓名和编号。我创建了一个Room数据库,并使用这个指南成功地将“姓名”和“编号”列填充到列表中。(此处链接略)
当用户打开AddNewStudentActivity时,需要从相册选择一张照片,并在两个editText中填写姓名和编号,点击“保存”并将学生保存到studentDatabase中。
在列表中应该显示这个图像以及那两个文本(姓名和编号)。
我不知道怎么做,只是认为这个过程应该像“设置一个打开相册的intent,我们可以选择一张图片,并把它的路径存储在数据库中,然后从数据库中显示到列表中”。但我不知道如何编码。有关此问题的教程都使用SQLITE,而不是Room,我对整个数据库主题很陌生。
- 谢谢
NewStudentActivity.java
在列表中应该显示这个图像以及那两个文本(姓名和编号)。
我不知道怎么做,只是认为这个过程应该像“设置一个打开相册的intent,我们可以选择一张图片,并把它的路径存储在数据库中,然后从数据库中显示到列表中”。但我不知道如何编码。有关此问题的教程都使用SQLITE,而不是Room,我对整个数据库主题很陌生。
- 谢谢
NewStudentActivity.java
public class NewStudentActivity extends AppCompatActivity {
public static final String EXTRA_REPLY = "com.example.android.studentlistsql.REPLY";
private EditText mNameWordView;
private EditText mNumWordView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_student);
mNameWordView = findViewById(R.id.name_word);
mNumWordView = findViewById(R.id.num_word);
final Button button = findViewById(R.id.button_save);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent replyIntent = new Intent();
if (TextUtils.isEmpty(mNameWordView.getText())) {
setResult(RESULT_CANCELED, replyIntent);
} else {
String word = mNameWordView.getText().toString();
replyIntent.putExtra(EXTRA_REPLY, word);
setResult(RESULT_OK, replyIntent);
}
if (TextUtils.isEmpty(mNumWordView.getText())) {
setResult(RESULT_CANCELED, replyIntent);
} else {
String word = mNumWordView.getText().toString();
replyIntent.putExtra(EXTRA_REPLY, word);
setResult(RESULT_OK, replyIntent);
}
finish();
}
});
}
}
StudentListAdapter.java
public class StudentListAdapter extends RecyclerView.Adapter<StudentListAdapter.WordViewHolder> {
class WordViewHolder extends RecyclerView.ViewHolder {
private final TextView nameItemView;
private final TextView numberItemView;
private WordViewHolder(View itemView) {
super(itemView);
nameItemView = itemView.findViewById(R.id.nameTextView);
numberItemView = itemView.findViewById(R.id.numberTextView);
}
}
private final LayoutInflater mInflater;
private List<Student> mStudents; // Cached copy of words
StudentListAdapter(Context context) { mInflater = LayoutInflater.from(context); }
@Override
public WordViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = mInflater.inflate(R.layout.recyclerview_item, parent, false);
return new WordViewHolder(itemView);
}
@Override
public void onBindViewHolder(WordViewHolder holder, int position) {
if (mStudents != null) {
Student current = mStudents.get(position);
holder.nameItemView.setText(current.getStudentName());
holder.numberItemView.setText(current.getStudentNumber());
} else {
// Covers the case of data not being ready yet.
holder.nameItemView.setText("No Word");
}
}
void setStudents(List<Student> words){
mStudents = words;
notifyDataSetChanged();
}
// getItemCount() is called many times, and when it is first called,
// mWords has not been updated (means initially, it's null, and we can't return null).
@Override
public int getItemCount() {
if (mStudents != null)
return mStudents.size();
else return 0;
}
}