如何在ListView上选择选项后显示数据?

时间:2012-12-29 18:35:51

标签: java android android-listview

我的ListView显示来自数据库的数据,我想要的是当我点击特定的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”发送到其他活动,以便我查询并检索其他信息。

1 个答案:

答案 0 :(得分: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?

}