简体   繁体   English

notifyDataSetChanged() 正在取消更改 ListView 中的 setColor

[英]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.与主题一样,当我在已更改的单元格中使用adapter.notifyDataSetChanged()文本颜色时,将设置回默认值。 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.我还要补充一点,这部分代码在其他线程中处于循环中,因为单元格的值每 30 秒更新一次。 Here is method getViewByPosition:这是 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);首先这一行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 .没有任何意义,因为通过手动调用,您将始终在内部为列表视图创建和膨胀新行,但此视图从未在您的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 .看到你总是传递第二个参数convertView null 所以在内部这个方法将创建新视图但这个视图永远不会在你的ListView Tip 1. Don't call getView() method yourself Tip 1. 不要自己调用getView()方法

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.您可能知道,当您正确使用ViewHolder模式时, ListView在内存中存储的行/视图仅与它们在屏幕上可见的行数/视图一样多。

So for now you are setting color for every row that is visible and even those not visible that really not exist in ListView .因此,现在您正在为可见的每一行设置颜色,甚至为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.提示 2.对任何行进行着色或更改任何内容的最佳方法是在getView()方法实现中执行此操作,具体取决于您的适配器项状态。 Don't do it from outside because it looks like some kind of a hack or wrong architecture.不要从外面做,因为它看起来像是某种黑客或错误的架构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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