0

我有一个包含许多列表项的列表视图。每个列表项都包含测试名称、测试结束日期和编号。问题。我想根据日期对列表项进行排序。过期的日期是灰色的,新的测试日期是黑色的。我附上了我到目前为止所做的代码。你能建议我一些代码来做吗?

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

1 回答 1

1
  • 为一个包含名称、结束日期和问题的列表项创建一个容器类。
  • 与其拥有三个字符串数组,不如拥有一个包含这些容器的 ArrayList。
  • 使用 Collections.sort(List, Comparator) 对该 ArrayList 进行排序

如果您在这些步骤中需要具体帮助,请告诉我们。

额外学分:

  • 使用 Date 对象而不是字符串来表示日期。
于 2013-10-25T10:36:05.727 回答