gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 07:22:10 24 4
gpt4 key购买 nike

我的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com