Insert image in ListView via JSON + SQL

1

I created a code that perfectly matches the results via JSON from my database and populates my ListView with Strings, but I'd like the database to bring it from the "image" column I created, the image-related address : link ) and the ListView loaded this image into it through the address recorded in the database, the addresses of the images I've tried in several ways, but I could not, I'll post the code that fills my ListView with only strings, because this code is working correctly:

private void showEmployee(){

JSONObject jsonObject = null;

ArrayList> list = new ArrayList>();

try { jsonObject = new JSONObject(JSON_STRING); JSONArray result = jsonObject.getJSONArray(Config.TAG_JSON_ARRAY);

for(int i = 0; i<result.length(); i++){ JSONObject jo = result.getJSONObject(i); String id = jo.getString(Config.TAG_ID); String name = jo.getString(Config.TAG_NAME); String salary = jo.getString(Config.TAG_SAL); String image = jo.getString(Config.TAG_IMAGE); HashMap<String, String> employees = new HashMap<>(); employees.put(Config.TAG_ID,id); employees.put(Config.TAG_NAME,name); employees.put(Config.TAG_SAL,salary); employees.put(Config.TAG_IMAGE,image); list.add(employees); } } catch (JSONException e) { e.printStackTrace(); } //ListAdapter SimpleAdapter adapter = new SimpleAdapter( ViewAllEmployee.this, list, R.layout.list_item, new String[]{Config.TAG_ID,Config.TAG_NAME, Config.TAG_SAL, Config.TAG_IMAGE}, new int[]{R.id.id, R.id.name, R.id.salary, R.id.image} ); listView.setAdapter(adapter); }

    
asked by anonymous 31.10.2016 / 12:29

1 answer

1

Well, as it was not specified I would recommend that you do this using some of the existing tools (including Glide is maintained by Google)

To use Glide you need to add dependency on it.

In the buid.gradle file of your APP (There is one in the project root, add in the APP). Find the part of dependencies and add this line:

dependencies {
  compile 'com.github.bumptech.glide:glide:3.7.0'
  compile 'com.android.support:support-v4:19.1.0' // <---- Não precisa, mas é recomendado utilizar uma RecyclerView ao invés da ListView
}

Within your adapter, in the getView method you do something like:

  @Override 
  public View getView(int position, View recycled, ViewGroup container) {
  final ImageView minhaImageView;
  if (recycled == null) {
    minhaImageView= (ImageView) inflater.inflate(R.layout.minha_image_view, container, false);
  } else {
    minhaImageView= (ImageView) recycled;
  }

  String url = minhaListaDeUrls.get(position);

  Glide
    .with(meuContexto)  // <------ É necessário passar um contexto para ele, pode ser seu Fragment ou um Context
    .load(url)   // <--- Sua Url
    .centerCrop()  // <---- Estético, é o método como a imagem será posicionada na view
    .placeholder(R.drawable.loading_spinner)  // <---- Você pode especificar um Drawable para aparecer enquanto carrega
    .crossFade()    // <----- Animaçãozinha :)
    .into(minhaImageView);   // <----- Onde a imagem será carregada.

  return minhaImageView;
}

Note that I have implemented a list with ImageView only for the purpose of understanding, what is important is the part Glide.... the rest is just for you to define in which ImageView you will load the image, adapt to your need

    
31.10.2016 / 13:45