2013-10-25 42 views
0

我有一個包含許多列表項的列表視圖。每個列表項都包含testname,test enddate和no。我想根據日期對列表項進行排序。已過期的日期爲灰色,新的測試日期爲黑色。我附上了我迄今爲止所做的代碼。 可以請你建議我做一些代碼。Android:如何在Android中按日期排序列表

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

你有任何錯誤或只是上面的代碼不起作用? – SathishKumar

+0

我沒有得到errors.the代碼工作正常,但我不知道如何排序列表 –

回答

1
  • 爲包含名稱,結束日期和問題一個列表項創建一個容器類。
  • 而不是有三個字符串數組,有一個ArrayList包含這些容器。
  • 那種ArrayList的使用Collections.sort(名單時,比較器)

讓我們知道,如果你需要任何的這些步驟的具體幫助。

附加題:

  • 使用Date對象,而不是字符串來表示日期。