Android:按日期排序列表

Android:按日期排序列表,android,listview,Android,Listview,我是android新手。我正在做一个项目。我有一个包含日期的列表视图。我必须首先显示最近的日期,最后显示过期的日期,对列表项进行排序。 我尝试了以下代码,但没有得到结果 public static String getDate(long milliSeconds, String dateFormat) throws Exception { DateFormat formatter = null; Calendar calendar = null;

我是android新手。我正在做一个项目。我有一个包含日期的列表视图。我必须首先显示最近的日期,最后显示过期的日期,对列表项进行排序。 我尝试了以下代码,但没有得到结果

        public static String getDate(long milliSeconds, String dateFormat)
        throws Exception {
    DateFormat formatter = null;
    Calendar calendar = null;
    formatter = new SimpleDateFormat(dateFormat);
     Date date = new Date(0);
        String current_date = formatter.format(date);

    try {

        calendar = Calendar.getInstance();
        calendar.setTimeInMillis(milliSeconds);

    } catch (Exception e) {
        e.printStackTrace();
    }
    calendar.add(Calendar.DATE, 1);

    Collections.sort(null, new Comparator<MyObject>() {
          public int compare(MyObject o1, MyObject o2) {
              if (o1.getDateTime() == null || o2.getDateTime() == null)
                return 0;
              return o1.getDateTime().compareTo(o2.getDateTime());
          }
        });



    return formatter.format(calendar.getTime());

   }
}
公共静态字符串getDate(长毫秒,字符串日期格式)
抛出异常{
DateFormat格式化程序=null;
日历=空;
格式化程序=新的SimpleDataFormat(日期格式);
日期=新日期(0);
字符串current_date=formatter.format(日期);
试一试{
calendar=calendar.getInstance();
calendar.setTimeInMillis(毫秒);
}捕获(例外e){
e、 printStackTrace();
}
calendar.add(calendar.DATE,1);
Collections.sort(null,新的Comparator(){
公共整数比较(MyObject o1,MyObject o2){
如果(o1.getDateTime()==null | | o2.getDateTime()==null)
返回0;
返回o1.getDateTime().compareTo(o2.getDateTime());
}
});
返回formatter.format(calendar.getTime());
}
}
请帮我写一些代码。我不知道如何使用比较器编码。我的listview对象是lv。我应该在哪里编写代码并声明listview。请帮助编写一些代码

我的类适配器如下所示

   public class Test_List_Adapter extends BaseAdapter {

public Activity context;
public String names[];
public String endDate[];
public String questions[];
public LayoutInflater inflater;

public Test_List_Adapter(Activity context, ArrayList<String> testid,
        ArrayList<String> testName, ArrayList<String> endDate,
        ArrayList<String> questions) {
    this.context = context;
    this.names = testName.toArray(new String[0]);
    this.endDate = endDate.toArray(new String[0]);
    this.questions = questions.toArray(new String[0]);
//  this.inflater = (LayoutInflater)                                                          context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    this.inflater = LayoutInflater.from(context);
}

@Override
public int getCount() {
    return names.length;
}

   @Override
    public Object getItem(int arg0) {
      // TODO Auto-generated method stub
       return null;
    }

    @Override
      public long getItemId(int arg0) {
       // TODO Auto-generated method stub
        return 0;
     }

public static class ViewHolder {
    TextView testname;
    TextView questions;
    TextView testenddate;
}

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

        ViewHolder holder;
        //Test_List_Adapter.setSelectedIndex(position);
        if (convertView == null) {
            holder = new ViewHolder();
            //inflater = (LayoutInflater)                                          context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
                      //inflater = LayoutInflater.from(context);
            convertView = inflater.inflate(R.layout.test_list_items,
                    null);
            holder.testname = (TextView) convertView
                    .findViewById(R.id.testName);
            holder.questions = (TextView) convertView
                    .findViewById(R.id.noofQuestions);
            holder.testenddate = (TextView) convertView
                    .findViewById(R.id.testDate);
            convertView.setTag(holder);
        } else
            holder = (ViewHolder) convertView.getTag();
        holder.testname.setText(names[position]);
        holder.testenddate.setText(endDate[position]);
        holder.questions.setText(questions[position]);
        Log.v("exiting =test list adapter", "true");
        Log.v("exiting =test list adapter", "return convertview");
        notifyDataSetChanged();     
        } catch (Exception e) {
        Log.e("Test_List_Adapter/getView()", e.toString());
        e.printStackTrace();
      }
            return convertView;
        }
           }
公共类测试\u列表\u适配器扩展BaseAdapter{
公共活动语境;
公共字符串名称[];
公共字符串endDate[];
公共字符串问题[];
公共场所;充气机;
公共测试列表适配器(活动上下文、ArrayList testid、,
ArrayList testName,ArrayList endDate,
ArrayList问题){
this.context=上下文;
this.names=testName.toArray(新字符串[0]);
this.endDate=endDate.toArray(新字符串[0]);
this.questions=questions.toArray(新字符串[0]);
//this.inflater=(LayoutInflater)context.getSystemService(context.LAYOUT\u inflater\u SERVICE);
this.inflater=layoutiner.from(上下文);
}
@凌驾
public int getCount(){
返回name.length;
}
@凌驾
公共对象getItem(int arg0){
//TODO自动生成的方法存根
返回null;
}
@凌驾
公共长getItemId(int arg0){
//TODO自动生成的方法存根
返回0;
}
公共静态类视图持有者{
TextView测试名;
文本视图问题;
TextView测试日期;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
试一试{
视窗座;
//测试列表适配器。设置所选索引(位置);
if(convertView==null){
holder=新的ViewHolder();
//充气器=(LayoutInflater)context.getSystemService(Activity.LAYOUT\u充气器\u SERVICE);
//充气器=充气器。从(上下文);
convertView=充气机。充气(右布局。测试列表项目,
无效);
holder.testname=(TextView)convertView
.findviewbyd(R.id.testName);
holder.questions=(TextView)convertView
.findviewbyd(R.id.noofQuestions);
holder.testenddate=(TextView)convertView
.findViewById(R.id.testDate);
convertView.setTag(支架);
}否则
holder=(ViewHolder)convertView.getTag();
holder.testname.setText(名称[位置]);
holder.testenddate.setText(endDate[位置]);
holder.questions.setText(问题[位置]);
Log.v(“exiting=测试列表适配器”,“true”);
Log.v(“退出=测试列表适配器”,“返回convertview”);
notifyDataSetChanged();
}捕获(例外e){
Log.e(“Test_List_Adapter/getView()”,e.toString());
e、 printStackTrace();
}
返回视图;
}
}

您在错误的位置排序。接下来,将null传递给Collections.sort(),而这应该是要排序的项的集合。能否显示为列表创建适配器的代码?我编辑了适配器类。能否显示创建适配器的行以及检索结束日期的行?