Dynamically add ListView sub-layout in Android Studio

Recently, I am writing to get the list of devices in the cloud platform through Android, and I need to dynamically update the ListView. So I wrote this note. Here you will learn how to add ListView dynamically! The following is the content of the blogger I borrowed from

Reference link: https://blog.csdn.net/lcp0633/article/details/127651720


foreword

  • Here my environment is: Android Studio Dolphin | 2021.3.1
  • We need to prepare: ListView, sub-layout Create a ListView sub-layout in the res/Layout directory, the entity class of the data, and the most important adapter, and finally the operation code of the main program
  • Renderings:

1. Create a ListView layout file

Little knowledge: How to set the distance between items of ListView?

android:divider="@null"
android:fadingEdge="none"
android:headerDividersEnabled="false"
android:footerDividersEnabled="false"
<!-- 用来控制List View之间的间距-->>
android:dividerHeight="10dp"

<?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="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".ui.main.SecondFragment">
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        
        <ListView
            android:id="@+id/list_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:divider="@null"
            android:dividerHeight="3dp"
            android:fadingEdge="none"
            android:footerDividersEnabled="false"
            android:headerDividersEnabled="false" />
    </LinearLayout>
    
</LinearLayout>

2. Create a sub-layout file of ListView

<?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="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/bg_message"
        android:orientation="vertical"
        android:padding="12dp">

        <TextView
            android:id="@+id/sensor_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="STM32"
            android:textColor="@color/be_2"
            android:textSize="25sp"
            android:textStyle="bold" />

        <LinearLayout

            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:paddingLeft="2dp"
                android:text="Uid:"
                android:textColor="@color/be_2"
                android:textSize="18sp" />

            <TextView
                android:id="@+id/sensor_uid"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:paddingLeft="10dp"
                android:text="49"
                android:textColor="@color/be_2" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:paddingLeft="2dp"
                android:text="Data:"
                android:textColor="@color/be_2"
                android:textSize="18sp" />

            <LinearLayout
                android:orientation="horizontal"
                android:layout_gravity="center"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
                <TextView
                    android:id="@+id/sensor_data"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:paddingLeft="10dp"
                    android:text="49"
                    android:textColor="@color/be_2" />
                <TextView
                    android:id="@+id/sensor_unit"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:paddingLeft="3dp"
                    android:text="%"
                    android:textColor="@color/be_2" />
            </LinearLayout>

        </LinearLayout>

    </LinearLayout>

</LinearLayout>

3. Create an entity class (here we use name, uid, data, unit)

package com.example.prodtech.tools.bean;

public class SensorBean {
    private String name;
    private String Uid;
    private String Data;
    private String Unit;

    public SensorBean(String name, String uid, String data, String unit) {
        this.name = name;
        Uid = uid;
        Data = data;
        Unit = unit;
    }

    public String getUnit() {
        return Unit;
    }

    public void setUnit(String unit) {
        Unit = unit;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getUid() {
        return Uid;
    }

    public void setUid(String uid) {
        Uid = uid;
    }

    public String getData() {
        return Data;
    }

    public void setData(String data) {
        Data = data;
    }
}

4. Create an adapter

We will not explain the adapter here https://www.cnblogs.com/AChenWeiqiangA/p/13093381.html

Each step has a detailed explanation, if you still don't understand, please leave a message and let us solve it together!

package com.example.prodtech.tools.adapter;

import android.content.Context;
import android.hardware.Sensor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.example.prodtech.R;
import com.example.prodtech.tools.bean.SensorBean;

import java.util.List;

public class SensorAdapter extends ArrayAdapter<SensorBean> {
    //用于存储Sensor对象数据
    private List<SensorBean> sensorBeanList;

    public SensorAdapter(Context context, int resource, List<SensorBean> objects) {
        super(context, resource, objects);
        sensorBeanList = objects;
        //发生了数据更改需要更新UI
        notifyDataSetChanged();
    }

    public void add(SensorBean sensorBean) {
        //将SensorBean对象添加到数据源
        sensorBeanList.add(sensorBean);
    }

    /**
     * 用于设置ListView中每一项内同和UI的显示
     * @param position 用于显示当前要处理List View中的项的位置
     * @param convertView 要显示的视图
     * @param parent 列表的父级视图
     * @return 返回的是视图
     */

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        SensorBean sensorBean = getItem(position);
        View view;
        Sensor sensor;
        if (convertView == null) {
            //创建一个视图列表对象,并其填充到列表项中
            view = LayoutInflater.from(getContext()).inflate(R.layout.list_sensor, parent,
                    false);
            sensor = new Sensor();
            sensor.sensor_name = view.findViewById(R.id.sensor_name);
            sensor.sensor_uid = view.findViewById(R.id.sensor_uid);
            sensor.sensor_data = view.findViewById(R.id.sensor_data);
            sensor.sensor_unit = view.findViewById(R.id.sensor_unit);
            //getView方法中的,SetTag方法把查找到的View缓存起来,方便多次使用
            view.setTag(sensor);

        } else {
            view = convertView;
            //getView方法中的,GetTag方法把这个数据取出来
            sensor = (Sensor) view.getTag();
        }
        //设置要显示的内容
        sensor.sensor_name.setText(sensorBean.getName());
        sensor.sensor_uid.setText(sensorBean.getUid());
        sensor.sensor_data.setText(sensorBean.getData());
        sensor.sensor_unit.setText(sensorBean.getUnit());
        //将生成的视图返回给GetView方法
        return view;
    }

    class Sensor {
        //定义了一个内部类,这个代码用于存储ListView中的每一项的数据
        TextView sensor_name, sensor_uid, sensor_data, sensor_unit;
    }
}

 Five, the operation of writing the main program

Here you can implement the operation of dynamically adding ListView, you can use click events and so on;


public class ActivitySensorPage extends AppCompatActivity {
    private List<SensorBean> sensorBeanList = new ArrayList<>();
    private SensorAdapter sensorAdapter;
    private SensorBean sensorBean;
    private ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.page_sensor);
        listView = findViewById(R.id.sensor_listView);
        sensorAdapter = new SensorAdapter(this, R.layout.list_sensor, sensorBeanList);
        listView.setAdapter(sensorAdapter);
        sensorBean = new SensorBean("123", "123", "123", "123");
        sensorAdapter.add(sensorBean);
    }                                                             
}

Summarize

This article mainly focuses on how to dynamically add ListView. I feel that bloggers, you can take a look, it is really good! ! https://blog.csdn.net/lcp0633/article/details/127651720

Guess you like

Origin blog.csdn.net/weixin_47024334/article/details/130860373