效果图:
啥也没干的ListView张这样:
fry.Activity01
1 package fry; 2 3 import com.example.ListView.R; 4 5 import android.app.Activity; 6 import android.os.Bundle; 7 import android.view.View; 8 import android.widget.AdapterView; 9 import android.widget.AdapterView.OnItemClickListener; 10 import android.widget.ArrayAdapter; 11 import android.widget.ListView; 12 import android.widget.Toast; 13 14 public class Activity01 extends Activity implements OnItemClickListener{ 15 private ListView listView; 16 @Override 17 protected void onCreate(Bundle savedInstanceState) { 18 // TODO Auto-generated method stub 19 super.onCreate(savedInstanceState); 20 setContentView(R.layout.activity01); 21 listView=(ListView) findViewById(R.id.listView); 22 ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, getData()); 23 listView.setAdapter(adapter); 24 listView.setOnItemClickListener(this); 25 } 26 @Override 27 public void onItemClick(AdapterView<?> parent, View view, int position, 28 long id) { 29 // TODO Auto-generated method stub 30 String string=(String) parent.getItemAtPosition(position); 31 32 Toast.makeText(this, string, Toast.LENGTH_SHORT).show(); 33 } 34 35 private String[] getData(){ 36 return new String[]{"韩寒","蔡静","卢本伟","五五开","战狼2","朝阳兄","虹桥一姐","犀利哥","王宝庆", 37 "系大大","搞踹踹","张三","李四","王五"}; 38 } 39 }
/ListView/res/layout/activity01.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 <ListView 8 android:id="@+id/listView" 9 android:layout_width="match_parent" 10 android:layout_height="wrap_content" 11 ></ListView> 12 13 </LinearLayout>