ListView contains 2 textViews. Custom Adapter:

@Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; if (convertView == null){ holder = new ViewHolder(); convertView = layoutInflater.inflate(R.layout.settings_row_online_entry,null); holder.tv1 = (TextView)convertView.findViewById(R.id.tv_onlineentry1); holder.tv2 = (TextView)convertView.findViewById(R.id.tv_onlineentry2); holder.tv1.setTextColor(Color.GRAY); //holder.tv2.setTextColor(Color.BLACK); if ( holder.tv2.getText().equals("")){ holder.tv1.setTextColor(Color.GREEN); } else { holder.tv1.setTextColor(Color.GRAY); } convertView.setTag(holder); } else { holder = (ViewHolder)convertView.getTag(); } holder.tv1.setText(myList.get(position).getOnlineEntryDate()); holder.tv2.setText(myList.get(position).getOnlineEntryText()); return convertView; } 

The list contains 5 entries. Now all the color is loaded after building a view. Tell me how to make the text color in the text change dynamically after making a change to the text I twist? Is it right to do this in the adapter? or need to make changes to the activit?

    2 answers 2

    The principle is as follows:

    1. In the getView adapter, first form the holder
    2. Only then, with the existing holder, change the properties of the markup elements

    In this case, if we change something in the adapter data, the markup depends on, then after calling notifyDataSetChanged() adapter will redraw the elements according to the new conditions.

     @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; if (convertView == null){ holder = new ViewHolder(); convertView = layoutInflater.inflate(R.layout.settings_row_online_entry,null); //только находим элементы в разметке, но не меняем их convertView.setTag(holder); } else { holder = (ViewHolder)convertView.getTag(); } //тут назначаем свойства разметке в зависимости от данных return convertView; } 
    • one
      What you need! Thanks - Ivan Vovk

    If the ListView has 5 entries, is it worth it to use it at all? All entries will be constantly on the screen. may be worth reconsidering the layout? Then programmatically in the activation will change the color of the buttons. The adapter serves all the same to reconcile the data between the source and the display. The very display, layout, color design is better to entrust to activate.

    • Through the activation, I did not find how to change the data of the text I see in the adapter. - Ivan Vovk