Android组件 列表(ListView)

1、ListView的使用-------ArrayAdapter

<?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/myListView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>
package com.demo.android.listview1;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class Android_ListView_ArrayAdapterActivity extends Activity {
	private ListView myListView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        myListView=(ListView) findViewById(R.id.myListView);
        ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1,getMyData());
        myListView.setAdapter(adapter);
    }
    private List<String> getMyData(){
    	List<String> datas=new ArrayList<String>();
    	datas.add("数据项一");
    	datas.add("数据项二");
    	datas.add("数据项三");
    	datas.add("数据项四");
    	datas.add("数据项五");
    	return datas;
    } 
}

2、ListView的使用-------SimpleAdapter

/res/drawable/trippoilistviewbg.xml文件代码如下:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true" android:drawable="@drawable/beijing"/>
    <item android:state_selected="true" android:drawable="@drawable/beijing"/>
    <item android:state_focused="true" android:drawable="@drawable/beijing"/>
</selector>
 

/res/layout/listviewrow.xml文件代码如下:

<?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="horizontal" 
    android:background="@drawable/trippoilistviewbg">
    
	<!-- 手机图片 -->
	<ImageView 
	    android:id="@+id/tripImg"
	    android:layout_width="68dp"
	    android:layout_height="65dp"
	    android:layout_margin="10dp"/>
	
	<!-- 显示手机名称,价格,打折信息的 LinearLayout-->
	<LinearLayout 
	    android:orientation="vertical"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:layout_marginTop="10dp"
	    android:layout_marginRight="10dp"
	    android:layout_marginBottom="10dp">
	    
	    <!-- 手机名称 -->
	    <TextView 
	        android:id="@+id/phoneName"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:textColor="#ff000000"
	        />
	    
	    <!-- 手机价格 -->
	    <TextView 
	        android:id="@+id/phoneMoney"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:textColor="#ff000000"
	        android:layout_marginTop="5dp"
	        android:layout_marginRight="20dp"/>
	    
	    <!-- 手机打折 -->
	    <TextView 
	        android:id="@+id/phoneDiscount"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:textColor="#ffFF0000"
	        android:layout_marginTop="5dp"
	        android:layout_marginRight="20dp"/>
	</LinearLayout>
</LinearLayout>
<?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" 
    android:id="@+id/myListView"
    android:background="#FFFFF9EB">
</LinearLayout>
package com.demo.android.lv2;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class MainActivity extends Activity {
	private LinearLayout myListLayout;
    private ListView tripListView;
    private List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //获得布局文件
        myListLayout=(LinearLayout) findViewById(R.id.myListView);
        //创建ListView对象
        tripListView=new ListView(MainActivity.this);
        //设置布局参数
        LinearLayout.LayoutParams tripListViewParam=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT);
        //当拖拽列表时,默认显示的颜色为黑色,这里设置为白色
        tripListView.setCacheColorHint(Color.WHITE);
        myListLayout.addView(tripListView, tripListViewParam);
        /**
         *第一个参数:上下文Context
		 *第二个参数:每一行的布局资源文件,这里自定义的列表项布局文件
		 *第三个参数:HashMap中的key信息img,name,money,zhe
		 *第四个参数:listviewrow.xml中的组件id
         */
        SimpleAdapter adapter=new SimpleAdapter(MainActivity.this, getTripListData(),R.layout.listviewrow,new String[]{"img","name","money","zhe"} ,new int[]{R.id.tripImg,R.id.phoneName,R.id.phoneMoney,R.id.phoneDiscount});
        tripListView.setAdapter(adapter);
        
        tripListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View view, int position,long id) {
				Toast.makeText(MainActivity.this, "您选择的是:"+list.get(position).get("name").toString(), Toast.LENGTH_SHORT).show();
			}
		});
    }
    
    public List<Map<String, Object>> getTripListData(){
    	Map<String, Object> map = new HashMap<String, Object>();
		map.put("img", R.drawable.moto);
		map.put("name", "摩托罗拉(motorola)XT711 3G手机");
		map.put("money", "2699元");
		map.put("zhe", "9折");
		list.add(map);

		map = new HashMap<String, Object>();
		map.put("img", R.drawable.iphone);
		map.put("name", "iPhone4 16G版");
		map.put("money", "5880元");
		map.put("zhe", "8.5折");
		list.add(map);

		map = new HashMap<String, Object>();
		map.put("img", R.drawable.samsung);
		map.put("name", "三星(SAMSUNG)i9003 3G手机");
		map.put("money", "3099元");
		map.put("zhe", "9折");
		list.add(map);

		return list;
    }
}

 

3、ListView的使用-------SimpleCursorAdapter

<?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" 
    android:background="#FF666666">
    <ListView 
        android:id="@+id/myListView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>
package com.demo.android.lv3;

import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.Contacts.People;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class Android_ListView_SimpleCursorAdapterActivity extends Activity {
	private ListView myListView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        myListView=(ListView) findViewById(R.id.myListView);
        ContentResolver resolver=getContentResolver();
        Cursor cursor=resolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        //将Cursor交给Activity管理,这样Cursor的生命周期可以和Activity自动同步
        startManagingCursor(cursor);
        SimpleCursorAdapter adapter=new SimpleCursorAdapter(this,android.R.layout.simple_expandable_list_item_1,cursor,new String[]{ContactsContract.Contacts.DISPLAY_NAME},new int[]{android.R.id.text1});
        myListView.setAdapter(adapter);
    }
}




 

你可能感兴趣的:(Android组件 列表(ListView))