基本的ListView实现

整个的流程描述下来就是
  主Activity继承ListActivity , 主Activity布局文件中加入id为android:list的Listview控件
  主Activity里准备需要显示的数据 ArrayList ,
   写好对应的每一行的显示布局文件 ,  然后用Adapter来加载这个布局文件, 
  Activity中实例化Adapter的时候传入需要显示的数据ArrayList,  Adapter实例化了后调用setListAdapter, 结束
  处理点击某一行, 重新ListActivity的onListItemClick方法.


1.  继承ListActivity 
2.  准备数据 ArrayList <HashMap<String,String>> list 

3.  创建类MyAdapter继承SimpleAdapter, 构造函数中创建一个LayoutInflater对象, 并重写getView方法加载数据显示的xml文件

4.  实例化adapter对象, 并调用ListActivity的方法setListAdapter(myAdapter);  

5.  修改主Activity的布局文件, 加入id为android:list的Listview控件

6.  需要处理点击事件的话 重写onListItemClick

	SimpleAdapter


<strong>public abstract class
LayoutInflater
extends Object</strong>

java.lang.Object
   ↳ 	android.view.LayoutInflater


Class Overview


Instantiates a layout XML file into its corresponding View objects. It is never used directly. Instead, use getLayoutInflater() or getSystemService(Class) to retrieve a standard LayoutInflater instance that is already hooked up to the current context and correctly configured for the device you are running on. For example:


LayoutInflater inflater = (LayoutInflater)context.getSystemService
      (Context.LAYOUT_INFLATER_SERVICE);


To create a new LayoutInflater with an additional LayoutInflater.Factory for your own views, you can use cloneInContext(Context) to clone an existing ViewFactory, and then call setFactory(LayoutInflater.Factory) on it to include your Factory.


For performance reasons, view inflation relies heavily on pre-processing of XML files that is done at build time. Therefore, it is not currently possible to use LayoutInflater with an XmlPullParser over a plain XML file at runtime; it only works with an XmlPullParser returned from a compiled resource (R.something file.)


Public Methods:

static LayoutInflater 	from(Context context)
Obtains the LayoutInflater from the given context. 


==============================================

<strong>public class
SimpleAdapter
extends BaseAdapter
implements Filterable ThemedSpinnerAdapter</strong>




Class Overview


An easy adapter to map static data to views defined in an XML file. You can specify the data backing the list as an ArrayList of Maps. Each entry in the ArrayList corresponds to one row in the list. The Maps contain the data for each row. You also specify an XML file that defines the views used to display the row, and a mapping from keys in the Map to specific views. Binding data to views occurs in two phases. First, if a SimpleAdapter.ViewBinder is available, setViewValue(android.view.View, Object, String) is invoked. If the returned value is true, binding has occurred. If the returned value is false, the following views are then tried in order:


    A view that implements Checkable (e.g. CheckBox). The expected bind value is a boolean.
    TextView. The expected bind value is a string and setViewText(TextView, String) is invoked.
    ImageView. The expected bind value is a resource id or a string and setViewImage(ImageView, int) or setViewImage(ImageView, String) is invoked. 


If no appropriate binding can be found, an IllegalStateException is thrown. 




<strong>Public Constructors
	SimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)</strong>
 

Public Methods:

 View 	getView(int position, View convertView, ViewGroup parent)
Get a View that displays the data at the specified position in the data set. 



setListAdapter(myAdapter);  这个方法属于ListActivity<pre name="code" class="java"><span style="font-family: Arial, Helvetica, sans-serif;">所以我们的显示List的Activity要继承这个类</span>
 





错误
11-22 03:26:29.635: E/AndroidRuntime(2130): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.simplelistview/com.example.simplelistview.ActivityIndex}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'


在需要显示的Layout里必须有ListView控件, 并且ID叫android:list


    <ListView android:id="@+id/android:list" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:drawSelectorOnTop="true"

android:scrollbars="vertical" />


public class ActivityIndex extends ListActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.index);
		ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
		HashMap<String, String> map1 = new HashMap<String, String>();
		map1.put("user_name", "zhangsan");
		map1.put("user_ip", "192.168.1.1");
		HashMap<String, String> map2 = new HashMap<String, String>();
		map2.put("user_name", "lisi");
		map2.put("user_ip", "192.168.1.2");
		HashMap<String, String> map3 = new HashMap<String, String>();
		map3.put("user_name", "wangwu");
		map3.put("user_ip", "192.168.1.3");
		list.add(map1);
		list.add(map2);
		list.add(map3);

		MyAdapter myAdapter = new MyAdapter(ActivityIndex.this, list,
				R.layout.user, new String[] { "user_name", "user_ip" },
				new int[] { R.id.user_name, R.id.user_ip });
		setListAdapter(myAdapter);
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
		System.out.println("id -------------" + id);
		System.out.println("position----------" + position);
	}
}


Adapter

public class MyAdapter extends SimpleAdapter {
	private LayoutInflater inflater = null;
	private List<HashMap<String, String>> style = null;
	public MyAdapter(Context context, List<? extends Map<String, ?>> data,
			int resource, String[] from, int[] to) {
		super(context, data, resource, from, to);
		// this.inflater = new LayoutInflater.from(context);
		inflater = LayoutInflater.from(context);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View result = super.getView(position, convertView, parent);
		// System.out.println("possition ----->" + position);
		if (result != null) {
			inflater.inflate(R.layout.user, null);
		}
		return result;
	}
}


user.xml

<?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="horizontal" >
    <TextView 
        android:id="@+id/user_name"
        android:layout_height="30dip"
        android:layout_width="180dip"
        android:textSize="12pt"
        android:singleLine="true"/>
    
        <TextView 
        android:id="@+id/user_ip"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:textSize="12pt"
        android:singleLine="true"/>

</LinearLayout>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值