• 008_01_SimpleAdapter的用法


    封装数据

     1 package com.example.bean;
     2 
     3  
     4 public class UserPhone {
     5 
     6      private int id ;
     7      private String name;
     8      private String telephone;
     9     public int getId() {
    10         return id;
    11     }
    12     public void setId(int id) {
    13         this.id = id;
    14     }
    15     public String getName() {
    16         return name;
    17     }
    18     public void setName(String name) {
    19         this.name = name;
    20     }
    21     public String getTelephone() {
    22         return telephone;
    23     }
    24     public void setTelephone(String telephone) {
    25         this.telephone = telephone;
    26     }
    27     @Override
    28     public String toString() {
    29         return "UserPhone [id=" + id + ", name=" + name + ", telephone="
    30                 + telephone + "]";
    31     }
    32     public UserPhone(int id, String name, String telephone) {
    33         super();
    34         this.id = id;
    35         this.name = name;
    36         this.telephone = telephone;
    37     }
    38     public UserPhone() {
    39         super();
    40 
    41     }     
    42 }

    创建数据库及tables

     1 package com.example.day08_adapter;
     2 
     3 import android.content.ContentValues;
     4 import android.content.Context;
     5 import android.database.sqlite.SQLiteDatabase;
     6 import android.database.sqlite.SQLiteDatabase.CursorFactory;
     7 import android.database.sqlite.SQLiteOpenHelper;
     8 import android.util.Log;
     9 
    10 public class MydbOpenHelper extends SQLiteOpenHelper {
    11 
    12     public MydbOpenHelper(Context context, String name, CursorFactory factory,
    13             int version) {
    14         super(context, name, factory, version);
    15     }
    16 
    17     @Override
    18     public void onCreate(SQLiteDatabase db) {
    19         String createtable = "create table userphone(id int, name varchar(20), telephone char(11))";
    20         db.execSQL(createtable);
    21         
    22         ContentValues c = new ContentValues();
    23         for(int i=1; i<50; i++){
    24             c.put("id", i);
    25             c.put("name", "user" + i);
    26             if(i<10)
    27                 c.put("telephone", "1800000000" + i);
    28             else{
    29                 c.put("telephone", "1811111111" + i);
    30             }
    31             db.insert("userphone", null, c);
    32         }
    33         System.out.println("MydbOpenHelper.onCreate()");
    34     }
    35 
    36     @Override
    37     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    38         Log.i("MydbOpenHelper", "oldervserion"+oldVersion+"newversion"+newVersion);
    39         System.out.println("MydbOpenHelper.onUpgrade()");
    40     }
    41 }
    
     1 package com.example.day08_adapter;
     2 
     3 import java.util.ArrayList;
     4 import java.util.HashMap;
     5 import java.util.List;
     6 import java.util.Map;
     7 
     8 import com.example.bean.UserPhone;
     9 
    10 import android.app.Activity;
    11 import android.database.Cursor;
    12 import android.database.sqlite.SQLiteDatabase;
    13 import android.os.Bundle;
    14 import android.util.Log;
    15 import android.widget.ListView;
    16 import android.widget.SimpleAdapter;
    17 
    18 public class MainActivity extends Activity {
    19     
    20     List<UserPhone> list = new ArrayList<UserPhone>();
    21     UserPhone userPhone;
    22 
    23     @Override
    24     protected void onCreate(Bundle savedInstanceState) {
    25         super.onCreate(savedInstanceState);
    26         setContentView(R.layout.activity_main);
    27         
    28         MydbOpenHelper helper = new MydbOpenHelper(this, "telephone.db", null, 1);
    29         SQLiteDatabase db = helper.getReadableDatabase();
    30         Cursor c = db.rawQuery("select * from userphone", null);
    31         
    32         while (c.moveToNext()){
    33             int id = c.getInt(0);
    34             String name = c.getString(1);
    35             String telephone = c.getString(2);
    36             
    37             Log.i("test", id + "," + name + "," + telephone);
    38             userPhone = new UserPhone(id, name, telephone);
    39             list.add(userPhone);
    40             }
    41         db.close();
    42         
    43         ListView listView = (ListView) findViewById(R.id.listview);
    44         
    45         List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
    46         
    47         Map<String, Object> map1 = new HashMap<String, Object>();
    48         map1.put("header", R.drawable.a);
    49         map1.put("name", "zhangsan");
    50         data.add(map1);
    51         
    52         Map<String, Object> map2 = new HashMap<String, Object>();
    53         map2.put("header", R.drawable.b);
    54         map2.put("name", "lisi");
    55         data.add(map2);
    56         
    57         Map<String, Object> map3 = new HashMap<String, Object>();
    58         map3.put("header", R.drawable.c);
    59         map3.put("name", "wangwu");
    60         data.add(map3);
    61         
    62         Map<String, Object> map4 = new HashMap<String, Object>();
    63         map4.put("header", R.drawable.d);
    64         map4.put("name", "zhaoliu");
    65         data.add(map4);
    66         
    67         Map<String, Object> map5 = new HashMap<String, Object>();
    68         map5.put("header", R.drawable.e);
    69         map5.put("name", "jiangfeng");
    70         data.add(map5);
    71         
    72         Map<String, Object> map6 = new HashMap<String, Object>();
    73         map6.put("header", R.drawable.f);
    74         map6.put("name", "xiongda");
    75         data.add(map6);
    76         
    77         SimpleAdapter sadapter = new SimpleAdapter(this, data, R.layout.array_adapter,
    78                 new String[]{"header", "name"}, new int[]{R.id.iv_header, R.id.tv_name});
    79         
    80         listView.setAdapter(sadapter);
    81         }
    82 }

    activity_main.xml

     1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     xmlns:tools="http://schemas.android.com/tools"
     3     android:layout_width="match_parent"
     4     android:layout_height="match_parent"
     5     android:paddingBottom="@dimen/activity_vertical_margin"
     6     android:paddingLeft="@dimen/activity_horizontal_margin"
     7     android:paddingRight="@dimen/activity_horizontal_margin"
     8     android:paddingTop="@dimen/activity_vertical_margin"
     9     tools:context="com.example.day08_adapter.MainActivity" 
    10     android:orientation="vertical">
    11 
    12     <TextView
    13         android:layout_width="wrap_content"
    14         android:layout_height="wrap_content"
    15         android:text="@string/hello_world" />
    16 <!--     <ScrollView
    17         android:layout_width="wrap_content"
    18         android:layout_height="wrap_content">
    19          <LinearLayout
    20         android:id="@+id/linearlayout"
    21         android:layout_width="wrap_content"
    22         android:layout_height="wrap_content"
    23         android:orientation="vertical"
    24         ></LinearLayout>        
    25     </ScrollView> -->
    26     
    27     <ListView 
    28         android:id="@+id/listview"
    29         android:layout_width="fill_parent"
    30         android:layout_height="wrap_content"
    31         android:dividerHeight="5dp"
    32         ></ListView>
    33    
    34 </LinearLayout>

    array_adapter.xml

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     android:layout_width="match_parent"
     4     android:layout_height="match_parent"
     5     android:orientation="horizontal" >
     6     
     7     <ImageView 
     8           android:id="@+id/iv_header"
     9           android:layout_width="50dp"
    10           android:layout_height="50dp"
    11         />
    12     <TextView 
    13           android:id="@+id/tv_name"
    14           android:layout_width="wrap_content"
    15           android:layout_height="wrap_content"
    16         />
    17     
    18     
    19 </LinearLayout>

    效果图:

    物随心转,境由心造,一切烦恼皆由心生。
  • 相关阅读:
    【应用】SVG饼状图
    【应用】图片翻转js
    userData IE
    cookie
    Local storage htm5
    全局ajax事件
    jQuery ajax序列化函数
    jQuery ajax中的get请求方法汇总
    $.ajax()
    nodejs安装错误
  • 原文地址:https://www.cnblogs.com/woodrow2015/p/4515455.html
Copyright © 2020-2023  润新知