2016-02-20 63 views
1

我有一個ListView幾個小時的問題。在Android中滾動時Listview項目發生變化

我使用ListView來顯示我的警報及其weekdays並使用switch來顯示它是否處於活動狀態。但是,當我滾動我的weekdaysswitch正在更改時,只有顯示時間的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; 
    } 

} 

回答

1

當我在getView()中有很多條件時,我總是用我的listView適配器來面對這個問題。第一個建議是在getView()中使用最小條件。我使用的另一種解決方案是將getView函數中的所有視圖持有者視圖初始化爲最終變量,並在每個condtion上添加notifyDataSetChange();大多數時候這樣做解決了我的問題。 而代替

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

只需使用這個

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

沒有別的條件。還初始化您的所有textViews和其他視圖之外的這個convertView條件。

注:這是不是最好的解決辦法,但這樣解決我的問題大部分時間

+0

謝謝你的回答。好吧,我會照你說的去做。我第一次使用它,這就是爲什麼我對它感到困惑的原因 – HesenliEldar

+0

當我滾動這個時,它使交換機停止如何解決它? – HesenliEldar

+0

你需要將開關的某個值存儲在哪裏。您可以在列表中爲切換活動或不活動創建一個布爾變量,並基於該變量您可以更改您的開關 – Arshad

1

如果convertView != null您應該首先將日間顏色重置爲默認值。

+0

謝謝主席先生。它完美地工作和什麼開關? – HesenliEldar

相關問題