Change the DrawableLeft property of a TextView

0

I have a SimpleAdapter that I use in a listview to show data that comes from the database. Follow the code below

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. Is it possible to do that?

    
asked by anonymous 03.07.2017 / 17:09

1 answer

1

You can use the getView method on your SimpleAdapter and the setCompoundDrawablesWithIntrinsicBounds setting in the first argument the specific drawable , which in your case would be R.drawable.tic14 . This will ensure that the drawable will be inserted to the left of your TextView . Here's 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;
    };
};
    
03.07.2017 / 18:00