zoukankan      html  css  js  c++  java
  • Android入门:ListView(SimpleAdapter实现)

    ListView是类似于将一个界面分为一行一行,如下图:



    注意:listView.getItemAtPosition(int pos)内部调用了adapter.getItem(int position)方法,而每种适配器返回的类型都是不一样的:

    当SimpleAdapter返回Map<String,Object>

    SimpleCursorAdapter返回Cursor;

    继承BaseAdapter返回自己实现的类型;


    一般ListView都是用来显示列表的,一般列表的数据都是来自数据库的,因此我们这里假设前面已经实现了一个DBService类,里面存在pageQuery(int offset,int length);

    比如dbservice.pageQuery(3,5);表示跳过3个记录,插入5条记录;



    main.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="vertical" >
    
        <LinearLayout
            android:layout_width="wrap_content"//注意一定要wrap_content
            android:layout_height="wrap_content"//注意一定要wrap_content
            android:orientation="horizontal" >
            <TextView
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:text="ID" />
            <TextView
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:text="NAME" />
            <TextView
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:text="AGE" />
        </LinearLayout>
        <ListView
            android:id="@+id/listview"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent" >
        </ListView>
    
    </LinearLayout>

    item.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" >
        <TextView
            android:id="@+id/id1"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            />
        <TextView
            android:id="@+id/name"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            />
        <TextView
            android:id="@+id/age"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            />
    </LinearLayout>

    MainActivity.java


    package org.xiazdong.db;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import org.xiazdong.db.domain.Person;
    import org.xiazdong.db.service.DBService;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    import android.widget.ListView;
    import android.widget.SimpleAdapter;
    
    public class MainActivity extends Activity {
    	private ListView listView;
    	@Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            listView = (ListView)this.findViewById(R.id.listview);
            DBService service = new DBService(this);
            List<Person> persons = service.pageQuery(0, 10);
            List<HashMap<String,Object>> data = new ArrayList<HashMap<String,Object>>();
            for(Person person:persons){
            	HashMap<String,Object>map = new HashMap<String,Object>();
            	map.put("id", person.getId());
            	map.put("name", person.getName());
            	map.put("age", person.getAge());
            	data.add(map);
            }
            System.out.println(data);
            SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.item, new String[]{"id","name","age"}, new int[]{R.id.id1,R.id.name,R.id.age});
    //data表示显示的数据,一个Map为一行,List<Map>表示多行
    //R.layout.item表示一个item的布局
    //new String[]{"id","name","age"}表示将key="id"的值映射到R.id.id1上
            listView.setAdapter(adapter);
    	}
    }

    总结:SimpleAdapter不需要内部实现Adapter,只能实现每个item布局都一样的列表;

    二、设置每个Item的监听器


    SimpleAdapter:

    private OnItemClickListener listener = new OnItemClickListener(){
    	@Override
    	public void onItemClick(AdapterView<?> parent, View view, int position,//parent就是ListView,view表示Item视图,position表示数据索引
    			long id) {
    		ListView lv = (ListView)parent;
    		HashMap<String,Object> person = (HashMap<String,Object>)lv.getItemAtPosition(position);//SimpleAdapter返回Map
    		Toast.makeText(getApplicationContext(), person.toString(), Toast.LENGTH_SHORT).show();
    	}	
    };
    listView.setOnItemClickListener(listener);
    


    SimpleCursorAdapter:

    Cursor cursor = (Cursor)lv.getItemAtPosition(position);



  • 相关阅读:
    C++ linux 共享内存的学习
    C++11 result_of 学习
    C++线程池 基于Cpp98的实现 学习2 未完待续
    Piecewise Smooth Subdivision Surfaces with Normal Control 未完待续
    C++线程池 基于C的实现 学习1
    C++ 标准函数 。abort & typeid
    线段树(区间更新, 区间查询 ,线段染色)
    bfs(标记整个棋盘)
    stringstream流分割空格
    lcm与gcd
  • 原文地址:https://www.cnblogs.com/xiazdong/p/3058342.html
Copyright © 2011-2022 走看看