java - ListView选择选项后如何显示数据?

标签 java android android-listview

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

我在打开新 Activity 时没有问题,但无法让该 Activity 知道在 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”发送到其他 Activity ,以便我查询它并检索其他信息。

最佳答案

在列表项上单击获取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?

}   

关于java - ListView选择选项后如何显示数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14084735/

相关文章:

java - 找不到符号错误_PrintWriter.printf()方法_

java - 如何使用三个表编写这个复杂的 MySQL 查询?

android - 使用嵌套的 json 对象改造获取请求

android - 输入几行后文本输入的底部溢出 flutter

Android ListView 理解

android - 在代码下方运行时,GC 因 GC_CONCURRENT 而发疯

android - 如何在 ListView 的按下状态下使用带有渐变的可绘制对象?

java - 在 jooq 中使用数组参数调用 postgres 函数

java - JSF 多个部分验证场景

Android - 如何在 2 个布局之间放置 fab 菜单(来自 yavski 库)