2012-08-08 57 views
0

所以基本上我有來自數據庫的記錄,每個記錄都有自己的唯一ID(主鍵)和日期(mm:hh mm/dd/yy),我想在應用程序的列表視圖中顯示這些記錄。但有時間(mm:hh)有點醜,所以我決定只在列表中顯示mm/dd/yy,現在的問題是,當我編寫onitemClick時,如何找出正在點擊哪個項目??因爲我沒有唯一的ID和確切的日期顯示在列表中了。如果項目名稱相同,如何找出列表中的哪個項目是點擊?

ArrayList<HashMap<String, String>> aList = new ArrayList<HashMap<String, String>>(); 
     HashMap<String, String> map; 

     for (int x = Records.size()-1; x >=0; x--) 
     { 
      map = new HashMap<String, String>(); 
      map.put("ID", String.valueOf(Records.get(x).getId())); 
      map.put("date", Records.get(x).getDate()); //I am going to change this date to just mm/dd/yy 


      aList.add(map); 
     } 

     sd = new SimpleAdapter(this, aList, R.layout.historyactivityrow, 
       new String[] 
       { "date" }, new int[]    
       { R.id.date }); 

     lv.setAdapter(sd); 

     lv.setOnItemClickListener(new OnItemClickListener() 
     { 

      public void onItemClick(AdapterView<?> arg0, View view, int arg2, 
        long arg3) 
      { 
       TextView tx = (TextView) view.findViewById(R.id.date); 
       String s = tx.getText().toString(); 
       Intent intent = new Intent(HistoryActivity.this, EditRecordActivity.class);   
       intent.putExtra("date", s); //I can't do this anymore because now the EditRecordActivity will not know the exact record to be edited. 
       startActivity(intent); 

      } 
     }); 

請幫忙。

回答

0

這一切都在OnItemClickListener()

public void onItemClick(AdapterView<?> arg0, View view, int arg2, 
       long arg3) 

你是正確的,你沒有一個唯一的ID,但你可以通過使用CursorAdapterLoaderManager改變這種狀況。當使用CursorLoader時,參數long arg3成爲唯一ID。將CursorLoaderLoaderManager以及可能ContentProvider一起使用,這是一個很好的附加好處,儘管我告訴你這一點,所以你可以做一些研究。至於你如何做到這一點,你有幾個選擇。當您將物品放入ListView時,您可以給它一個帶有唯一ID的setTag()的標籤。這不會有太多的額外代碼來執行你現在已有的東西。您的其他選項是檢查int arg2這是ListView中的位置。缺點是你沒有來自數據庫的唯一ID。但它會告訴你哪些是你需要的清單。如果你需要身份證,那麼前者就是你所做的。

希望它有幫助。

+0

感謝您的幫助! – 2012-08-08 22:08:23

相關問題