[英]Android Studio: how can I see the information carried through the intent method on my second activity?
[英]How do I display information in my second activity in Android
這是我第一次為Android編程。 Ive正在努力創建一個包含國家列表的活動,一旦單擊其中任何一個,就會打開一個新活動,其中包含有關該國家的信息。
我正在嘗試使用意圖函數來執行此操作,我以不同的順序將所需的所有信息按不同的順序排列在所有數組中,但是我的問題似乎在於“位置”函數能夠顯示它們。
這是我有關意圖的MainActivity代碼:
公共類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);
這是我第二個活動中的代碼:
@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);
您需要獲取要在其中顯示信息的View。如果您在第二個活動的XML中定義了TextView,則可以通過以下方法完成:
TextView textView = (TextView)findViewById(R.id.myTextView); // Using ID you set in XML
textView.setText("My information"); // Display your information
我猜您可能會將文本設置為CountryArray[position]
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.