Listview images and quantity - Android

Asked

Viewed 458 times

-1

Next, I have 2 questions that relate, so I’ll ask on 1 topic.

Well, I use this query to return a list of strings with product names.

public List<String> getMedico() {
    List<String> list = new ArrayList<>();
    Cursor cursor = database.rawQuery("SELECT nome FROM Armas Where Classe like 'médico' ORDER BY nome ASC", null);
    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
        list.add(cursor.getString(0));
        cursor.moveToNext();
    }
    cursor.close();
    return list;
}

When I click on a list item it opens a new detail page, which shows all the other attributes of the product. Then, on this page, I recover the image by passing as parameter the name of the clicked item.

 public byte[] getImage(String name) {
    byte[] data = null;
    Cursor cursor = database.rawQuery("SELECT imagem FROM Armas WHERE nome = ?", new String[]{name});
    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
        data = cursor.getBlob(0);
        break;
    }
    cursor.close();
    return data;
}

Blz, it works smoothly. Now, what I need: Return an "image list". Instead of returning the name, I want to return the images and place them in a listview. But I have no idea how to do it. But blz, in doing so, takes the second question: When it is ready, it will return 300 images of 15kb each. Overall it gives almost 4mb. Return this much image can weigh in the application when opening this screen? And if you have 3 fragments returning 300 images each?

Ah, I want to do in sqlite and not put in drawables pq beyond the image will have name and description. Or it would be better to put in drawables, put in sqlite only the image path and return it somehow?

3 answers

2

Guy you could first make a DTO Object with the attributes name and Image, saved in Sqlite. In this, to limit, it seeks to use an SQL where you limit the results by pages, type:

SELECT nome, imagem FROM Armas Where Classe like 'médico' ORDER BY nome LIMIT 5, 10

This will return 5 from Row 10. When you need more images, just change the value from 10 to 15, 20.. Read more about this here.

As you will use a large number of results, use a Recyclerview. Using Scroll Listener, you can recover more results when the user gives scroll in the list. Read about this here.

And so, when the user clicks on an item in the list, just take the name saved in the DTO Object and open the Fragment.

I hope this helps you, hugs!

0

Do not waste time trying to put images in SQLITE. You can do, but it loses type 99% of the resolution, making the image useless. Either you save to drawable and insert the path into db, type R.drawable.img01 (which is nothing more than an int), or move the images up some online server and save the link to db. In both cases, you will need to use a library to load the images, such as Glide, for example. It is very simple to use. If you are on Adapter, instead of imgView.setResource...., do Glide.with(contexto).load(R.drawable.img1).into(imgView); or Glide.with(contexto).load("www.meuserv.com/img01").into(imgView);

  • Well, I’m currently doing with sqlite, and since the images are already small, it doesn’t seem to have lost quality, but it didn’t answer what I asked

  • The first question: "Only I have no idea how to do it" Answer: "...you will have to use a library to load the images, like Glide, for example. It’s very simple to use..." The second question: "Can return this much image can weigh on the application when opening this screen?" Answer: "... In both cases, you will have to use a library..." Will weigh and crash the app. You’ll have to use a lib to manage image loading.

  • Starting, of course, from the principle that you already have a string arrayList with the image links or an int array, in which case it would be the drawable.

  • That’s why I told you to save the images as strings (links from an external server) or as ints (which is the drawable) so you don’t have difficulties at the time of the query. I think waste of time inserting images directly in db, pq have to convert and disconnect all the time...

0

About images in databases:

I do not recommend saving images within the database, for query performance reasons and we are limited when working with more resolutions - and as we know, devices have limited resources.

When we save the image in the bank, and we need to recover, we have to worry about translating text to image, and if we want to include, the same need arises. I always recommend using the app cache folder ('/data/data/br.com.seuapp/images') - external storage - for this you can upload the images dynamically, using your app to search on the server.

There is an Android Developer (Google) page that addresses how display images efficiently, on this page you can read a little about how to process, handle and display bitmaps.

Remembering that: If we beat a photo in a resolution of: 5 megapixels (2592x1936 pixels), and we want to load/display, we will use in all, 19mb, breaking the limit of preloading of many devices.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.