gpt4 book ai didi

android - 将变量从 ListView 传递到另一个 Activity

转载 作者:行者123 更新时间:2023-11-29 00:30:52 25 4
gpt4 key购买 nike

我一直在努力使用一些教程将 ListView 选项传递给新 Activity 并使其成为标题(我稍后会用它做其他事情)。我已经根据最好放入其中的内容设置了一个 OnClickListener

ListView listView1 = (ListView) findViewById(R.id.sportslist);

String[] items = { "Archery", "Badminton", "Cricket", "Dodgeball", "Equestrian", "Football", "Golf", "Handball", "Ice Hockey", "Ju Jitsu", "Karate", "Lacrosse", "Mountain Biking", "Netball" };

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);

listView1.setAdapter(adapter);

listView1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id)

?????

}
});

谢谢

编辑:额外代码

final TextView changetitle = (TextView) findViewById(R.id.detailedsocietyname);
changetitle.setText(name);

最佳答案

首先,您需要获取所选项目:

final String selected = items[position];

或者像drive医生建议的那样

final String selected = (String) parent.getSelectedItem();

然后您需要将该字符串作为额外内容传递给您的新 Activity

Intent i = new Intent(getApplicationContext(), MyClass.class);
i.putExtra("name", selected);
startActivity (i);

最后在你的下一个 Activity 中

    Intent in = getIntent();
String name = in.getStringExtra(("name"));//gets name from intent

关于android - 将变量从 ListView 传递到另一个 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15810386/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com