There are a list of students.each row shows an Image,Name and number. I created a Room database and only managed to populate "name" and "number" columns into the list using this guide.
When user opens the AddNewStudentActivity, He/She needs to choose a photo from gallery and fill two editTexts for the name and number and click "save" and save to student to the studentDatabase.
the image should be displayed in the list alongside those two texts(name and number).
I have NO IDEA how to do this i only think the process should be like "setting up an intent that opens the gallery and we can choose an image and get it's path stored on the database and display it from database to the list"but don't know how to code it.there are tutorials about this but they were all using SQLITE and not Room and I'm new to the whole database topic.
-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;
}
}
To be honest the process isn't that much different with Room. As you said, to pick the photo from the Gallery you use an intent:
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "select a picture"), YOUR_IMAGE_CODE);
Then you handle this case in 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();
}
}
So selectedImageUri
is the piece of information that you save in your database. In your Entity class Student.java
you can change mStudentPic
to String
so when you insert your Uri
you can use a method:
selectedImageUri.toString();
and when you want to convert it back to Uri
:
Uri uri = Uri.parse(yourUriAsString);
I assumed that you know how to insert and query values from the database.
And then in your onBindViewHolder
you can use Glide or Picasso, or any other library to load the image, for example with Glide:
Glide.with(context)
.load(new File(uri.getPath()))
.into(imageView);
Also, I am using the same function with SQLite I am the storing just image path and get from SQLite and load image using image URI
holder.imgUserPhoto.setImageURI(Uri.parse(new File(contactModel.getContactPhoto()).toString()));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With