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