简体   繁体   English

如何在Android的第二个活动中显示信息

[英]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.

相关问题 Android Studio:如何在我的第二个活动中看到intent方法携带的信息? - Android Studio: how can I see the information carried through the intent method on my second activity? 我将名称存储在mysql中,如何在android应用中显示该信息 - I have names stored in mysql how do I display that information in my android app 带有ArrayList的Android自定义ListView如何打开第二个活动 - Android Custom ListView with ArrayList How do I open a second activity 如何在我在android studio中的第二个活动上显示单击的单选按钮值? - how to display the clicked radio button value on my second activity in android studio? 将主要活动中的第一个按钮链接到网站后,如何将按钮链接到第二个活动? - how do i link a button to a second activity after linking the first buttons in my main activity to websites? 如何将第二个活动中的RecyclerView项目传递给Android中的上一个活动? - How do I pass a RecyclerView item in the second Activity to the previous Activity in Android? 在Android中离开活动后如何保留我的列表视图? - How do I retain my listview after leaving the activity in android? Android:如何告诉我的活动从服务中开始新的活动? - Android: how do I tell my activity to start new activity from my service? 如何获得我的按钮以在Android Studio中打开第二个活动? - How to get my button to open a second activity in Android Studio? 如何从第二个活动中获取领域信息? - How to get the realm information from an second activity?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM