关于我的来电活动:
protected void onListItemClick(ListView l, View v, int position, long id) {
Intent myIntent = new Intent(MainActivity.this, sondaggioActivity.class);
myIntent.putExtra("categoriaId", String.valueOf(id));
MainActivity.this.startActivity(myIntent);
// Toast.makeText(this, String.valueOf(id), Toast.LENGTH_LONG).show();
}
toast返回列表中单击的项的id的正确值。 关于reciver活动我有:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.maindomanda);
int category = getIntent().getExtras().getInt("categoriaId");
TextView text = (TextView) findViewById(R.id.domanda);
text.setText(String.valueOf(category));
}
非常直接......文本字段总是返回0 ...我做错了什么? String.ValueOf()不知何故被弃用了?或者更好地使用其他东西......
还有另外一个问题......我通过long id
,就像我知道我在做什么一样,这是非常错误的...它是所谓的“_ID
”字段吗?我从dbtable获取那些值,是那个表ID吗?
答案 0 :(得分:2)
将您的代码更改为:
String strcategory = getIntent().getExtras().getString("categoriaId");
因为您将String
从onListItemClick传递到sondaggioActivity活动并尝试接收为Int
注意:
第二点也很重要的一点是id
是long
,并且您正在尝试将其解析为Int
,这也是无效的。
看这篇文章:How can I convert a long to int in Java?
解决方案只是从onListItemClick
发送id作为
myIntent.putExtra("categoriaId",id);
并按照sondaggioActivity
活动
long category = getIntent().getExtras().getLong("categoriaId");
答案 1 :(得分:1)
你得到一个int getInt("categoriaId")
,你传入一个字符串putExtra("categoriaId", String.valueOf(id));
将String.valueOf();返回int的STring表示。
答案 2 :(得分:0)
将categoriaId传递给myIntent.putExtra(“categoriaId”,id);