简体   繁体   中英

notifyDataSetChanged() is canceling changing setColor in ListView

as in topic, when I use adapter.notifyDataSetChanged() text color in a cell which i have already changed is seting back to default value. I don't know why it happens im putting here me method for changing color:

for(int l=0;l<list.size();l++){
System.out. println("kolorujemy! "+ list.size() );
LinearLayout root = (LinearLayout) getViewByPosition(l,listView);
((TextView) root.findViewById(R.id.wartosc_calosci)).setTextColor(Color.YELLOW);

I would also add that this part of code is in loop in other thread because the vaules of the cells is updating every 30 seconds. Here is method getViewByPosition:

public View getViewByPosition(int pos, ListView listView) {
        final int firstListItemPosition = listView.getFirstVisiblePosition();
        final int lastListItemPosition = firstListItemPosition + listView.getChildCount();

        if (pos < firstListItemPosition || pos > lastListItemPosition ) {
            return listView.getAdapter().getView(pos, null, listView);
        } else {
            final int childIndex = pos - firstListItemPosition+1;
            return listView.getChildAt(childIndex);
        }
    }

getView:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
ListViewHolder listViewHolder;
if(convertView == null){
    listViewHolder = new ListViewHolder();
    convertView = activity.getLayoutInflater().inflate(R.layout.lista_wlasnych_spolek, null);
    listViewHolder.txtFirst = (TextView) convertView.findViewById(R.id.nazwa_spolki);
    listViewHolder.txtSecond = (TextView) convertView.findViewById(R.id.wartosc_akt);
    listViewHolder.txtThird = (TextView) convertView.findViewById(R.id.wartosc_kupna);
    listViewHolder.txtFourth = (TextView) convertView.findViewById(R.id.wartosc_calosci);
    convertView.setTag(listViewHolder);
} else {
    listViewHolder = (ListViewHolder) convertView.getTag();
}

First of all this line return listView.getAdapter().getView(pos, null, listView); makes no sense, because with this call by hand you will internally always create and inflate new row for the list view but this view is never used within your ListView . See that you are always passing second parameter convertView null so internally this method will create new view but this view will be never used inside your ListView . Tip 1. Don't call getView() method yourself

As you may know ListView stores in memory only as many rows/view as they are visible on the screen when you use ViewHolder pattern properly.

So for now you are setting color for every row that is visible and even those not visible that really not exist in ListView .

Tip 2. Best way to color or change anything about any of your rows, is to do it just inside getView() method implementation depend on your adapter item state. Don't do it from outside because it looks like some kind of a hack or wrong architecture.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM