Android|ListView列表简单案例(含测试源码)

本文起源于安卓基础,旨在对ListView类做简单的案例型学习。ListView类是Android程序开发中经常用到的组件。该组件必须与适配器配合使用,由适配器提供显示样式和显示数据。常用方法如下表

常用方法 说明
ListView(Context context) 构造方法
setAdapter(ListAdapter adapter) 设置数组选项的适配器
addHeaderView(View v) 设置列表项目的头部
addFooterView(View v) 设置列表项目的底部
setOnItemClickListener
(AdapterView.OniItemClickListener listener)
注册单击选项时执行的方法,该方法继承于父类android.widger.AdapterView

程序测试效果

在这里插入图片描述
将选中的列表项目进行打印输出,运用到一个TextView和一个ListView类

程序实现步骤

创建项目,项目名为My thirApplication

在这里插入图片描述
点进Project—>Empty Activity—>然后名字改下,finish即可。成功之后,点击箭头运行程序。
在这里插入图片描述
程序正常可以跑成功hello world字样,下面我们继续

布局activity_main.xml

这一个步骤根据界面搭建,把相应的基础控件做好,剩下的我们交给Java来做。后面会对activity_main内容做详细讲解

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="智慧校园"
        android:textSize="24sp" />
    <ListView
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:id="@+id/ListView01"/>

</LinearLayout>

LinearLayout详解

它是一种线性布局,做下简单的配置,包括width与height,方向为垂直。根据内容不同方向就又会有不同。

TextView详解

除了简单的width与height填充父元素,和高度包含自身内容大小外,TextView在效果展示是写有“智慧校园”,因此它的text就有智慧校园字样

ListView详解

除了简单的width与height之外,取个id后面,java文件会根据这个进行操作。

搭建MainActivity.java内容

里面继承监听器事件,激活获得id,申请数组适配器放在里面,最后点击每一个list会打印消息。因此代码如下:

package com.example.mythirapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements AdapterView.OnItemClickListener {
    ListView list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        list = (ListView)findViewById(R.id.ListView01);
        String[] data ={
                "企业会话",
                "办公邮件",
                "财务信息查询",

        };
        //为ListView设置数组适配器ArrayAdapter
        list.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,data));
        //为ListView设置列表选项监听器
        list.setOnItemClickListener(this);
    }


    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        Toast.makeText(this,"你选择的项目是:" + ((TextView)arg1).getText(),Toast.LENGTH_SHORT).show();
    }
}

运行程序

效果最后就可以变成这样
在这里插入图片描述

总结

  • 创建新项目,跑起hello world
  • 搭建布局文件有一个TextView和ListView
  • 修改java文件
  • 运行程序,收获喜悦

希望博文能对大家有所帮助。

原创文章 247 获赞 92 访问量 2万+

猜你喜欢

转载自blog.csdn.net/m0_37149062/article/details/106095977
今日推荐