您好,登錄后才能下訂單哦!
MainActivity.java
package com.example; import android.support.v7.app.ActionBarActivity; import android.support.v7.app.ActionBar; import android.support.v4.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; //用ListView需引用ListView包 import android.widget.ArrayAdapter; //用ArrayAdapter需引用ArrayAdapter包 import android.widget.ListView; import android.os.Build; public class MainActivity extends ActionBarActivity { private ListView listview; //定義一個String類型的數組來作為顯示在ListView上的數據 private String[] userlist = new String[]{"孫悟空","豬八戒","沙和尚"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //獲取UI上的listview組件 listview = (ListView) findViewById(R.id.listview); //定義ArrayAdapter并添加三個參數 ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,userlist); //listview綁定adapter listview.setAdapter(adapter); if (savedInstanceState == null) { getSupportFragmentManager().beginTransaction() .add(R.id.container, new PlaceholderFragment()) .commit(); } } }
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.MainActivity" tools:ignore="MergeRootFrame" > <ListView android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。