Android 如何在使用onListItemClick启动新活动时收集对象信息
我有一个事件处理程序:Android 如何在使用onListItemClick启动新活动时收集对象信息,android,sqlite,android-activity,Android,Sqlite,Android Activity,我有一个事件处理程序: @Override protected void onListItemClick(ListView l, View v, int position, long id) { // TODO Auto-generated method stub //super.onListItemClick(l, v, position, id); String selection = l.getItemAtPositio
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
//super.onListItemClick(l, v, position, id);
String selection = l.getItemAtPosition(position).toString();
Intent myIntent = new Intent(MainActivity.this, sondaggioActivity.class);
MainActivity.this.startActivity(myIntent);
//Toast.makeText(this, selection, Toast.LENGTH_LONG).show();
}
单击的项目是以下内容的列表视图:
public class categorie {
private long id;
private String nome;
private long preferita;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public long getPreferita() {
return preferita;
}
public void setPreferita(long preferita) {
this.preferita = preferita;
}
// Will be used by the ArrayAdapter in the ListView
@Override
public String toString() {
return nome;
}
}
我按以下方式列出:
datasource = new pollDataSource(this);
datasource.open();
Cursor values = datasource.getAllCategorie();
String[] categorieColumns =
{
MySQLiteHelper.COLUMN_NOME // Contract class constant containing the word column name
};
int[] mWordListItems = { R.id.categoria_label };
SimpleCursorAdapter adapter = new SimpleCursorAdapter(
getApplicationContext(), // The application's Context object
R.layout.single_list_item, // A layout in XML for one row in the ListView
values, // The result from the query
categorieColumns, // A string array of column names in the cursor
mWordListItems, // An integer array of view IDs in the row layout
0); // Flags (usually none are needed)
setListAdapter(adapter);
在新的活动中,我想收集数据库中给定类别(我单击的类别)中的一些“问题”。。。如何将单击的类别传递给新活动?您知道意图吗?您可以在开始下一个活动的意图中添加简单的原语、字符串等。它与键值关系一起工作,如
HashMap
。这里有一个简单的例子
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
int selection = position;
Intent myIntent = new Intent(MainActivity.this, sondaggioActivity.class);
myIntent.putExtra("SELECTION", selection); // <--- put the value (KEY, VALUE)
MainActivity.this.startActivity(myIntent);
}
@覆盖
受保护的void onListItemClick(列表视图l、视图v、整数位置、长id){
int选择=位置;
Intent myIntent=新的Intent(MainActivity.this,sondaggioActivity.class);
myIntent.putExtra(“选择”,选择);//您可能想要:
Cursor data = (Cursor)l.getItemAtPosition(position);
String cat = data.getString(cursor.getIndexColumn(MySQLiteHelper.COLUMN_NOME));
Intent myIntent = new Intent(MainActivity.this, sondaggioActivity.class);
myIntent.putExtra("categorieName", cat);
MainActivity.this.startActivity(myIntent);
然后在SONDAGIOActivity
活动中,您可以使用getIntent()
获取启动活动的Intent
,并检索分类名称。是光标=光标?还是光标是其他东西?
Cursor data = (Cursor)l.getItemAtPosition(position);
String cat = data.getString(cursor.getIndexColumn(MySQLiteHelper.COLUMN_NOME));
Intent myIntent = new Intent(MainActivity.this, sondaggioActivity.class);
myIntent.putExtra("categorieName", cat);
MainActivity.this.startActivity(myIntent);