如何按日期排序ListView项目

时间:2015-10-19 12:05:18

标签: android sorting date listview

在我的应用程序中,我必须显示一个列表视图,其中显示Date和String值。一个Model类用于向Array列表提供值。 Date.Date值是使用Adapter Class中Model类的值派生的.Have显示按日期升序排序的列表视图,并突出显示最接近当前date.highlight的视图。特定视图的背景颜色,接下来大于或等于当前日期。

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;
    }

任何人都可以帮助我吗?先谢谢

4 个答案:

答案 0 :(得分:5)

您调用适配器的地方使用:

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
    }
}

您的CustomComparator可能如下所示:

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

要突出显示前一行或第二行,请在适配器的getView方法中编写此代码。无需在此处进行任何排序。

nowrap

希望它对你有所帮助。 如果您遇到任何问题,请发表评论。

答案 1 :(得分:1)

如果问题仅在于对列表进行排序,则可以使用Comparator

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

如果您发布用于获取项目列表和Model课程的代码,将会很有帮助。

答案 2 :(得分:0)

Collections.sort(dateList)放在getView上是完全错误的,您应该在适配器上生成一个排序方法并在适配器外部使用它(可能在片段onStart或onResume上)确保调用{{1在适配器之后。

答案 3 :(得分: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);

            }

        }
    }