在Android中滚动时,Listview项目会发生变化

时间:2016-02-20 14:43:40

标签: android listview scroll

我有ListView小时的问题。

我使用ListView来显示我的警报及其weekdays并使用switch来显示它是否处于活动状态。但是,当我滚动weekdays并且switch正在更改时,只显示时间的textview项目不会更改。

如何解决这个问题?

public class AlarmListCustomAdapter extends BaseAdapter {

    private ArrayList<AlarmDomain> alarmDomains;
    private LayoutInflater layoutInflater;
    private Context context;
    private UserDomain userDomain;
    private AlarmListCustomAdapter alarmListCustomAdapter = this;
    public boolean[] firstChecked;

    public AlarmListCustomAdapter(Context context, ArrayList<AlarmDomain> alarmDomains, UserDomain userDomain, boolean[] firstCheck) {

        this.alarmDomains = alarmDomains;
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
        this.userDomain = userDomain;
        this.firstChecked = firstCheck;
    }

    @Override
    public int getCount() {
        return alarmDomains.size();
    }

    @Override
    public AlarmDomain getItem(int position) {
        return alarmDomains.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemViewType(int position) {
        // current menu type
        return position % 3;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.alarm_list_item, null);


             holder = new ViewHolder();
            holder.textView = (TextView) convertView.findViewById(R.id.textView4);
            holder.aSwitch = (Switch) convertView.findViewById(R.id.switch1);
            holder.Mo = (TextView) convertView.findViewById(R.id.Mo);
            holder.Tu = (TextView) convertView.findViewById(R.id.Tu);
            holder.We = (TextView) convertView.findViewById(R.id.We);
            holder.Th = (TextView) convertView.findViewById(R.id.Th);
            holder.Fr = (TextView) convertView.findViewById(R.id.Fr);
            holder.Sa = (TextView) convertView.findViewById(R.id.Sa);
            holder.Su = (TextView) convertView.findViewById(R.id.Su);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder)convertView.getTag();

        }
        try {

            Long time = alarmDomains.get(position).getTime();
            long h = time / 60;
            long m = time % 60;
            String hour = String.valueOf(h);
            if (h < 10) hour = "0" + hour;
            else {
                hour = String.valueOf(h);
            }
            String minute = String.valueOf(m);
            if (m < 10) minute = "0" + minute;
            else {
                minute = String.valueOf(m);
            }
            holder.textView.setText(String.valueOf(hour) + ":" + String.valueOf(minute));
            long[] weekdays1 = alarmDomains.get(position).getWeekDays();
            for (long x : weekdays1) {
                switch ((int) x) {

                    case 2:
                        holder.Mo.setTextColor(Color.parseColor("#0055a7"));
                        break;
                    case 3:
                        holder.Tu.setTextColor(Color.parseColor("#0055a7"));
                        break;
                    case 4:
                        holder.We.setTextColor(Color.parseColor("#0055a7"));
                        break;
                    case 5:
                        holder.Th.setTextColor(Color.parseColor("#0055a7"));
                        break;
                    case 6:
                        holder.Fr.setTextColor(Color.parseColor("#0055a7"));
                        break;
                    case 7:
                        holder.Sa.setTextColor(Color.parseColor("#0055a7"));
                        break;
                    case 1:
                        holder.Su.setTextColor(Color.parseColor("#0055a7"));
                        break;
                    default:
                        break;
                }
            }

            if (alarmDomains.get(position).getIsActive() == 1)
                holder.aSwitch.setChecked(true);
            else {
                holder.aSwitch.setChecked(false);
            }

            holder.aSwitch.setText(String.valueOf(position));
            holder.aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                    if (isChecked)
                        editIsActive(buttonView.getText().toString(), 1);
                    else
                        editIsActive(buttonView.getText().toString(), 0);

                }
            });

        } catch (Exception e) {
        }
        convertView.setTag(holder);
        return convertView;
    }

    public void editIsActive(String idx, int isActive) {

        AlarmDomain alarmDomain = alarmDomains.get(Integer.valueOf(idx));
        alarmDomain.setIsActive(isActive);
        EditAlarmController editAlarmController = new EditAlarmController();
        editAlarmController.isActive(context, alarmDomain, alarmListCustomAdapter);
    }

    public void onResultIsActive(Object object) {

    }

    private class ViewHolder {

         TextView textView;
         Switch aSwitch;
         TextView Mo;
         TextView Tu;
         TextView We;
         TextView Th;
         TextView Fr;
         TextView Sa;
         TextView Su;
    }

}

2 个答案:

答案 0 :(得分:1)

如果convertView != null您应该首先将日期颜色重置为默认值。

答案 1 :(得分:1)

当我在getView()中有很多条件时,我的listView适配器总是遇到这个问题。首先建议在getView()中使用最小条件。我使用的另一个解决方案是将getView函数中的所有视图持有者视图初始化为最终变量,并在每个条件上添加notifyDataSetChange();大多数时候这样做可以解决我的问题。 而不是

if(converView == null) {
  //inflate view
}

仅使用此

{{1}}

没有其他条件。如果条件还要初始化此convertView以外的所有textView和其他视图。

注意:这不是最佳解决方案,但这样做可以在大多数时间解决我的问题