Change the Drawableleft property of a Textview

Asked

Viewed 239 times

0

I have a Simpleadapter that I use in a listview to show data coming from the database. Follows the code

Map<String, String> map;
for(int i = 0;i < buscaCombustivel.getCount();i++) {
    map = new HashMap<String, String>();
    map.put("idItem", buscaCombustivel.getString(buscaCombustivel.getColumnIndex("id")));
    map.put("text1", buscaCombustivel.getString(buscaCombustivel.getColumnIndex("dataEntrada")));
    map.put("desc","Litros: " + buscaCombustivel.getString(buscaCombustivel.getColumnIndex("litrosEntrada")) + " | Valor unitário: " +
            buscaCombustivel.getString(buscaCombustivel.getColumnIndex("valorUnitarioEntrada")) + " | Valor total: " +
            buscaCombustivel.getString(buscaCombustivel.getColumnIndex("valorTotalEntrada")) + "\n");
    list.add(map);
    buscaCombustivel.moveToNext();
}
SimpleAdapter adapterDropDown = new SimpleAdapter(getApplicationContext(), list, R.layout.custom_list, new String[] { "text1", desc", "idItem" }, new int[] { R.id.text1, R.id.desc, R.id.idItem });
listDados.setAdapter(adapterDropDown);

R.id.text1, used in Simpleadapter, has a property called Drawableleft in its xml. I needed to change this property depending on a certain value that comes from the database. It is possible to do this?

  • Yes! Usually you put an image in Drawableleft. A database image url is coming up?!

  • No, it’s a drawable even... android:drawableLeft="@drawable/tic14"

  • Why don’t you create a custom Adapter and define the xml you want? It’s pretty simple.

1 answer

1


You can use the method getView in his SimpleAdapter and the setCompoundDrawablesWithIntrinsicBounds defining in the first argument the drawable which in its case would be the R.drawable.tic14. This will ensure that the drawable shall be inserted to the left of its TextView. Take an example:

SimpleAdapter adapterDropDown = new SimpleAdapter(getApplicationContext(), list,
    R.layout.custom_list, new String[] {
        "text1",
        desc,
        idItem
    }, new int[] {
        R.id.text1, R.id.desc, R.id.idItem
    }) {

    public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);
        TextView text1 = (TextView) view.findViewById(android.R.id.text1);

        int imgResource = R.drawable.tic14;
        text1.setCompoundDrawablesWithIntrinsicBounds(imgResource, 0, 0, 0);

        return view;
    };
};

Browser other questions tagged

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