gpt4 book ai didi

android - 在 Android 中滚动时 ListView 项目发生变化

转载 作者:行者123 更新时间:2023-11-29 20:06:59 26 4
gpt4 key购买 nike

ListView 有几个小时的问题。

我使用 ListView 显示我的闹钟及其 工作日 并使用 switch 显示它是否处于 Activity 状态。但是,当我滚动 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;
}

}

最佳答案

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

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

只用这个

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

没有其他条件。如果条件,还初始化此 convertView 之外的所有 textView 和其他 View 。

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

关于android - 在 Android 中滚动时 ListView 项目发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35524794/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com