Android:如何在android中按日期排序列表

时间:2013-10-25 10:29:38

标签: android

我有一个包含许多列表项的列表视图。每个列表项包含testname,test enddate和no。问题。我想根据日期对列表项进行排序。已过期的日期为灰色,新测试日期为黑色。我附上了我迄今为止所做的代码。 你能告诉我一些代码吗?

  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.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;
        ViewHolder holder1 = null;
        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();

        final long date = System.currentTimeMillis();
        Log.d("Current date", date + "");
        long endtime = Constants.test_closetime.get(position);
        endtime = TimeUnit.SECONDS.toMillis(endtime);
        Log.v("end time of selected class is: ", endtime + "");


        if (date < endtime) {
            Log.d("color", "white");
            convertView.setBackgroundColor(Color.BLACK);
            holder.testenddate.setText(endDate[position]);



        }
        else
        {
            Log.d("color", "gray");
            holder.testenddate.setText(endDate[position]);
            convertView.setBackgroundColor(Color.DKGRAY);


        }

        /*for (int i = 0; i < Constants.test_closetime.size(); i++) 
        {
            long endtime1 = Constants.test_closetime.get(i);
            endtime1 = TimeUnit.SECONDS.toMillis(endtime1);
            for (int j = i+1; j < Constants.test_closetime.size(); j++)
            {
                long endtime2 = Constants.test_closetime.get(j);
                endtime2 = TimeUnit.SECONDS.toMillis(endtime2);
                if(endtime1<endtime2)
                {
                    long temp;
                    temp=endtime1;
                    endtime1=endtime2;
                    endtime2=temp;
                                                                                                        holder.testenddate.setText(endDate[position]);
                }

            }
            holder.testenddate.setText(endDate[position]);
        }*/     


        holder.testenddate.setText(endDate[position]);
        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");
             } catch (Exception e) {
        Log.e("Test_List_Adapter/getView()", e.toString());
        e.printStackTrace();
         }
              return convertView;
      }
    }

1 个答案:

答案 0 :(得分:1)

  • 为包含name,endDate和question。的一个列表项创建容器类。
  • 不要使用三个字符串数组,而是使用一个包含这些容器的ArrayList。
  • 使用Collections.sort(List,Comparator)
  • 对ArrayList进行排序

如果您需要任何相关步骤的具体帮助,请告诉我们。

额外信用:

  • 使用Date对象而不是字符串来表示日期。