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

New*_*Dev 4 database sqlite android 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;
}
Run Code Online (Sandbox Code Playgroud)

}

感谢你的帮助!

Nam*_*ung 5

onItemClick方法中,你可以看到第三个参数- long idid在数据库中.所以你只需简单地获得点击的项目ID

  @Override
    public void onItemClick(AdapterView<?> listView, View view, int position, long arg3) {
       Log.d("Clicked item id", " "+ arg3); 
    }
Run Code Online (Sandbox Code Playgroud)

如果你想要其他字段,你应该获得点击项目的光标

   @Override
    public void onItemClick(AdapterView<?> listView, View view, int position, long arg3) {
     Cursor item=   (Cursor) reminderCursorAdapter.getItem(position);
Log.d("Clicked item field", " "+ item.getColum(your colum index)); 
    }
Run Code Online (Sandbox Code Playgroud)