Transfer a string from list view on one activity to another in android
本问题已经有最佳答案,请猛点这里访问。
如何在点击时从listView传输字符串并使用保存在不同活动中的变量中的字符串?
您可以通过在listview的项目单击事件中编写用于调用下一个活动的代码来完成此操作
1 2 3 | Intent intent= new Intent(getBaseContext(),AnotherActivity.class); intent.putExtra("ANY_KEY","YOUR STRING VALUE"); startActivity(intent); |
然后在其他活动的创建方法上获取你的字符串的值
1 | String str=getIntent().getStringExtra("ANY_KEY"); |
是的,使用这样的代码:
1 2 3 4 5 6 7 8 | 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
1 2 | Bundle bundle = getIntent().getExtras(); String var = bundle.getString("var"); |
1 2 3 | Intent intent = new Intent(fisrtActivity.this, secondActivity.class); intent.putExtra(name of extra,String); startActivity(intent); |
要获得secondActivity:
1 2 | Intent i = getIntent() String s = i.getStringExtra(name of extra); |
修改您的适配器类,如下所示,并尝试,
1 2 3 4 5 6 7 | @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); } |
为了获得价值,
1 | String yourString = getIntent().getExtras().getString("value"); |