Image in listview with Sqllite / Drawable

Asked

Viewed 757 times

1

I have an app that does a corporate phone search on a Sqllite database. It’s working normally, but I need to get some image into some of these companies. The database field would be only the image name and the image would be in the res/drawable folder... I won’t use field blob. How do I show this image in imageview in my results listview. See my code below

String mostraTexto = params.getString("palavrachave");  // parametro para busca

    Cursor cursor = db.rawQuery("select nome,liberado,imagem,cidade,CIDADES._id,telefone,ddd,tipo,cid_nome,TELEFONES._id from TELEFONES INNER JOIN CIDADES ON TELEFONES.cidade = CIDADES._id where telefone like '%" + mostraTexto + "%' or nome like '%" + mostraTexto + "%' order by cid_nome,nome", null);


    String[] from = {"nome","telefone","cid_nome","ddd"};
    int[] to = {R.id.txt_nome, R.id.txt_telefone, R.id.txt_cidade, R.id.txt_ddd, R.dr};

    //vamos contas os resultados
    int id[]=new int[cursor.getCount()];
    if (cursor.getCount() < 1)
    {           
     AlertDialog.Builder dialogo = new AlertDialog.Builder(Resultado.this);
     dialogo.setTitle("Aviso");
     dialogo.setMessage("Nenhuma resultado na sua pesquisa!");
     dialogo.setNeutralButton("Ok", null);
     dialogo.show();
    }
    if (cursor.getCount() > 0) 
    {

    android.support.v4.widget.SimpleCursorAdapter ad =
            new android.support.v4.widget.SimpleCursorAdapter(
                    getBaseContext(),R.layout.buscar_ltw,cursor, from, to, 0);

    ListView ltw_listar = (ListView)findViewById(R.id.ltw_listar);

        ltw_listar.setAdapter(ad);
  • The image name is the resource id or the "filename"? Couldn’t you save the id or URI to the database with the absolute path? Why Simplecursoradapter treats column value as the drawable id in that folder and URI.

  • Wakim Recover the image ID I can easily. I’m just not being able to send it to the Imageview field present in Listwiew. Analyze my code, where I could put the command to send the image to imageView.

1 answer

1

use this snippet to get the image id

   String mDrawableName = "myImageName";
int id = mContext.getResources().getIdentifier(mDrawableName , "drawable", mContext.getPackageName());

and to set the image is like this

imageview.setImageResource(id);

Browser other questions tagged

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