How to change Textview of a View in another View?

Asked

Viewed 1,344 times

1

I’m facing the problem, below:

I have 2 xml layout: list_single.xml and tela_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 field KEY_IAESTORNO, only 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 didn’t work, it is null, precisely because it is reading the layout-> tela_authorization.xml and the field is in list_single.xml

I’m using an Adapter on 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);

    }

Someone knows how to do?

  • Rogers, your question is a little confusing, so I understand you want to retrieve the View main of its Activity right (if not, try to clarify a little better)? if you do: findViewById(android.R.id.content) within your Activity or getView in his Fragment, it returns the root of its View. From that View you can search for the other elements. If Textview is inside a ListView you need: Or access the data that feeds the list item (by Adapter) or to seek a child, using the ViewGroup.childAt.

  • @Wakim edited, you can understand?

  • I think I understand, you want to change only one, given a position, or you want to change all (a View that is within the layout of the ListView that has id KEY_IAESTORNO)?

  • just the color of a @Wakin,

  • Only the color of KEY_IAESTORNO understood?

  • Got it, I’m putting together an answer with a few details.

Show 1 more comment

1 answer

2


If you need to customize the View of an item of ListView create a subclass of SimpleAdapter and include the customization logic in 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 much more: modify visibility, add EventListeners, popular Views more complex.

Now, if the list has been built and some event occurred and need to change the item’s color punctually.

Two cases may occur:

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

  2. The element is visible, in which case there are two solutions:

    1. Change your template and use the notifyDataSetChanged, this will force the updating of items that are visible on the screen.

    2. Mark the Views that make up the layout of the ListView with their position and iterate on the children of ListView searching for that tag. When retrieving the target child, change its layout. The code would look like this.

      Altering the Adapter to mark the View with your 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 the 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
          }
      }
      
  • Wakim couldn’t do it, I have to create a class to do it?

  • Using the anonymous class he does not tag? Which part did not work?

Browser other questions tagged

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