【问题标题】:ListView item background color updateListView 项目背景颜色更新
【发布时间】:2016-03-13 11:20:25
【问题描述】:

我创建了包含动态项目的列表视图,其中包含两个项目:CheckBox 和 TextView。当用户点击项目内的复选框时,它会更新 textView 的背景颜色。它工作正常,直到此项被滚动到列表的用户空间可见并且我收到错误:com.viewactivities.AddNewPosition.changeTextColorWhenCheckBoxIsClicked(AddNewPosition.java:183) 处的 java.lang.NullPointerException。

我还使用一种负责更改颜色的方法实现了接口。我观察到,当 listView 中的项目不再可见时,也会触发方法“changeTextColorWhenCheckBoxIsClicked”(这会导致错误)。有人可以帮忙吗?

包含列表视图和更改颜色方法的类的代码:

@Override
public void changeTextColorWhenCheckBoxIsClicked(int position) {
    // TODO Auto-generated method stub

    listView.getChildAt(position).findViewById(R.id.listViewItemText).setBackgroundColor(Color.BLUE);

}

以及来自 listView 适配器的代码:

@Override
    public View getView(int position, View convertView, ViewGroup parent) 
    {
          ViewHolder viewHolder = null;
          globalPosition = position;

        if(convertView==null){

             LayoutInflater inflater =  ((AddNewPosition) mContext).getLayoutInflater();    
             convertView = inflater.inflate(layoutResourceId, parent, false);

             viewHolder = new ViewHolder();
                viewHolder.text = (TextView) convertView.findViewById(R.id.listViewItemText);
                viewHolder.checkbox = (CheckBox) convertView.findViewById(R.id.listViewItemCheckBox);

                viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {


                        int getPosition = (Integer) buttonView.getTag();  // Here we get the position that we have set for the checkbox using setTag.
                        listOfObjects.get(getPosition).setCheckBoxEnabled(buttonView.isChecked()); // Set the value of checkbox to maintain its state.   changeTextColorWhenCheckBoxIsClicked(getPosition);
                    }
                });
                convertView.setTag(viewHolder);
                convertView.setTag(R.id.listViewItemText, viewHolder.text);
                convertView.setTag(R.id.listViewItemCheckBox, viewHolder.checkbox);
                } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            viewHolder.checkbox.setTag(position); // This line is important.

            viewHolder.text.setText(listOfObjects.get(position).getTextFromListViewItemObject());
            viewHolder.checkbox.setChecked(listOfObjects.get(position).getIsCheckedBoxEnabled());
            //viewHolder.text.setBackgroundColor(Color.RED);

        //ListViewItemObject tmpObject = listOfObjects.get(position);

        //TextView listItemText = (TextView) convertView.findViewById(R.id.listViewItemText);
        //CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.listViewItemCheckBox);
        //checkBox.setChecked(false);

        //listItemText.setText(tmpObject.getTextFromListViewItemObject());

         return convertView;
    }


    @Override
    public void changeTextColorWhenCheckBoxIsClicked(int position) {
        // TODO Auto-generated method stub

        ((AddNewPosition) mContext).changeTextColorWhenCheckBoxIsClicked(position);

    }

【问题讨论】:

    标签: java android listview android-listview


    【解决方案1】:

    ListView 重复使用它的视图,所以不用 setTag() 而是使用一个列表来保持位置。

    viewHolder.checkbox.setTag(position); // This line is important.
    

    这样的—— Android Listview Scrolling challenge for row elements

    【讨论】:

    • 我已经阅读了您的答案,但我仍然感到困惑。你能在我的代码中显示应该改变什么吗?
    猜你喜欢
    • 2012-03-10
    • 1970-01-01
    • 1970-01-01
    • 2011-09-19
    • 2011-01-14
    • 2013-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多