标签:
最终的运行效果如图:
具体的实现操作如下:
1、创建名为ListView的工程
2、编写string.xml.具体代码如下所示:
1 <?xml version="1.0" encoding="utf-8"?> 2 <resources> 3 4 <string name="app_name">ListView示例</string> 5 <string name="hello_world">Hello world!</string> 6 <string name="action_settings">Settings</string> 7 <string name="name">姓名</string> 8 9 </resources>
3、编写main.xml文件。具体代码如下所示:
1 <RelativeLayout xmlns: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" > 10 11 12 <LinearLayout 13 android:orientation="vertical" 14 android:layout_width="fill_parent" 15 android:layout_height="fill_parent" 16 > 17 <TextView 18 android:layout_width="fill_parent" 19 android:layout_height="wrap_content" 20 android:text="@string/name"/> 21 <ListView 22 android:layout_width="fill_parent" 23 android:layout_height="wrap_content" 24 android:id="@+id/listview" 25 ></ListView> 26 </LinearLayout> 27 </RelativeLayout>
4、编写MainActivity.java文件。具体代码如下所示:
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; 10 import android.view.MenuItem; 11 import android.view.View; 12 import android.view.ViewGroup; 13 import android.widget.AdapterView; 14 import android.widget.ArrayAdapter; 15 import android.widget.ListView; 16 import android.widget.Toast; 17 import android.os.Build; 18 19 public class MainActivity extends ActionBarActivity { 20 21 @Override 22 protected void onCreate(Bundle savedInstanceState) { 23 super.onCreate(savedInstanceState); 24 setContentView(R.layout.activity_main); 25 26 if (savedInstanceState == null) { 27 getSupportFragmentManager().beginTransaction() 28 .add(R.id.container, new PlaceholderFragment()).commit(); 29 } 30 } 31 32 @Override 33 public boolean onCreateOptionsMenu(Menu menu) { 34 35 // Inflate the menu; this adds items to the action bar if it is present. 36 getMenuInflater().inflate(R.menu.main, menu); 37 return true; 38 } 39 40 @Override 41 public boolean onOptionsItemSelected(MenuItem item) { 42 // Handle action bar item clicks here. The action bar will 43 // automatically handle clicks on the Home/Up button, so long 44 // as you specify a parent activity in AndroidManifest.xml. 45 int id = item.getItemId(); 46 if (id == R.id.action_settings) { 47 return true; 48 } 49 return super.onOptionsItemSelected(item); 50 } 51 52 /** 53 * A placeholder fragment containing a simple view. 54 */ 55 @SuppressLint("ValidFragment") 56 public class PlaceholderFragment extends Fragment { 57 58 @SuppressLint("ValidFragment") 59 public PlaceholderFragment() { 60 } 61 62 @Override 63 public View onCreateView(LayoutInflater inflater, ViewGroup container, 64 Bundle savedInstanceState) { 65 View rootView = inflater.inflate(R.layout.fragment_main, container, 66 false); 67 //获取控件的ID 68 final ListView listview=(ListView)rootView.findViewById(R.id.listview); 69 //定义数组 70 final String[] name={"张三","李四","王五","刘刘"}; 71 //创建一个ArrayAdapter 72 ArrayAdapter adapter=new ArrayAdapter(MainActivity.this,android.R.layout.simple_expandable_list_item_1,name); 73 listview.setAdapter(adapter); 74 //注册一个元素单击事件监听方法 75 listview.setOnItemClickListener(new AdapterView.OnItemClickListener() { 76 77 @Override 78 public void onItemClick(AdapterView<?> arg0, View view, 79 int position, long id) { 80 // TODO Auto-generated method stub 81 Toast.makeText(MainActivity.this, name[position], Toast.LENGTH_LONG).show(); 82 } 83 }); 84 return rootView; 85 } 86 } 87 88 }
标签:
原文地址:http://www.cnblogs.com/lvdiquan/p/4178440.html