The Activity_list.xml file code is as follows:
1<?xml version="1.0"encoding="Utf-8"?>2<LinearLayout3Xmlns:android="http://schemas.android.com/apk/res/android"4android:orientation="Vertical"5Android:layout_width="fill_parent"6android:layout_height="fill_parent">7 8<ListView9Android:id="@+id/listview"TenAndroid:layout_width="match_parent" Oneandroid:layout_height="match_parent"> A</ListView> - -</LinearLayout>
The Listactivity.java file code is as follows:
1 Package Com.example.sample_6_4;2 3 ImportAndroid.app.Dialog;4 ImportAndroid.graphics.Color;5 Importandroid.preference.DialogPreference;6 ImportAndroid.support.v7.app.AlertDialog;7 Importandroid.support.v7.app.AppCompatActivity;8 ImportAndroid.os.Bundle;9 ImportAndroid.view.View;Ten ImportAndroid.view.ViewGroup; One ImportAndroid.view.WindowManager; A ImportAndroid.widget.ArrayAdapter; - ImportAndroid.widget.BaseAdapter; - ImportAndroid.widget.Button; the ImportAndroid.widget.ListView; - ImportAndroid.widget.TextView; - -PublicclassListactivity extends Appcompatactivity { + @Override - protected void OnCreate (Bundle savedinstancestate) { + super.oncreate (savedinstancestate); A Setcontentview (r.layout.activity_list); atListView LV =(ListView) Findviewbyid (R.id.listview); -//make an adapter to use the ListView - Lv.setadapter (New Myadapter ()); - } - classMyadapter extends baseadapter{ - @Override in//returns the number of entries, the list of entries, such as how many bars a screen displays - public int GetCount () { to return20; + } - @Override the//determine the layout and content of each entry, position represents the location of the entry, starting with 0 * Public View GetView (int position, View Converview, ViewGroup parent) { $TextView TV =new TextView (listactivity.this);Panax NotoginsengTv.settext ("Articles"+position); -Tv.settextsize (23); the Tv.settextcolor (color.blue); + returnTV; A } the @Override + Public Object GetItem (int position) { - returnnull; $ } $ @Override - Public long Getitemid (int position) { - return0; the } - }Wuyi}The effect is as follows:
Android:listview automatically generate list entries