ListView控件简单使用
main.xml:设定ListView父控件的大小,定义ListView的各种属性
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:id="@+id/listLinearLayout" android:layout_width="fill_parent" android:layout_height="fill_parent"> <!-- 在LinearLayout里嵌套一个ListView控件 --> <ListView android:id="@id/android:list" android:layout_width="fill_parent" android:layout_height="wrap_content" android:drawSelectorOnTop="false" android:scrollbars="vertical"/> </LinearLayout>
user.xml:设定ListView列表中每一项的排列布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:id="@+id/subject" android:layout_width="wrap_content" android:layout_height="wrap_content" android:singleLine="true" android:textSize="20pt" /> <TextView android:id="@+id/teacher" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="10pt" /> </LinearLayout>
ListViewActivity.java:主要代码文件
package paj.ListView; import java.util.ArrayList; import java.util.HashMap; import android.app.Activity; import android.app.ListActivity; import android.os.Bundle; import android.view.View; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.Toast; public class ListViewActivity extends ListActivity{ /** Called when the activity is first created. */ //列表展示内容 String subject[][] = new String[][]{{"Math","Tom"}, {"Chinese","Mary"}, {"English","Jim"}, {"Biology","Bill"}, {"History","vitor"}, {"Polical","kim"}}; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // 建立一个List列表 ArrayList<HashMap<String, String>> list =new ArrayList<HashMap<String,String>>(); /** * SimpleAdapter()方法说明, * 第一个参数this是指ListViewActivity, * 第二个参数是指List列表,即之前建立的list变量, * 第三个参数是指列表参数的标识符:这里的每一项列表有两个值,一个是用subject来标识科目,一个用teacher标识teacher * 第四个参数是指值对应的控件,比如subject对应的是ID号码为R。id。subject的TextView文件,同理可得teacher对应的是ID码为R。id。teacher的TextView文件 */ SimpleAdapter listAdapter =new SimpleAdapter(this, list, R.layout.user, new String[]{"subject","teacher"}, new int[]{R.id.subject,R.id.teacher}); //item变量 HashMap<String, String> map =null; for (int i=0;i<6;i++){ /** * 申请一个新的Hashmap * map。put()方法说明 * 第一个参数对应的是列表参数标识符,即subject或teacher * 第二个参数对应的是列表参数标识符所对应的值 */ map = new HashMap<String, String>(); map.put("subject", subject[i][0]); map.put("teacher", subject[i][1]); list.add(map); } //设置显示ListView setListAdapter(listAdapter); } //重写onListItemClick但是ListView条目事件 // position反馈的是当前点击的item是在ListView中的第几个item @Override protected void onListItemClick(ListView l, View v, int position, long id) { // TODO Auto-generated method stub super.onListItemClick(l, v, position, id); Toast.makeText(getApplicationContext(), subject[position][1],Toast.LENGTH_SHORT).show(); } }
相关推荐
chenjinlong 2020-02-19
83580494 2013-07-19
langjiao 2013-07-16
kiduo0 2013-07-10
gongzhiyao0 2010-11-15
bigdatazx 2010-11-05
Urchindong 2011-08-01
peixiaopao 2011-08-21
MeOrdinary 2014-05-13
magic00 2019-10-21
guizhongyun 2011-09-27
csuhanshuai 2015-03-30
Sunanang 2015-03-30
snailbing 2015-04-23
huohu00 2015-04-22
toperfect 2015-07-01
nickey 2012-01-29
Rgenxiao 2012-01-26
满城风絮 2011-12-03