-2

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

prabhu
  • 21
  • 4
  • "Address is null" is the text you are using if there is no image path? i mean with that you check if there is image or not? – V-rund Puro-hit Dec 12 '18 at 05:41
  • Can’t you just set the default image directly in the xml file and just change it if the adress is not null? – P.Juni Dec 12 '18 at 05:46
  • if no image is captured, " Address is null" is stored as Image address in database. again im checking that in on bindViewholder – prabhu Dec 12 '18 at 05:47
  • the if - else Statement inside OnBindViewHolder is not working as i expect.the If {} shows the desired result but the else part is not displaying the default image , instead the image view is empty – prabhu Dec 12 '18 at 05:51
  • Possible duplicate of [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Vladyslav Matviienko Dec 12 '18 at 06:28
  • that was an eye opener – prabhu Dec 12 '18 at 10:10

2 Answers2

0

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.

flashberry
  • 153
  • 11
0

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
V-rund Puro-hit
  • 5,518
  • 9
  • 31
  • 50