0

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

        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());

   }
}

请帮助一些代码。我不知道如何使用比较器进行编码。我的列表视图对象是 lv。我应该在哪里编写代码并声明列表视图。请帮助一些代码。

我的班级适配器如下

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

0 回答 0