有一个学生列表。每行显示图像,名称和编号。 我创建了一个会议室数据库,并设法使用this指南将“名称”和“数字”列填充到列表中。
当用户打开AddNewStudentActivity时,他/她需要从图库中选择一张照片并填写两个editTexts作为名称和编号,然后单击“保存”并将学生保存到studentDatabase。
图像应与这两个文本(名称和编号)一起显示在列表中。
我没有IDEA如何做到这一点我只认为这个过程应该像“设置打开图库的意图,我们可以选择一个图像并将其路径存储在数据库中将它从数据库显示到列表“但不知道如何编写它。有关于此的教程,但他们都使用SQLITE而不是Room,我是整个数据库主题的新手。
-Thanks
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;
}
}
答案 0 :(得分:1)
老实说,这个过程与 Room 并没有多大区别。正如您所说,要从图库中选择照片,您需要使用意图:
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "select a picture"), YOUR_IMAGE_CODE);
然后你在onActivityResult
中处理这个案例:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == YOUR_IMAGE_CODE) {
if(resultCode == RESULT_OK)
Uri selectedImageUri = data.getData();
}
}
因此selectedImageUri
是您在数据库中保存的信息。在您的实体班Student.java
中,您可以将mStudentPic
更改为String
,因此当您插入Uri
时,您可以使用某种方法:
selectedImageUri.toString();
当您想将其转换回Uri
时:
Uri uri = Uri.parse(yourUriAsString);
我假设您知道如何插入和查询数据库中的值。
然后在您的onBindViewHolder
中,您可以使用Glide或Picasso或任何其他库来加载图片,例如使用Glide:
Glide.with(context)
.load(new File(uri.getPath()))
.into(imageView);
答案 1 :(得分:0)
另外,我使用与SQLite相同的功能我只存储图像路径并从SQLite获取并使用图像URI加载图像
holder.imgUserPhoto.setImageURI(Uri.parse(new File(contactModel.getContactPhoto()).toString()));