2015-05-20 42 views
0

我正在處理報警我設置了整個報警系統(模型,數據庫,服務,接收器等),但除此之外。製作ListView時出現問題,每個項目顯示一個模型的ArrayList

我根據基於用戶輸入的重複日期(星期日 - 星期六),將每個鬧鐘分組在名爲「matchingAlarms」的數組列表中。 我與matchingAlarms ArrayList中,我試圖讓一個ListView其中在ListView中的每一項顯示爲ArrayList中的每個索引的報警名稱和報警時間(我希望他們得到這個部分了,這裏沒有問題

現在用定時器在ListView中更改)。

實施例:

我有1個報警:8 AM - 11 PM,重複天數:太陽週四 AND 1報警上午09點 - 下午12點,重複天數:星期五至六

那些報警中的每一個都在matchingAlarms ArrayList中的索引, 所以matchingAlarms大小是2

我想我提到了一切,現在是代碼。

AlarmListAdapter:

public class AlarmListAdapter extends BaseAdapter { 

public static Context mContext; 
private List<AlarmModel> mAlarms; 
private ArrayList<ArrayList<AlarmModel>> mMatchingAlarms = new ArrayList<ArrayList<AlarmModel>>(); 
private ArrayList<AlarmModel> grouped = new ArrayList<AlarmModel>(); 

private Animation anim; 

AlarmModel model; 
TextView txtTime,txtName,sun,mon,tue,wed,thur,fri,sat; 
ToggleButton btnToggle; 
int matchingAlarmsIndex = 0; 
int groupIndex = 0; 
int itemIndex; 

public AlarmListAdapter(Context context, List<AlarmModel> alarms, ArrayList<ArrayList<AlarmModel>> matchingAlarms) { 
    mContext = context; 
    mAlarms = alarms; 
    mMatchingAlarms = matchingAlarms; 
    anim = AnimationUtils.loadAnimation(mContext, R.anim.right_left); 
} 

public AlarmListAdapter(Context context) { 
    mContext = context; 
} 

public void setAlarms(List<AlarmModel> alarms) { 
    mAlarms = alarms; 
} 

@Override 
public int getCount() { 
    if (mMatchingAlarms != null) { 
     return mMatchingAlarms.size(); 
    } 
    return 0; 
} 

@Override 
public ArrayList<AlarmModel> getItem(int position) { 
    if (mMatchingAlarms != null) { 
     return mMatchingAlarms.get(position); 
    } 
    return null; 
} 

@Override 
public long getItemId(int position) { 
    if (mAlarms != null) { 
     return mAlarms.get(position).id; 
    } 
    return 0; 
} 



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

    if (view == null) { 
     LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     view = inflater.inflate(R.layout.alarm_list_item, parent, false); 
    } 

    txtName = (TextView) view.findViewById(R.id.alarm_item_name); 
    txtTime = (TextView) view.findViewById(R.id.alarm_item_time); 
    sun = (TextView) view.findViewById(R.id.alarm_item_sunday); 
    mon = (TextView) view.findViewById(R.id.alarm_item_monday); 
    tue = (TextView) view.findViewById(R.id.alarm_item_tuesday); 
    wed = (TextView) view.findViewById(R.id.alarm_item_wednesday); 
    thur = (TextView) view.findViewById(R.id.alarm_item_thursday); 
    fri = (TextView) view.findViewById(R.id.alarm_item_friday); 
    sat = (TextView) view.findViewById(R.id.alarm_item_saturday); 
    btnToggle = (ToggleButton) view.findViewById(R.id.alarm_item_toggle); 

    grouped = getItem(position); 


    Timer timer = new Timer(); 
    timer.scheduleAtFixedRate(new TimerTask() { 
     public void run() { 
      ((AlarmListActivity) mContext).runOnUiThread(new Runnable() { 
       @Override 
       public void run() { 
        if ((groupIndex + 1) <= grouped.size()) { 
         model = grouped.get(groupIndex); 
         updateTimeAndDates(model); 
         groupIndex++; 
        } 
        if ((groupIndex + 1) > grouped.size()) { 
         groupIndex = 0; 
        } 
        Log.i("", String.valueOf(groupIndex)); 
       } 
      }); 

     } 
    }, 0, 2500); 



    return view; 
} 

private void updateTextColor(TextView view, boolean isOn) { 
    if (isOn) { 
     view.setTextColor(Colors.IndianRed); 
    } else { 
     view.setTextColor(Color.LTGRAY); 
    } 
} 

