2012-08-08 55 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

相关问题