2015-10-19 129 views
2

在我的應用程序中,我必須顯示一個列表視圖,其中顯示Date和一個String值。一個Model類用於向Array列表提供值。 Date.Date值是使用適配器類中的Model類的值派生的。具有顯示按日期升序排序的列表視圖,並突出顯示與當前日期最近的視圖。下一個大於或等於當前日期的特定視圖的背景顏色。如何按日期排序ListView項目

BaseAdapter類

private ArrayList<Model> mListItems; 
    private Activity mActivity; 
    private LayoutInflater mInflater; 
    private Integer[] mListIcons; 
    private Date DateOfBirth; 
    private int NumberOfMonths; 
    private ArrayList<Integer> SottList; 
    List<Date> dateList = new ArrayList<Date>(); 

    public Adapter(Activity mActivity, Date DateOfBirth,ArrayList<VaccinationScheduleModel> mListItems) { 
//  super(); 

     this.mActivity = mActivity; 
     this.mListItems=mListItems; 
     this.DateOfBirth=DateOfBirth; 
     mInflater = (LayoutInflater) mActivity.getLayoutInflater(); 
    } 

    @Override 
    public int getCount() { 
     return mListItems.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     return mListItems.get(position); 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 

    @Override 
    public View getView(final int position, View convertView, ViewGroup parent) { 
     if (convertView == null) { 
      convertView = mInflater.inflate(R.layout.custom_lv_vacc_schedule, 
        parent, false); 
     } else { 
      convertView = convertView; 
     } 

     String atrNumberOfMonths=mListItems.get(position).getmNumberOfMonths(); 
     NumberOfMonths = Integer.parseInt(atrNumberOfMonths); 
     Calendar cal = Calendar.getInstance(); 
     cal.setTime(DateOfBirth); 
     cal.add(Calendar.MONTH, NumberOfMonths); 
     Date result = cal.getTime(); 
     dateList.add(result); 
     String dayOfTheWeek = (String) android.text.format.DateFormat.format("EEEE", result);//Thursday 
     String stringMonth = (String) android.text.format.DateFormat.format("MMM", result); //Jun 
     String intMonth = (String) android.text.format.DateFormat.format("MM", result); //06 
     String year = (String) android.text.format.DateFormat.format("yyyy", result); //2013 
     String day = (String) android.text.format.DateFormat.format("dd", result); //20 
     System.out.println("19102015:VaccinationScheduleAdapter" + result); 
     TextView txtVaccTitle = (TextView) convertView.findViewById(R.id.txtVaccTitle); 
     TextView txtVaccMonth = (TextView) convertView.findViewById(R.id.txtVaccMonth); 
     TextView txtVaccDay= (TextView) convertView.findViewById(R.id.txtVaccDay); 
     TextView txtVaccYear = (TextView) convertView.findViewById(R.id.txtVaccYear); 
     ImageView imgIcon = (ImageView) convertView.findViewById(R.id.imgIcon); 
     imgIcon.setBackgroundResource(R.drawable.calendar_icon); 
     txtVaccTitle.setText(mListItems.get(position).getmDescription()); 
     txtVaccMonth.setText(stringMonth); 
     txtVaccDay.setText(day); 
     txtVaccYear.setText(year); 
     Collections.sort(dateList); 
     convertView.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 




      } 
     }); 

     return convertView; 
    } 

任何一個可以幫助我在此先感謝

+0

如果你是從獲取數據庫中的數據,然後做出我不是你在使用使用數據庫 –

+0

查詢? 從哪裏得到日期? –

+0

是 –

回答

1

如果您的問題僅與排序列表,你可以使用一個Comparator

Collections.sort(mListItems, new MyComparator<Model>() { 
    int compare(Model first, Model second) { 
     return first.getDate().compareTo(second.getDate()); 
    } 
}); 

它如果您發佈用於獲取物品清單和您的類的代碼將會很有幫助。

0

getViewCollections.sort(dateList)是完全錯誤的,你應該生成你的適配器上排序方法和使用它的適配器(也許在片段在onStart或的onResume)確認後,有權要求notifyDataSetChanged適配器上之外。

5

如果你打電話給你的適配器類用途:

Collections.sort(mListItems, new CustomComparator()); 
adapter = new YourAdater(this,dateOfBirth, mListItems); 
lv.setAdapter(adapter); 

你CustomComparator可能看起來像這樣:

public class CustomComparator implements Comparator<YourObject> {// may be it would be Model 
    @Override 
    public int compare(YourObject obj1, YourObject obj2) { 
     return obj1.getDate().compareTo(obj2.getDate());// compare two objects 
    } 
} 

爲了突出自己的第一個或兩行,寫你的適配器的getView方法,這裏面的代碼。無需在這裏排序。

if(position==0){ // for two rows if(position==0 || position==1) 
    convertView.setBackgroudColor(Your Color); 
} 

希望它可以幫助你。 如果您遇到任何問題,請點評。

0

這部影片幫了我很多按日期排序:

https://www.youtube.com/watch?v=iTGtk1AuQCI

它是關於如何排序但for語句。

我首先有一個arrayListclaseRenta項目,並且它們每個都包含String中的日期。我首先將字符串轉換爲日期,然後在millis中獲得時間,然後比較毫秒中的時間並重新排列arrayRentas。最後,我獲得按照每個日期排序的claseRenta項目的數組。

public void sortRentas() { 

    SimpleDateFormat format = new SimpleDateFormat("E, d MMM, yyyy hh:mm a"); 

    for(int i=0; i<arrayRentas.size();i++){ 
     for(int j=i+1; j<arrayRentas.size(); j++){ 
      Date date1 = null; 
      try { 
       date1 = format.parse(arrayRentas.get(i).getFechaEntrega()); 
      } catch (ParseException e) { 
       e.printStackTrace(); 
      } 
      calendario.setTime(date1); 
      millitime1 = calendario.getTimeInMillis(); 
      Date date2 = null; 
      try { 
       date2 = format.parse(arrayRentas.get(j).getFechaEntrega()); 
      } catch (ParseException e) { 
       e.printStackTrace(); 
      } 
      calendario.setTime(date2); 
      millitime2 = calendario.getTimeInMillis(); 

      if(millitime1>millitime2){ 

       temporalRenta = arrayRentas.get(i); 
       arrayRentas.set(i,arrayRentas.get(j)); 
       arrayRentas.set(j,temporalRenta); 

      } 

     } 
    }