ListView组件

来源:互联网 发布:轩辕网络钥匙扣 编辑:程序博客网 时间:2024/04/29 17:13

package com.geyongchao.te;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TabHost;
import android.widget.TabWidget;

public class TeActivity extends Activity {
 private String[] datas = new String[]{"a","b","c"};
 
 private  String[] strs = new String[] {
      "first", "second", "third", "fourth", "fifth"
      };//定义一个String数组用来显示ListView的内容private ListView lv;/** Called when the activity is first created. */
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
//        ListView lv = (ListView) findViewById(R.id.lv1);//得到ListView对象的引用 /*为ListView设置Adapter来绑定数据*/
//        ListView lv1 = (ListView) this.findViewById(R.id.lv1);
//        lv1.setAdapter(new ArrayAdapter<String>(getBaseContext(),android.R.layout.simple_list_item_1,datas ));
        ListView list = (ListView) findViewById(R.id.lv1); 
       
        //生成动态数组,并且转载数据 
        ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>(); 
        for(int i=0;i<30;i++) 
        { 
            HashMap<String, String> map = new HashMap<String, String>(); 
            map.put("ItemTitle", "This is Title....."); 
            //map.put("ItemText", "This is text....."); 
            mylist.add(map); 
        } 
        //生成适配器,数组===》ListItem 
        SimpleAdapter mSchedule = new SimpleAdapter(this, //没什么解释 
        mylist,//数据来源  
        R.layout.listitem,//ListItem的XML实现                                           
        //动态数组与ListItem对应的子项         
        new String[] {"ItemTitle", "ItemText"},                                            
       //ListItem的XML文件里面的两个TextView ID 
        new int[] {R.id.ItemTitle,R.id.ItemText}); 
        //添加并且显示 
        list.setAdapter(mSchedule); 
         
       
    }
}

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/lv1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

 

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout  
        android:layout_width="fill_parent"  
        xmlns:android="http://schemas.android.com/apk/res/android"  
        android:orientation="vertical" 
        android:layout_height="wrap_content"  
        android:id="@+id/MyListItem"  
        android:paddingBottom="3dip"  
        android:paddingLeft="10dip"> 
        <TextView  
                android:layout_height="wrap_content"  
                android:layout_width="fill_parent"  
                android:id="@+id/ItemTitle"  
                android:textSize="30dip"> 
        </TextView> 
        <TextView  
                android:layout_height="wrap_content"  
                android:layout_width="fill_parent"  
                android:id="@+id/ItemText"> 
        </TextView> 
</LinearLayout>  

0 0