获取选定的ListView项目ID(使用SQLite DB填充的数据)

时间:2012-09-19 00:08:53

标签: android database sqlite listview

我有一个数据库,可以跟踪用户的提醒数据(提醒名称,注释,日期,时间等)。第一列是它的主键(_ID)。 ListView从数据库中填充,它显示一组带有提醒名称的简单行,如下所示:


取出垃圾。


遛狗。


吃午饭。



现在我的问题是,如何让我的应用找出点击了哪个特定提醒?单击一行时,我需要从我的数据库中找到其主键(_ID列),并能够检索该行中的所有数据。

到目前为止,我知道我需要使用onItemClick来检测点击。但是,如何获取已单击的项目的主键值(_ID)?我目前的代码如下:

final Context context = this;

//DB Connectivity variables.
protected RemindersDAO remindersDAO;
protected SimpleCursorAdapter remindersCursorAdapter;
public ListView viewRemindersListView;

@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_view_local_reminders);

    // Get rid of the app title in the action bar.
    ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayShowTitleEnabled(false);

    viewRemindersListView = (ListView) findViewById(R.id.listview_view_local_reminders);

    remindersDAO = new RemindersDAO(this);
    Cursor cursor = remindersDAO.all(this);

    remindersCursorAdapter = new SimpleCursorAdapter(this,
                                     R.xml.view_reminders_item_layout,
                                     cursor, new String [] { RemindersDAO.NAME },
                                     new int[] { R.id.view_reminders_item_text } );

    viewRemindersListView.setAdapter(remindersCursorAdapter);
}

@Override
public void onItemClick(AdapterView<?> listView, View view, int position, long arg3) {
    remindersDAO = new RemindersDAO(this);
    Cursor cursor = remindersDAO.all(this);

    int idColIndex = cursor.getColumnIndex(RemindersDAO._ID); 
    int rowId = cursor.getInt(idColIndex); 
}

@Override
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
    // TODO Auto-generated method stub
    MenuInflater inflater = getSupportMenuInflater();
    inflater.inflate(R.menu.activity_view_local_reminders, menu);

    return super.onCreateOptionsMenu(menu);
}

public boolean onOptionsItemSelected(MenuItem item) {
     //Set the classes that are called from each actionbar item.
     switch (item.getItemId()) {
         case R.id.local_reminders_actionbar_go_advanced:
             Intent i=new Intent(this, AdvancedNewReminder.class);
             startActivity(i);
             return true;
         case R.id.local_reminders_actionbar_simple_reminder:
             Intent k = new Intent(this, QuickNewReminder.class);
             startActivity(k);
             return true;

             /* case R.id.local_reminders_actionbar_google_tasks:
             showGoogleTasksBetaDialog();
             return true; */

     }
     return false;
 }

@Override
public void onBackPressed() {
    return;
}

}

感谢您的帮助!

0 个答案:

没有答案