您好,登錄后才能下訂單哦!
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; //定義一個(gè)String類型的數(shù)組來作為顯示在ListView上的數(shù)據(jù) 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并添加三個(gè)參數(shù) 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>
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。