[英]How do I display information in my second activity in Android
this is my first attempt at programming for Android. 这是我第一次为Android编程。 Ive been working on creating an activity which has a list of countries, and once any of them are clicked, a new activity is opened with information regarding that country.
Ive正在努力创建一个包含国家列表的活动,一旦单击其中任何一个,就会打开一个新活动,其中包含有关该国家的信息。
I am trying to use the intent function to do this, I have all the information I want in different arrays, all in the correct order, but my problem seems to be with the "position" function to be able to display them. 我正在尝试使用意图函数来执行此操作,我以不同的顺序将所需的所有信息按不同的顺序排列在所有数组中,但是我的问题似乎在于“位置”函数能够显示它们。
This is my MainActivity code regarding the intent: 这是我有关意图的MainActivity代码:
public class MainActivity extends ListActivity { 公共类MainActivity扩展了ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] CountryArray = {"Egypt", "Jordan", "Kuwait", "Saudi Arabia"};
setListAdapter(new MyArrayAdapter(this, CountryArray));
final ListView CountryList = getListView();
CountryList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
final String Country = (String) getListAdapter().getItem(position);
{
Intent myIntent = new Intent(view.getContext(), SecondActivity.class);
myIntent.putExtra(Country, position);
startActivity(myIntent);
and this is the code in my second activity: 这是我第二个活动中的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
String[] CountryArray = {"Egypt", "Jordan", "Kuwait", "Saudi Arabia"};
String[] CapitalArray = {"Cairo", "Amman", "Kuwait City", "Riyadh"};
String[] CurrencyArray = {"EGP", "Jordanian Dinar", "Kuwait Dinar", "Riyal"};
String[] PopulationlArray = {"90 Million", "6.3 Million", "3.25 Million", "29 Million"};
Intent i = getIntent();
int position = i.getIntExtra("Country", 0);
You need to grab the View that you wish to display the information in. If you have a TextView defined in the XML for your second activity this can be done by: 您需要获取要在其中显示信息的View。如果您在第二个活动的XML中定义了TextView,则可以通过以下方法完成:
TextView textView = (TextView)findViewById(R.id.myTextView); // Using ID you set in XML
textView.setText("My information"); // Display your information
I'm guessing you would probably set the text to something like CountryArray[position]
. 我猜您可能会将文本设置为
CountryArray[position]
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.