简体   繁体   中英

Listview items change when scrolling in Android

I have a problem with ListView for hours.

I use ListView to show my alarms and its weekdays and use switch to show if it is active. However, when I scroll my weekdays and switch is changing, only textview item which show time doesn't change.

How to solve this problem ?

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;
    }

}

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

I always face this issue with my listView adapter, when I have alot of conditions in getView(). First suggestions is to use minimum conditions in getView(). The other solution which I use is to initialize all view holder views in getView function as final variable and on every condtion add notifyDataSetChange(); Most of the time doing this solves my problem. And in place of

if(convertView == null) {
 //your code
} else {
  convertView.getTag()
}

Use just this

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

No else condition. Also initialize your all textViews and other views outside of this convertView if condition.

Note: This is not the best solution but doing this solves my problem most of the time

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