• Android中如何使用Listview


    第一步
    首先在xml文件中声明一个List View控件,并且标明id
    (这一步其实不用说,怕自学Android的小白不懂,就好比当初的我,哈哈)

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".ListView1">
    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    第二步:

    package com.example.myapplication;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.os.Bundle;
    import android.view.View;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
    
    public class ListView1 extends AppCompatActivity {
    
        private ListView listview1;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_list_view1);
            listview1=findViewById(R.id.listview);
    String [] messages={"张三","李四","王五"};
            ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(ListView1.this,R.layout.support_simple_spinner_dropdown_item,messages);
            listview1.setAdapter(arrayAdapter);
        }
    }
    
    

    声明一个List View控件listview1,给其赋值

    listview1=findViewById…

    声明一个数组,自定义类型的

    new一个ArrayAdapter(记得要带泛型,泛型的类型为你刚才声明数组的类型)
    new 后面的参数为(当前的类,固定的代码)
    给listview1填充数据
    listview1.setAdapter(这里面写你刚才new的ArrayAdapter的名字)

    InTheEnd
    shift+F10运行此程序

    在这里插入图片描述

  • 相关阅读:
    [CSS3 + HTML5] Modernizr
    [Javascript] Array methods in depth
    [Unit Testing] Node testing: Test api Get request
    [CSS3] Interactive Pseudo-Classes :link :visited :hover :active
    [Javascript] Intro to Recursion
    [Javascript ] Array methods in depth
    [Javascript] Advanced Reduce: Flatten, Flatmap and ReduceRight
    [Redux] Composition with Objects
    AI-Info-Micron-Insight:人如其食:人工智能和人类微生物组
    AI-Info-Micron-Insight:用内存解决方案演化神经网络智能
  • 原文地址:https://www.cnblogs.com/a1439775520/p/13076482.html
Copyright © 2020-2023  润新知