private void updateTimeAndDates(AlarmModel model) { 
    txtName.setText(model.name); 
    txtName.startAnimation(anim); 
    txtTime.setText(DietDoctor.to12Hours(model.timeHour, model.timeMinute)); 
    txtTime.startAnimation(anim); 

    updateTextColor(sun, model.getRepeatingDay(AlarmModel.SUNDAY)); 
    updateTextColor(mon, model.getRepeatingDay(AlarmModel.MONDAY)); 
    updateTextColor(tue, model.getRepeatingDay(AlarmModel.TUESDAY)); 
    updateTextColor(wed, model.getRepeatingDay(AlarmModel.WEDNESDAY)); 
    updateTextColor(thur, model.getRepeatingDay(AlarmModel.THURSDAY)); 
    updateTextColor(fri, model.getRepeatingDay(AlarmModel.FRDIAY)); 
    updateTextColor(sat, model.getRepeatingDay(AlarmModel.SATURDAY)); 

    btnToggle.setChecked(model.isEnabled); 
} 

} 

AlarmListActivity:

public class AlarmListActivity extends ListActivity { 

    private AlarmListAdapter mAdapter; 
    private AlarmDBHelper dbHelper = new AlarmDBHelper(this); 
    private Context mContext; 
    private boolean fromSettings; 
    private ArrayList<ArrayList<AlarmModel>> matchingAlarms = new ArrayList<ArrayList<AlarmModel>>(); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     if(getIntent().hasExtra("FromSettings")) 
     fromSettings = getIntent().getExtras().getBoolean("FromSettings"); 

     mContext = this; 

     requestWindowFeature(Window.FEATURE_ACTION_BAR); 

     setContentView(R.layout.activity_alarmlist); 

     setGroups(); 

     mAdapter = new AlarmListAdapter(this, dbHelper.getAlarms(), matchingAlarms); 

     setListAdapter(mAdapter); 
    } 
... 

public void setGroups() { 
     ArrayList<AlarmModel> group = new ArrayList<AlarmModel>(); 
     List<AlarmModel> mAlarms = dbHelper.getAlarms(); 
     for (int i = 0; i < mAlarms.size(); i++) { 
      if ((i + 1) != mAlarms.size()) { 
       boolean result = Arrays.equals(mAlarms.get(i).getRepeatingDays(), mAlarms.get(i + 1).getRepeatingDays()); 
       Log.i("Result", Boolean.toString(result)); 
       if (result) { 
        if (i == 0)group.add(mAlarms.get(i)); 
        group.add(mAlarms.get(i+1)); 
       } 
       if (!result) { 
        if (i == 0)group.add(mAlarms.get(i)); 
        matchingAlarms.add(group); 
        group = new ArrayList<AlarmModel>(); 
        group.add(mAlarms.get(i+1)); 
       } 
      } 

      if ((i + 1) == mAlarms.size()) { 
       boolean result1 = Arrays.equals(mAlarms.get(i).getRepeatingDays(), mAlarms.get(i - 1).getRepeatingDays()); 
       Log.i("Result_1", Boolean.toString(result1)); 
       if (result1) { 
        group.add(mAlarms.get(i)); 
        matchingAlarms.add(group); 
       } 
       if (!result1) { 
        matchingAlarms.add(group); 
        group = new ArrayList<AlarmModel>(); 
        group.add(mAlarms.get(i)); 
       } 
      } 

     } 
     Log.i("Matching Alarms", String.valueOf(matchingAlarms.size())); 
     Log.i("Group", String.valueOf(matchingAlarms.get(0).size())); 
     Log.i("UnGrouped", String.valueOf(mAlarms.size())); 

    } 

我認爲真正的問題出在getView方法及其中的計時器。 正如你從下面的圖片可以看到,我只有週五週六去。第一個項目應該有重複的天報警:太陽週四

enter image description here

回答

0

正在發生,因爲getView是越來越(每個項目至少一次對ListView)多次調用。

因此,在您的適配器使用所有這些TextView字段時,您每次都覆蓋其參考,只有最後一個是真正正確的。

要確定最好的辦法是應用Holder模式。如果你谷歌爲它,你會發現大約一百五十萬的例子,它是如何工作的。

private static class Holder { 
    AlarmModel model; 
    TextView txtTime,txtName,sun,mon,tue,wed,thur,fri,sat; 
    ToggleButton btnToggle; 
    Timer timer = new Timer(); 
} 

,然後更新您的updateTimeAndDates是:

updateTimeAndDates(Holder h) { 
    h.timer.cancel(); 
    h.timer.purge(); 
    // carry on updating the values and create the re-schedule the updates. 
} 

還我會建議對定時器,只是在視圖上使用.postDealayed(Runnable, long);。定時器並不是很好的課程。

相關問題