How to change TextView from a View in another View?

1

I'm facing the problem, below:

I have 2 xml layout: list_single.xml and screen_authorization.xml I have 1 class: Authorization.class

In the Authorization class I have:

  setContentView(R.layout.tela_autorizacao);

But I want to change the color of the KEY_IAESTORNO field, but this field is in list_single.xml, I tried to do this:

  teste = (TextView) findViewById (R.id.KEY_IAESTORNO);

  teste.setTextColor(Color.BLUE);

Of course it did not work, it's null, just because it's reading layout-> and the field is in list_single.xml

I'm using an adapter in the list:

... {

        ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
        NodeList nl2 = doc.getElementsByTagName(KEY_PAI);

        list = (ListView) findViewById(R.id.list);
        // looping through all item nodes <item>
        for (int i = 0; i < nl2.getLength(); i++) {
            // creating new HashMap
            HashMap<String, String> map = new HashMap<String, String>();
            Element e2 = (Element) nl2.item(i);

            // adding each child node to HashMap key => value
            map.put(KEY_IAAUTNUM,
                    "Nº Autorização: " + parser.getValue(e2, KEY_IAAUTNUM));
            map.put(KEY_IAESTORNO, sit + parser.getValue(e2, KEY_IAESTORNO));
            map.put(KEY_IAENTNOME,
                    "Entidade: " + parser.getValue(e2, KEY_IAENTNOME));
            map.put(KEY_IAASSNOME,
                    "Associado: " + parser.getValue(e2, KEY_IAASSNOME));
            map.put(KEY_IAASSPORT,
                    "Portador: " + parser.getValue(e2, KEY_IAASSPORT));
            map.put(KEY_IACARTFMT,
                    "Cartão: " + parser.getValue(e2, KEY_IACARTFMT));
            map.put(KEY_IAPARPRIM,
                    "Prim.Parc.: " + parser.getValue(e2, KEY_IAPARPRIM));
            map.put(KEY_IAPARCOUT,
                    "Demais: " + parser.getValue(e2, KEY_IAPARCOUT));
            map.put(KEY_IAQTDPAR,
                    "Nº Parc.: " + parser.getValue(e2, KEY_IAQTDPAR));
            map.put(KEY_IAVALOPE,
                    "Valor Total: " + parser.getValue(e2, KEY_IAVALOPE));
            map.put(KEY_IANOMMES,
                    "Mês Venc.: " + parser.getValue(e2, KEY_IANOMMES));
            map.put(KEY_IAESTNOME,
                    redetxt + parser.getValue(e2, KEY_IAESTNOME));
            map.put(KEY_IAESTCNPJ,
                    cnpjtxt + parser.getValue(e2, KEY_IAESTCNPJ));
            map.put(KEY_IADATA, "Data: " + parser.getValue(e2, KEY_IADATA));
            map.put(KEY_IAHORA, "Hora: " + parser.getValue(e2, KEY_IAHORA));
            // adding HashList to ArrayList
            NUMAUT2 = parser.getValue(e2, KEY_IAAUTNUM);
            SITU = parser.getValue(e2, KEY_IAESTORNO);
            menuItems.add(map);
        }

        if (SITU.equals("ESTORNADA")) {

            btnEstornar.setVisibility(View.INVISIBLE);


        } else {

            btnEstornar.setVisibility(View.VISIBLE);
        }

        ListAdapter adapter = new SimpleAdapter(this, menuItems,
                R.layout.list_single_xml, new String[] { KEY_IAAUTNUM,
                        KEY_IAESTORNO, KEY_IAENTNOME, KEY_IAASSNOME,
                        KEY_IAASSPORT, KEY_IACARTFMT, KEY_IAPARPRIM,
                        KEY_IAPARCOUT, KEY_IAQTDPAR, KEY_IAVALOPE,
                        KEY_IANOMMES, KEY_IAESTNOME, KEY_IAESTCNPJ,
                        KEY_IADATA, KEY_IAHORA }, new int[] {
                        R.id.KEY_IAAUTNUM, R.id.KEY_IAASSNOME,
                        R.id.KEY_IAENTNOME, R.id.KEY_IAASSNOME,
                        R.id.KEY_IAASSPORT, R.id.KEY_IACARTFMT,
                        R.id.KEY_IAPARPRIM, R.id.KEY_IAPARCOUT,
                        R.id.KEY_IAQTDPAR, R.id.KEY_IAVALOPE,
                        R.id.KEY_IANOMMES, R.id.KEY_IAESTNOME,
                        R.id.KEY_IAESTCNPJ, R.id.KEY_IADATA,
                        R.id.KEY_IAHORA

                });

        TextView estorno =(TextView) adapter.getItem(2);
        estorno.setTextColor(Color.RED);

        list.setAdapter(adapter);

    }

Does anyone know how to do it?

    
asked by anonymous 22.08.2014 / 16:09

1 answer

2

If you need to customize the View of an item from ListView create a subclass of SimpleAdapter and add the customization logic to it. For example:

public class MeuSimpleAdapter extends SimpleAdapter {
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);

        // Sua logica de customizacao, recuperando outras Views dentro do item.
        // Alem disso, de uma olhada no padrao View Holder,
        // ajuda a dar fluidez na sua lista.

        TextView text = view.findViewById(R.id.KEY_IAESTORNO);

        int atributoDoModelo = (...);

        // Buscar no modelo a informacao sobre a cor desse item
        text.setTextColor(atributoDoModelo);
        // ou
        text.setBackgroundColor(atributoDoModelo);

        return view;
    }
}

With this concept you can do a lot more: modify visibility, add EventListeners , popular Views more complex.

Now if the list has already been built and some event has occurred and needs to change the color of the item in a timely fashion .

Two cases can occur:

  • The item is not visible, so there is nothing to do. Just change some attribute of your model that Adapter uses to color the element.

  • The element is visible, in this case there are two solutions:

  • Changing your template and using notifyDataSetChanged will force you to update the items that are visible on the screen.

  • Check the Views that compose the item layout of ListView with its position and iterate over the children of ListView by searching for this tag. When retrieving the target child, change your layout. The code would look like this.

    Changing Adapter to mark View with its position:

    // Crio uma subclasse anônima do SimpleAdapter,
    // pode ser em um arquivo separado se achar melhor.
    ListAdapter adapter = new SimpleAdapter(...) {
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = super.getView(position, convertView, parent);
    
            // "Tageio" a View que representa o item com a posição dela no 'Adapter'.
            view.setTag(R.layout.list_single_xml, position);
    
            return view;
        }
    };
    

    Code to change the color of TextView :

    public void alterarCorDoItem(int position, int cor) {
        ListView list = findViewById(R.id.list);
        int size = list.getChildCount();
        boolean achei = false;
    
        // Itero sobre os filhos que estão visíveis
        for(int i = 0; i < size; ++i) {
            View child = list.childAt(i);
            Integer adapterPosition = (Integer) child.getTag(R.layout.list_single_xml);
    
            // Achei a view alvo
            if(adapterPosition != null && adapterPosition.intValue() == position) {
    
                TextView textView = (TextView) view.findViewById(R.id.KEY_IAESTORNO);
    
                textView.setTextColor(cor);
                // ou
                textView.setBackgroundColor(cor);
    
                achei = true;
                break;
            }
        }
    
        if(! achei) {
            // Posicao nao esta visivel, alterar modelo para quando
            // ficar visivel, ficar com a cor correta
        }
    }
    
  • 22.08.2014 / 20:54