• Android学习总结——适配器


      适配器是AdapterView视图(如ListView - 列表视图控件、Gallery - 缩略图浏览器控件、GridView - 网格控件、Spinner - 下拉列表控件、AutoCompleteTextView - 自动提示文本框、ExpandableListView - 支持展开/收缩功能的列表控件等)与数据之间的桥梁,用来处理数据并将数据绑定到AdapterView上。
      android提供多种适配器,开发时可以针对数据源的不同采用最方便的适配器,也可以自定义适配器完成复杂功能。

    1. String[]: ArrayAdapter

    2. List<Map<String,?>>: SimpleAdapter

    3. 数据库Cursor: SimpleCursorAdapter

    一.ArrayAdapter

    使用ArrayAdapter(数组适配器),需要把数据放入一个数组以便显示。
    android.R.layout.simple_list_item_1是系统定义好的布局文件只显示一行文字。

    1.用静态字符数组常量来给ArrayAdapter 赋值。 优点,直接用数组写入,数据量大建议使用。

    static final String[] list="...";
    
    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,R.layout.list_item,list);

    2.在程序中给ArrayAdapter 赋值。优点:可以在程序中灵活写入。

    ArrayList<String> list = new ArrayList<String>();
    list.add("数据1");
    list.add("数据N");
    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,R.layout.list_item,list);

    3.使用国际化接口 字符数组来 给ArrayAdapter 赋值。优点:提供的组件的选项可以国际化。

    目录【res】→【values】→【strings.xml】添加

    <string-array name="letter">
      <item>A</item>
      <item>B</item>
      <item>C</item>
      <item>D</item>
    </string-array>
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,R.array.letter,android.R.layout.my_list_item)//只需要显示
    
    ArrayAdapter<CharSequence> adapter = new ArrayAdapter(this,android.R.layout.my_list_item,Arrays.asList(getResources().getTextArray(R.array.letter)))//允许动态增删

    二.SimpleAdapter

    SimpleAdapter能定义各种各样的布局出来,可以放上ImageView(图片),还可以放上Button(按钮),CheckBox(复选框)

    布局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"
        android:weightSum="1">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="30dp"
            android:text="chushizhi"
            android:id="@+id/txtPhoneNumber"/>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="right">
    
            <Button
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:background="@drawable/call"
                android:id="@+id/btn_Call"/>
            <Button
                android:layout_width="40dp"
                android:layout_height="35dp"
                android:layout_marginLeft="30dp"
                android:layout_marginRight="15dp"
                android:background="@drawable/message"
                android:id="@+id/btn_Message"/>
    
        </LinearLayout>
    </LinearLayout>

    代码:

    package com.example.sssss.listview;
    
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.ListView;
    import android.widget.SimpleAdapter;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
        private ListView listView;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            listView= (ListView) findViewById(R.id.listView1);
    
            //数据
            Map<String,String> map1=new HashMap<String,String>();
            map1.put("phoneNumber","18468182835");
            map1.put("call","");
            map1.put("message","");
    
            Map<String,String> map2=new HashMap<String,String>();
            map2.put("phoneNumber","18787174541");
            map2.put("call","");
            map2.put("message","");
    
            Map<String,String> map3=new HashMap<String,String>();
            map3.put("phoneNumber","18468181959");
            map3.put("call","");
            map3.put("message","");
    
            List<Map<String,String>> list=new ArrayList<Map<String,String>>();
            list.add(map1);
            list.add(map2);
            list.add(map3);
    
            //适配器
            SimpleAdapter adapter=new SimpleAdapter(
                    this,
                    list,
                    R.layout.layout_item,
                    new String[]{"phoneNumber","call","message"},
                    new int[]{R.id.txtPhoneNumber,R.id.btn_Call,R.id.btn_Message});
    //可以想成把一个放好数据的布局文件放入本界面中 listView.setAdapter(adapter);

    }
  • 相关阅读:
    T-sql for xml path使用
    解决 SQL Server2012附加出错的问题
    安装应用程序 报“ 997 重叠 I/O 操作在进行中”错解决办法
    使用QQ互联登录应用
    monogdb windows环境下 安装及使用简单示例
    idle-实现清屏
    colorscheme-如何vim颜色风格
    android-从官网下拉源码(ubuntu)
    hq-源码编译
    文件目录进入终端
  • 原文地址:https://www.cnblogs.com/xch-yang/p/5829499.html
Copyright © 2020-2023  润新知