In my Android App, the Captured image along with its details is stored and displayed in a recycler View. The Details are name and description of the picture. I'm storing the details along with Storage path(mCurrentpath) of the captured image in a room database table. I'm able to retrieve each row of the database and display it in recyclerview item. The problem is - if I try to save only the details without any picture captured,the item displays an empty imageview along with details. Instead of displaying empty image view i need to display an default image.Kindly help
Note: i also tried to save a default string in database if image not captured. and checked With if() statement in onBindViewHolder
#Override
public void onBindViewHolder(#NonNull ImageHolder imageHolder, int i) {
ImageEntry currentImageEntry = imageEntries.get(i);
String storedAddress = currentImageEntry.getImageStoredAddress();
if(storedAddress != "Address is null") {
imageHolder.imageView.setImageBitmap(BitmapFactory.decodeFile(currentImageEntry.getImageStoredAddress()));
}else {imageHolder.imageView.setImageResource(R.drawable.default_image);}
imageHolder.textViewTpye.setText(currentImageEntry.getPropertyType());
imageHolder.textViewDesc.setText(currentImageEntry.getProprtyDescription());
}
thanks
use equals for comapring string value
if (storedAddress.equals("Address is null")) {
imageHolder.imageView.setImageResource(R.drawable.default_image);
} else {
imageHolder.imageView.setImageBitmap(BitmapFactory.decodeFile(currentImageEntry.getImageStoredAddress()));
}
Note : == tests object references, .equals() tests the string values.
for Example,
String a="test me";
String b="test me";
a == b //this will return false
a.equals(b) //this will return true
Just Place a default image in XML file and change this image
if
if(storedAddress != "Address is null")
or If no image is captured , store default image address in database.
Related
I'm currently creating a simple contact app using Kotlin and MVVM Architecture, I was able to display contact's name and email address with recyclerview, but when a "Contact" doesn't have a name I want to display only the email address also sorting it alphabetically in the recyclerview either the data is a "name" or "email address".
my Goal (name, email)
- John, john#gmail.com
- kaila#gmail.com
- Lee, lee#gmail.com
I've tried altering my List variable with
for (contact in listContacts) {
if (contact.name == "" || contact.name.isEmpty()) {
contact.name = contact.email
}
}
but it will make contact.name to have the same value as contact.email and I don't want it. contact.name need to keep their original value which is "" or null for further feature.
I can't think of a way to resolve this issue by changing the query inside my DAO.
For that I think you have taken two variables in your pojo and model class.
Now, For displaying Name I think you have taken a simple TextView in your raw file or a xml file for the RecyclerView's raw (adapter).
All right.
Now, You are setting the name value from contact.name which is correct.
But, In case If there is no value in it, You are assigning the value of contact.email in to contact.name. That's the reason the value of name is changed.
To avoid this, why you can't set the value of contact.email directly to your TextView as below :
for (contact in listContacts) {
if (contact.name == "" || contact.name.isEmpty()) {
tv_user_name.setText(""+contact.email);
}else{
tv_user_name.setText(""+contact.name);
}
}
Here, tv_user_name is a TextView inside your raw file.
Simple..!
Alphabetic sorting use List.sortBy()
list.sortBy { if (!it.name.isNullOrEmpty()) it.name else it.email }
Whenever name is not present, it will check with email
I have a SQLite database which feeds data to the cardviews via an ArrayList. Every record in the database consists of a different color code, I want to set the background color of each individual card to its respective color present in the database. Color codes are under the "color" column inside the database.
Now here's my code, as you can see my SQL SELECT Query should only select ONE PARTICULAR RECORD for each individual card but what is doing here is that it selects all the records present in database.
If I use c.moveToFirst(); it selects the first row of the table and if I use c.moveToLast(); it selects the last row of the table? If I am clearing mentioning ...where id ="+cardid+";", isn't it supposed to select only one particular row?
The problem I am facing here is that it sets a common background color for all cardviews, I need it to be different for every card. What am I doing wrong?
Code Reference :
TextView myid consists of the unique identifier of every record in the database
PersonViewHolder(View itemView, final Activity theact) {
super(itemView);
personName = (TextView)itemView.findViewById(R.id.person_name);
personAge = (TextView)itemView.findViewById(R.id.person_age);
personPhoto = (ImageView)itemView.findViewById(R.id.person_photo);
myid = (TextView)itemView.findViewById(R.id.IDTAG);
ohcolor = (TextView)itemView.findViewById(R.id.COLORCODE);
cv = (CardView)itemView.findViewById(R.id.cv);
cv.setUseCompatPadding(true);
openDatabase();
final String cardid = myid.getText().toString().trim();
c = db.rawQuery("SELECT color FROM persons WHERE id ="+cardid+";", null);
c.moveToFirst();
colorbro = c.getString(0);
i = Integer.parseInt(colorbro);
cv.setCardBackgroundColor(i);
cv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// do something
}
});
}
Problem is you are having only one reference to cardView which is getting updated, the moment you assign new object to it.
Replace this line of code
cv = (CardView)itemView.findViewById(R.id.cv);
with this
CardView cv = (CardView)itemView.findViewById(R.id.cv);
This should fix your problem.
The docs for db.rawQuery say "The SQL string must not be ; terminated", so that is potentially your problem. http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html
The right way to get color from colors.xml file is either provide color name or its id to getColor method:
i = Integer.parseInt(colorbro);
cv.setCardBackgroundColor(ContextCompat.getColor(context, i));
I am not sure what you have in your i variable. Debug and see if it has the correct value.
I found a solution myself, we can set the background color of the card inside OnBindViewHolder method
public void onBindViewHolder(PersonViewHolder personViewHolder, int i) {
personViewHolder.myid.setText(persons.get(i).id);
personViewHolder.personName.setText(persons.get(i).name);
personViewHolder.personAge.setText(persons.get(i).age);
personViewHolder.personPhoto.setImageResource(persons.get(i).photoId);
personViewHolder.ohcolor.setText(persons.get(i).color);
//setting background color here
personViewHolder.cv.setCardBackgroundColor(Integer.parseInt(persons.get(i).color));
}
Here, persons is an ArrayList getting data from the SQLite database. As my database also has a column for color codes, I feed it to the ArrayList and retrieve it inside OnBindViewHolder and set the background color for each individual card.
so in my app I need to save and to display an image on a image view, so i use SharedPreferences for saving it, the problem is that, if there is not a saved value, i want to display an image from drawables, and for that I´m using this code:
final ImageButton imgView = (ImageButton) findViewById(R.id.UserImageButton);
String photo = PreferenceManager.getDefaultSharedPreferences(UserActivity.this).getString("Image", String.valueOf(getResources().getDrawable(R.drawable.user, getApplicationContext().getTheme())));//NameOfTheString, StringIfNothingFound
imgView.setImageBitmap(BitmapFactory.decodeFile(photo));
But if there is no image saved, the drawable is not used on the ImageView, and it doesn´t show anything.
How could I fix it?
There's no requirement to use the default value of your getString() method as a direct path to your drawable. You could simply do the following:
private static final String IMAGE_PREF = "image_pref_key";
private static final String PREF_FAIL_CODE= "no_image_found";
String photo = PreferenceManager.getDefaultSharedPreferences(UserActivity.this).getString(IMAGE_PREF, PREF_FAIL_CODE);
if (photo.equals(PREF_FAIL_CODE) {
imageView.setImageDrawable(getResources().getDrawable(R.drawable.user));
}
When you are getting the string from default shared preferences, if the value is not stored, set the default value returned to something e.g. "not found"
Now before setting doing setImageBitmap check if the value in your String variable photo is "not found". If it is display the drawable from your resource. Else display your saved image after decoding it as your are doing it currently.
so for my android application i can insert an image to sqlite as a string. But im not sure exactly how i can retrieve this image from the database and convert it to a Image from string and display it in my app. Stuck for weeks, help appreciated! :(
Button Click saves image to sqlite
upload.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Student student = new Student(profadmin.getText().toString());
student.setImageUri(ImageUri);
stud.updateImage(student);
Toast passes = Toast.makeText(ProfileActivity.this, "Profile Picture has been updated", Toast.LENGTH_SHORT);
passes.show();
}
});
Database Handler:
public int updateImage(Student student) {
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(STUDENT_IMAGEURI, student.getImageUri().toString());
int rowsAffected = db.update(STUDENT_TABLE, values, STUDENT_ADMINNO + "=?", new String[]{String.valueOf(student.getAdminNo())});
db.close();
return rowsAffected;
}
Edit: Uploaded the image by clicking the empty ImageView (profileIV) , opening the android emulator gallery, choose image and then image will be displayed on the app. Then i click the 'Upload' button and it sends it to the database and uploads
public void onActivityResult(int reqCode,int resCode,Intent data){
if(resCode==RESULT_OK){
if(reqCode==1){
ImageUri = data.getData();
profileIV.setImageURI(data.getData());
}
}
What you can do is storing your pictures in your drawable folder.
Then you will just have to store the name in your database and retrieve the corresponding image thanks to this one. It will also allow you to avoid a heavy DB file.
Why you need to store image into database? it's not a good choice.
If you have to store image into database, you can encode you image with Base64,and store the result string into database. so you can decode the string to image. But this way will cause problems with large images.
I suggest you to store images with disk cache.
I FOUND A SOLUTION FOR RESTORING MY VALUES, SEE MY NEW VERSION of populateFields()
Okay so I have been reading through all the Spinner and SQlite posts on here and cannot seem to find a good answer for what I am looking for so I am posting this scenario.
My app has two screens and uses the sqlite database on my device saving a name and weight fields from editTexts as strings like so
String eName = name.getText().toString();
String eWeight = weight.getText().toString();// where name and weight are EditTexts
and I have two spinners as follows
String eReps = spinReps.getSelectedItem().toString();
String eSets = spinSets.getSelectedItem().toString();
Then I call this to add to the database
long id = mDbHelper.createExercise(eName, eWeight, eReps, eSets);
Here is where my issue is, upon someone selecting to create a new exercise my app crashes because it is trying to populate a spinner incorrectly. Here is what I have currently.
private void populateFields(){
if(mRowId != null){ // where mRowId is the selected row from the list
Cursor exercise = mDbHelper.fetchExercise();
name.setText(exercise.getString(exercise.
getColumnIndexOrThrow(ExerciseDbAdapter.KEY_NAME)));
weight.setText(exercise.getString(exercise.
getColumnIndexOrThrow(ExerciseDbAdapter.KEY_WEIGHT)));
// this is the part that i need help with, I do not know how to restore
// the current items spinner value for reps and sets from the database.
spinReps.setSelection(exercise.getString(exercise.
getColumnIndexOrThrow(ExerciseDbAdapter.KEY_REPS)));
spinSets.setSelection(exercise.getString(exercise.
getColumnIndexOrThrow(ExerciseDbAdapter.KEY_SETS)));
}
I assume I need to use some sort of adapter to restore my list items along with the current value from the database, I am just not sure how.
Can someone please help me with this???
** BELOW IS MY SOLUTION**
I had to move my ArrayAdapters repsAdapter, spinAdapter out of my onCreate()
and then implement this new populateFields()
private void populateFields(){
if(mRowId != null){
Cursor exercise = mDbHelper.fetchExercise(mRowId);
// same as before
name.setText(exercise.getString(exercise.
getColumnIndexOrThrow(ExerciseDbAdapter.KEY_NAME)));
// get the string for sReps and sSets from the database
String sReps = exercise.getString(exercise.
getColumnIndexOrThrow(ExerciseDbAdapter.KEY_REPS));
String sSets = exercise.getString(exercise.
getColumnIndexOrThrow(ExerciseDbAdapter.KEY_SETS));
// use the strings to get their position in my adapters
int r = repsAdapter.getPosition(sReps);
int s = setsAdapter.getPosition(sSets);
// set their returned values to the selected spinner Items
spinReps.setSelection(r);
spinSets.setSelection(s);
// same as before
weight.setText(exercise.getString(exercise.
getColumnIndexOrThrow(ExerciseDbAdapter.KEY_WEIGHT)));
}
}
The way to set a Spinner to a value, not a position, depends on what adapter you are using.
ArrayAdapter, this one is easy:
int position = adapter.getPosition(exercise.getString(exercise.
getColumnIndexOrThrow(ExerciseDbAdapter.KEY_REPS)));
spinReps.setSelection(position);
SimpleCursorAdapter, this one is a little harder:
String match = exercise.getString(exercise.getColumnIndexOrThrow(ExerciseDbAdapter.KEY_REPS));
Cursor cursor = adapter.getCursor();
cursor.moveToPosition(-1);
int columnIndex = cursor.getColumnIndexOrThrow(ExerciseDbAdapter.KEY_REPS);
while(cursor.moveToNext()) {
if(cursor.getString(columnIndex).equals(match))
break;
}
spinReps.setSelection(cursor.getPosition());
If you are using a different type of adapter and can't modify the code above to fit it, let me know. Hope that helps!