zoukankan      html  css  js  c++  java
  • Android笔记: ListView基本用法-ArrayAdapter

    ListView实现过程:

      新建适配器->添加数据源到适配器->视图加载适配器

    数据适配器:

      把复杂的数据(数组、链表、数据库、集合等)填充在制定的试图界面上。

    两种常用数据适配器

      ArrayAdapter 用于绑定一些格式单一的数据 数据源:集合或者数组

      SimpleAdapter 用于绑定格式复杂的数据 数据源:只能是特定风景的集合

    ArrayAdapter使用例子:

      ainActivity.java :

     1 package com.example.listviewdemo;
     2 
     3 import android.app.Activity;
     4 import android.os.Bundle;
     5 import android.widget.ArrayAdapter;
     6 import android.widget.ListView;
     7 import android.widget.SimpleAdapter;
     8 
     9 public class MainActivity extends Activity {
    10 
    11     private ListView mListView;
    12     // 1.新建适配器
    13     private ArrayAdapter<String> mArrAdapter;
    14 
    15     @Override
    16     protected void onCreate(Bundle savedInstanceState) {
    17         super.onCreate(savedInstanceState);
    18         setContentView(R.layout.activity_main);
    19         mListView = (ListView) findViewById(R.id.listView);
    20         // 2.添加数据源到适配器
    21         String[] mTestArrData = { "Test", "Test", "Test", "Test", "Test",
    22                 "Test", "Test", "Test" };
    23         mArrAdapter = new ArrayAdapter<String>(this,
    24                 android.R.layout.simple_list_item_1, mTestArrData);//android.R.layout.simple_list_item_1是Google提供的一种默认的listView的item样式,仅包含一个textView.
    25         // 3.视图加载适配器
    26         mListView.setAdapter(mArrAdapter);
    27     }
    28 }

      activity_main.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <ListView
            android:id="@+id/listView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </ListView>
    
    </RelativeLayout>

    效果展示:

  • 相关阅读:
    Vuex2.0+Vue2.0构建备忘录应用实践
    一步步构造自己的vue2.0+webpack环境
    .NET入行之工作前
    webpack入门之简单例子跑起来
    vue中,class、内联style绑定、computed属性
    wap问答系统工作总结
    ASP.NET Core Api网关Ocelot的中文文档
    在pom.xml中添加Spring依赖
    【java基础】从反射开始(Reflection)
    【java基础】 == 和 equals() 的区别
  • 原文地址:https://www.cnblogs.com/fortitude/p/4922719.html
Copyright © 2011-2022 走看看