我有一個ListView
幾個小時的問題。在Android中滾動時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;
}
}
謝謝你的回答。好吧,我會照你說的去做。我第一次使用它,這就是爲什麼我對它感到困惑的原因 – HesenliEldar
當我滾動這個時,它使交換機停止如何解決它? – HesenliEldar
你需要將開關的某個值存儲在哪裏。您可以在列表中爲切換活動或不活動創建一個布爾變量,並基於該變量您可以更改您的開關 – Arshad