我试过但却无法得到它...... 我尝试像这样添加
new String[] {"name", "_id"}, // I want the value of _id
new int[] {R.id.textView1, id});
但仍然在努力工作
public class Veg extends SherlockActivity implements OnItemClickListener {
private Cursor data;
private DBManager db;
ListView lv;
int id;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.nonveg);
lv = (ListView) findViewById(R.id.list1);
db = new DBManager(this, null, null, 0);
data = db.getVeg(); // you would not typically call this on the main thread
@SuppressWarnings("deprecation")
ListAdapter adapter = new SimpleCursorAdapter(this,
R.layout.veg_item,
data,
new String[] {"name", "_id"},
new int[] {R.id.textView1});
lv.setAdapter(adapter);
lv.setOnItemClickListener(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
data.close();
db.close();
}
@Override
public void onItemClick(AdapterView<?> arg0, View view, int i, long l) {
// TODO Auto-generated method stub
//Toast.makeText(this, id, Toast.LENGTH_LONG).show();
//Intent in = new Intent(Veg.this, Veg_View.class);
//in.putExtra("id", String.valueOf(i)); /// ID need to passed here
//startActivity(in);
}
我需要将此ID传递给新活动。 在新的活动中,我需要检索相应的数据
答案 0 :(得分:0)
将数据发送到另一个活动
是正确的Intent in = new Intent(Veg.this, Veg_View.class);
in.putExtra("id", String.valueOf(i)); /// ID need to passed here
startActivity(in);
在您的新活动中,只需使用此
即可Intent in = getIntent();
Bundle bu = in.getExtras();
int id = in.getInt("id");
答案 1 :(得分:0)
public void onItemClick(AdapterView<?> arg0, View view, int i, long l)
最后一个参数是你想要的_id OnItemClick方法签名是 public void onItemClick(AdapterView parent,View view,int position,long id)
parent
是适配器
view
是点击项目的根视图
position
是Adapter
id
CursorAdapters中与_id