• Android——ListView


    1.ArryAdapter:

    arry_adapter的layout文件:

    1 <?xml version="1.0" encoding="utf-8"?>
    2 
    3 
    4     <TextView xmlns:android="http://schemas.android.com/apk/res/android"
    5         android:layout_width="match_parent"
    6         android:layout_height="wrap_content"
    7         android:textSize="20sp"
    8     android:paddingTop="10dp"
    9     android:paddingBottom="10dp"/>

    activity_test6的layout文件:

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     xmlns:tools="http://schemas.android.com/tools"
     4     android:layout_width="match_parent"
     5     android:layout_height="match_parent"
     6     android:paddingBottom="@dimen/activity_vertical_margin"
     7     android:paddingLeft="@dimen/activity_horizontal_margin"
     8     android:paddingRight="@dimen/activity_horizontal_margin"
     9     android:paddingTop="@dimen/activity_vertical_margin"
    10     tools:context="com.hanqi.testapp2.TestActivity6">
    11 
    12     <ListView
    13         android:layout_width="match_parent"
    14         android:layout_height="match_parent"
    15         android:id="@+id/lv_1"></ListView>
    16 </LinearLayout>

    java类:

     1 package com.hanqi.testapp2;
     2 
     3 import android.os.Bundle;
     4 import android.support.v7.app.AppCompatActivity;
     5 import android.widget.ArrayAdapter;
     6 import android.widget.ListView;
     7 
     8 public class TestActivity6 extends AppCompatActivity {
     9 
    10     ListView lv_1;
    11     @Override
    12     protected void onCreate(Bundle savedInstanceState) {
    13         super.onCreate(savedInstanceState);
    14         setContentView(R.layout.activity_test6);
    15         ListView lv_1 = (ListView)findViewById(R.id.lv_1);
    16 
    17         //1.数据集合  layout文件
    18         String[] strings  = {"A1","A2","A3","A4","A5","A6","A7","A8","A9",
    19                 "A1","A2","A3","A4","A5","A6","A7","A8","A9"};
    20         //2.创建Adpter
    21         ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,R.layout.arry_adapter,strings);
    22         //3.绑定到ListView
    23         lv_1.setAdapter(arrayAdapter);
    24     }
    25 }

    效果图:

    2.SimpleAdapter:

    simple_adapter的layout文件:

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     android:orientation="horizontal" android:layout_width="match_parent"
     4     android:layout_height="match_parent">
     5 
     6     <ImageView
     7         android:layout_width="70dp"
     8         android:layout_height="70dp"
     9         android:src="@drawable/f1"
    10         android:id="@+id/iv_2"/>
    11     <LinearLayout
    12         android:layout_width="0dp"
    13         android:layout_height="match_parent"
    14         android:orientation="vertical"
    15         android:layout_weight="1"
    16         android:layout_marginLeft="20dp"
    17         android:gravity="center_vertical">
    18         <TextView
    19             android:layout_width="match_parent"
    20             android:layout_height="wrap_content"
    21             android:text="名字=aaa"
    22             android:id="@+id/tv_7"/>
    23         <TextView
    24             android:layout_width="match_parent"
    25             android:layout_height="wrap_content"
    26             android:text="内容=aaa"
    27             android:id="@+id/tv_8"/>
    28     </LinearLayout>
    29 </LinearLayout>

    activity_test7的layout文件:

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     xmlns:tools="http://schemas.android.com/tools"
     4     android:layout_width="match_parent"
     5     android:layout_height="match_parent"
     6     android:paddingBottom="@dimen/activity_vertical_margin"
     7     android:paddingLeft="@dimen/activity_horizontal_margin"
     8     android:paddingRight="@dimen/activity_horizontal_margin"
     9     android:paddingTop="@dimen/activity_vertical_margin"
    10     tools:context="com.hanqi.testapp2.TestActivity7">
    11 
    12     <ListView
    13         android:layout_width="match_parent"
    14         android:layout_height="match_parent"
    15         android:id="@+id/lv_2"></ListView>
    16 </LinearLayout>

    java类:

     1 package com.hanqi.testapp2;
     2 
     3 import android.os.Bundle;
     4 import android.support.v7.app.AppCompatActivity;
     5 import android.widget.ListView;
     6 import android.widget.SimpleAdapter;
     7 
     8 import java.util.ArrayList;
     9 import java.util.HashMap;
    10 import java.util.List;
    11 import java.util.Map;
    12 
    13 public class TestActivity7 extends AppCompatActivity {
    14 
    15     ListView lv_2;
    16     @Override
    17     protected void onCreate(Bundle savedInstanceState) {
    18         super.onCreate(savedInstanceState);
    19         setContentView(R.layout.activity_test7);
    20         lv_2 = (ListView)findViewById(R.id.lv_2);
    21         //1.数据集合  layout
    22         List<Map<String,Object>> lm = new ArrayList<Map<String,Object>>();
    23         Map<String,Object> map = new HashMap<String,Object>();
    24         map.put("img",R.drawable.f1);
    25         map.put("name","美食1");
    26         map.put("content","美食1的介绍");
    27         lm.add(map);
    28 
    29         map = new HashMap<String,Object>();
    30         map.put("img",R.drawable.f2);
    31         map.put("name","美食2");
    32         map.put("content","美食2的介绍");
    33         lm.add(map);
    34 
    35         map = new HashMap<String,Object>();
    36         map.put("img",R.drawable.f3);
    37         map.put("name","美食3");
    38         map.put("content","美食3的介绍");
    39         lm.add(map);
    40 
    41         map = new HashMap<String,Object>();
    42         map.put("img",R.drawable.f4);
    43         map.put("name","美食4");
    44         map.put("content","美食4的介绍");
    45         lm.add(map);
    46 
    47         map = new HashMap<String,Object>();
    48         map.put("img",R.drawable.f5);
    49         map.put("name","美食5");
    50         map.put("content","美食5的介绍");
    51         lm.add(map);
    52 
    53         map = new HashMap<String,Object>();
    54         map.put("img",R.drawable.f6);
    55         map.put("name","美食6");
    56         map.put("content","美食6的介绍");
    57         lm.add(map);
    58 
    59         map = new HashMap<String,Object>();
    60         map.put("img",R.drawable.f8);
    61         map.put("name","美食8");
    62         map.put("content","美食8的介绍");
    63         lm.add(map);
    64 
    65         map = new HashMap<String,Object>();
    66         map.put("img",R.drawable.f9);
    67         map.put("name","美食9");
    68         map.put("content","美食9的介绍");
    69         lm.add(map);
    70 
    71         map = new HashMap<String,Object>();
    72         map.put("img",R.drawable.f10);
    73         map.put("name","美食10");
    74         map.put("content","美食10的介绍");
    75         lm.add(map);
    76         //数组 key的数组
    77         String[]strings = {"img","name","content"};
    78         int[]ids = {R.id.iv_2,R.id.tv_7,R.id.tv_8};
    79         //2.创建Adapter
    80         SimpleAdapter simpleAdapter = new SimpleAdapter(this,
    81                 lm,R.layout.simple_adapter,strings,ids);
    82         lv_2.setAdapter(simpleAdapter);
    83     }
    84 }

    效果图:

  • 相关阅读:
    自己去写的一些方法
    pyhon,统计一个字符串,在一个文本里面的个数,写查慢sql时用到的
    为什么调用类方法,老是报缺少位置参数 self
    链接mongodb 使用pymong
    django框架学习+怎么连接mongdb,和普通的mysql
    前端写ajax请求,放在javascript里面
    django之注释,
    django的 render和 返回httpsresponse的区别
    windows系统安装mongodb数据库。
    解决SQL Server Management Studio 18(SSMS)打开闪退的问题
  • 原文地址:https://www.cnblogs.com/hanazawalove/p/5502054.html
Copyright © 2020-2023  润新知