Good day . Already the second day I am trying to deal with this issue. There should be a list with a different color highlighting the best offer from the default.

Must be so must be

But as a result, everything gets in the way. ![but

What could be the problem ? Adapter code:

@Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View view, ViewGroup parent) { DetailInfo detailInfo = (DetailInfo) getChild(groupPosition, childPosition); if (view == null) { LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); view = infalInflater.inflate(R.layout.child_row, null); } TextView bankName = (TextView) view.findViewById(R.id.bankName); bankName.setText(detailInfo.getName().trim()); TextView dateOfUpdate = (TextView) view.findViewById(R.id.DateofUpdate); dateOfUpdate.setText(detailInfo.getDate().trim()); TextView currencySell = (TextView) view.findViewById(R.id.CurrencySell); currencySell.setTag(childPosition); currencySell.setText(detailInfo.getSell().trim()); TextView currencyBuy = (TextView) view.findViewById(R.id.CurrencyBuy); currencyBuy.setTag(childPosition); currencyBuy.setText(detailInfo.getBuy().trim()); if(detailInfo.isBestBuyDeal()) { currencyBuy.setTextColor(Color.rgb(0,150,0)); } if(detailInfo.isBestSellDeal()) { currencySell.setTextColor(Color.rgb(0,150,0)); } return view; } 

DetailInfo:

 public class DetailInfo implements Comparable<DetailInfo>{ private String name = ""; private String date =""; private String buy = ""; private String sell = ""; private Map.Entry<Boolean,Boolean> bestDeal = null; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDate() { return date; } public void setDate(String date) { this.date = date; } public String getBuy() { return buy; } public void setBuy(String buy) { this.buy = buy; } public String getSell() { return sell; } public void setSell(String sell) { this.sell = sell; } public Map.Entry<Boolean,Boolean> getBestDeal() { return bestDeal; } public void setBestDeal(Map.Entry<Boolean,Boolean> bestDeal) { this.bestDeal = bestDeal; } public Boolean isBestBuyDeal() { return bestDeal.getKey(); } public Boolean isBestSellDeal() { return bestDeal.getValue(); } @Override public int compareTo(DetailInfo another) { return this.getName().compareTo(another.getName()); } 

}

child_row:

 <?xml version="1.0" encoding="utf-8"?> 

 <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/bankName" android:layout_width="wrap_content" android:layout_height="40dp" android:paddingLeft="50dp" android:textAppearance="?android:attr/textAppearanceMedium" android:layout_alignParentTop="true" android:layout_weight="1"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/DateofUpdate" android:layout_weight="1"/> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1"> <TextView android:text="Покупка:" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/BuyAmount" android:layout_weight="1"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/CurrencyBuy" android:layout_alignBottom="@+id/BuyAmount" android:layout_toEndOf="@+id/BuyAmount" android:layout_toRightOf="@+id/BuyAmount" android:layout_weight="1"/> <TextView android:text="Продажа:" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/Sell" android:layout_alignBottom="@+id/CurrencyBuy" android:layout_toRightOf="@+id/CurrencyBuy" android:layout_toEndOf="@+id/CurrencyBuy" android:layout_weight="1"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/CurrencySell" android:paddingLeft="10dp" android:layout_alignBottom="@+id/CurrencyBuy" android:layout_toRightOf="@+id/Sell" android:layout_toEndOf="@+id/Sell" android:layout_weight="1"/> </LinearLayout> 

Thanks for attention

  • For which element in the adapter has the color not changed, what should be the color and what does the test method look like? - Andrew Grow
  • For TextView, id = BuyAmount & SellAmount in child_row and id = currencyBuy & currencySell in the adapter. There is also a check. - Wiks
  • You need to set up elsewhere for your checks, in which to set the deafult color. Your views are reused, and the color remains from the previous item. - Yura Ivanov
  • if I push else, everything changes in default - Wiks
  • "if I push else, then everything changes in default" - show the code - Yura Ivanov

1 answer 1

View different lists as a rule are reused. If you change something only under some condition, then you need to change it back if this condition is not fulfilled:

 if (detailInfo.isBestSellDeal()) { currencySell.setTextColor(Color.rgb(0,150,0)); } else { currencySell.setTextColor(Color.rgb(0,0,0)); } 

PS colors, indents, text and in general all that can be put into resources, it is better to put into resources.