0

我的 ListView 显示来自数据库的数据,我想要的是,当我单击特定的 ListView 选项时,会启动一个新活动,并在该活动中显示与所选选项相关的数据。

我在打开新活动时没有问题,但无法让该活动知道在 ListView 上选择了哪个选项。

列表视图的代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    database = new projectdatabase(ProjectExplorer.this);

    openDatabase();
}

private void openDatabase() {

    database.open();
    cursor = database.getDataforDisplay();
    adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cursor, new String[] {"project_name"}, new int[] { android.R.id.text1 }, 0);
    setListAdapter(adapter);

}

@Override
public void onListItemClick(ListView parent, View view, int position, long id) {

    super.onListItemClick(parent, view, position, id);

            Intent intent = new intent(this, openactivity.class);
            startActivity(intent);


    //What else to add here?

}      

我只想将“project_name”发送到其他活动,以便查询它并检索其他信息。

4

1 回答 1

2

获取project_name 列表项单击为:

@Override
public void onListItemClick(ListView parent, View view, int position, long id) {

    super.onListItemClick(parent, view, position, id);

      Cursor c = ((SimpleCursorAdapter)parent.getAdapter()).getCursor();
      c.moveToPosition(position);

      // get project name here

      String str_projectname= c.getString(c.getColumnIndex("project_name"));

      Intent intent = new intent(this, openactivity.class);

      intent.putExtra("project_name", str_projectname);

      startActivity(intent);


    //What else to add here?

}   
于 2012-12-29T18:41:54.893 回答