如何在点击时从listView传输字符串并使用保存在不同活动中的变量中的字符串?
答案 0 :(得分:3)
您可以通过在listview项目点击事件中编写用于调用下一个活动的代码来完成此操作
Intent intent= new Intent(getBaseContext(),AnotherActivity.class);
intent.putExtra("ANY_KEY", "YOUR STRING VALUE");
startActivity(intent);
然后在另一个活动的oncreate方法中通过
获取字符串的值String str=getIntent().getStringExtra("ANY_KEY");
答案 1 :(得分:2)
是的,请使用以下代码:
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent i = new Intent(getBaseContext(), Activity2.class);
i.putExtra("var", (String)lv.getAdapter().getItem(position));
startActivity(i);
}
});
在Activity2中获取var
Bundle bundle = getIntent().getExtras();
String var = bundle.getString("var");
答案 2 :(得分:1)
Intent intent = new Intent(fisrtActivity.this, secondActivity.class);
intent.putExtra(name of extra,String);
startActivity(intent);
进入secondActivity:
Intent i = getIntent()
String s = i.getStringExtra(name of extra);
答案 3 :(得分:0)
按如下所示修改适配器类,然后尝试,
@Override
public void onItemClick(AdapterView<?> arg0, View v, int position,
long arg3) {
Intent intent = new Intent(context/getApplicationContext(),SecondActivity.class);
intent.putExtra("value", your_list.get(position));
context/getApplicationContext().startActivity(intent);
}
为了获得价值,
String yourString = getIntent().getExtras().getString("value");