The final effect of the operation
The following are the specific implementations:
1. Create a project named ListView
2, write String.xml. The specific code is as follows:
1 <?XML version= "1.0" encoding= "Utf-8"?>2 <Resources>3 4 <stringname= "App_name">ListView Example</string>5 <stringname= "Hello_world">Hello world!</string>6 <stringname= "Action_settings">Settings</string>7 <stringname= "Name">Name</string>8 9 </Resources>
View Code
3, write Main.xml file. The specific code looks like this:
1 <Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"2 Xmlns:tools= "Http://schemas.android.com/tools"3 Android:layout_width= "Match_parent"4 Android:layout_height= "Match_parent"5 Android:paddingbottom= "@dimen/activity_vertical_margin"6 Android:paddingleft= "@dimen/activity_horizontal_margin"7 Android:paddingright= "@dimen/activity_horizontal_margin"8 Android:paddingtop= "@dimen/activity_vertical_margin"9 Tools:context= "Com.example.listview.mainactivity$placeholderfragment" >Ten One A <LinearLayout - android:orientation= "vertical" - Android:layout_width= "Fill_parent" the Android:layout_height= "Fill_parent" - > - <TextView - Android:layout_width= "Fill_parent" + Android:layout_height= "Wrap_content" - Android:text= "@string/name"/> + <ListView A Android:layout_width= "Fill_parent" at Android:layout_height= "Wrap_content" - Android:id= "@+id/listview" - ></ListView> - </LinearLayout> - </Relativelayout>
View Code
4, write Mainactivity.java file. The specific code looks like this:
1 Package Com.example.listview;2 3 import android.support.v7.app.ActionBarActivity;4 import Android.support.v7.app.ActionBar;5 import android.support.v4.app.Fragment;6 import Android.annotation.SuppressLint;7 import Android.os.Bundle;8 import Android.view.LayoutInflater;9 import Android.view.Menu;Ten import Android.view.MenuItem; One import Android.view.View; A import Android.view.ViewGroup; - import Android.widget.AdapterView; - import Android.widget.ArrayAdapter; the import Android.widget.ListView; - import Android.widget.Toast; - import Android.os.Build; - + Public class Mainactivity extends Actionbaractivity { - + @Override A protected void OnCreate (Bundle savedinstancestate) { at super.oncreate (savedinstancestate); - Setcontentview (r.layout.activity_main); - - if (savedinstancestate = = null) { - Getsupportfragmentmanager (). BeginTransaction () - . Add (R.id.container, New Placeholderfragment ()). commit (); in } - } to + @Override - Public Boolean Oncreateoptionsmenu (Menu menu) { the * //inflate the menu; This adds items to the action bar if it is present. $ getmenuinflater (). Inflate (R.menu.main, menu);Panax Notoginseng return true; - } the + @Override A Public Boolean onoptionsitemselected (MenuItem item) { the //Handle Action Bar item clicks here. The Action Bar would + //automatically handle clicks on the Home/up button, so long - //As you specify a parent activity in Androidmanifest.xml. $ int id = item.getitemid (); $ if (id = = r.id.action_settings) { - return true; - } the return super.onoptionsitemselected (item); - }Wuyi the /** - * A placeholder fragment containing a simple view. Wu */ - @SuppressLint ("Validfragment") About Public class Placeholderfragment extends Fragment { $ - @SuppressLint ("Validfragment") - Public placeholderfragment () { - } A + @Override the Public View Oncreateview (layoutinflater inflater, ViewGroup container, - Bundle savedinstancestate) { $ View Rootview = inflater.inflate (R.layout.fragment_main, container, the false); the //Gets the ID of the control the Final ListView listview= (ListView) Rootview.findviewbyid (R.id.listview); the //define Arrays - final string[] name={"Zhang San", "John Doe", "Harry", "Liu Liu"}; in //Create a Arrayadapter the arrayadapter adapter=new arrayadapter (mainactivity.this,android. R.layout.simple_expandable_list_item_1,name); the Listview.setadapter (adapter); About //Register an element Click event Listener Method the Listview.setonitemclicklistener (New Adapterview.onitemclicklistener () { the the @Override +public void Onitemclick (Adapterview<?> arg0, view view, - int position, long ID) { the //TODO auto-generated method stubBayi Toast.maketext (Mainactivity.this, name[position], Toast.length_long). Show (); the } the }); - return rootview; - } the } the the }
View Code
Android List display (ListView)