<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <ListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="match_parent" android:entries="@array/names" android:divider="#aa0000" android:dividerHeight="20dp" android:drawSelectorOnTop="false" android:scrollbars="horizontal|vertical" android:fastScrollEnabled="true" android:listSelector="#00ffff" > </ListView> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">ListViewDemo</string> <string name="action_settings">Settings</string> <string name="hello_world">Hello world!</string> <string-array name="names"> <item>水电工</item> <item>定个的</item> <item>挨饿高</item> <item>反倒是</item> <item>改动份</item> <item>推荐个</item> <item>车厢内</item> <item>认同感</item> <item>参与奖</item> <item>如啼眼</item> <item>风格化</item> <item>电饭锅</item> <item>就快了</item> <item>老婆的</item> <item>错的人</item> </string-array> </resources>
package com.example.listviewdemo; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity implements OnItemClickListener{ private ListView lv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv = (ListView) findViewById(R.id.listView); lv.setOnItemClickListener(this); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub System.out.println("Parent = " + arg0.getClass()); System.out.println("View = " + arg1); TextView tv = (TextView)arg1; Toast.makeText(this,tv.getText(), Toast.LENGTH_SHORT).show(); System.out.println("Position = " + arg2); System.out.println("ID = " + arg3); } }
import android.app.ListActivity; import android.os.Bundle; import android.view.View; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; public class MainActivity2 extends ListActivity{ @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); String[] names = {"张三","李四","赵武","王六","周琦","养吧","文档",}; ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,names); setListAdapter(adapter); } @Override protected void onListItemClick(ListView l, View v, int position, long id) { // TODO Auto-generated method stub super.onListItemClick(l, v, position, id); TextView tv = (TextView)v; Toast.makeText(this,tv.getText(), Toast.LENGTH_SHORT).show(); } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="match_parent" > </ListView> </LinearLayout>
package com.example.listviewdemo; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.ListView; public class MainActivity3 extends Activity implements OnItemClickListener{ private ListView lv; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.main3); lv = (ListView) findViewById(R.id.listView1); String[] citys = {"北京","上海","广州","深圳","东光","中国"}; //单选模式 // ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice,citys); // lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE); //多选模式 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice,citys); lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); lv.setOnItemClickListener(this);//注册事件 lv.setAdapter(adapter); } @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub System.out.println("View = " + arg1); } }
原文地址:http://blog.csdn.net/yuxikuo_1/article/details/39530161