Android Studio实现简单ListView

效果图

MainActivity

java 复制代码
package com.example.listviewtest;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.ListView;

import com.example.listviewtest.adapter.PartAdapter;
import com.example.listviewtest.bean.PartBean;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private ListView mListVie;
    private List<PartBean> mPartBeanList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mListVie = findViewById(R.id.lv);
        for (int i = 0; i < 100; i++) {
            mPartBeanList.add(new PartBean(i + 1, "材料" + i, 1 + i, 10 + i * 10, "材料厂" + i, i));
        }
        mListVie.setAdapter(new PartAdapter(MainActivity.this, mPartBeanList));
    }
}

PartBean 建议重新建一个软件包

java 复制代码
package com.example.listviewtest.bean;

public class PartBean {
    private int mId;
    private String mPartName;
    private int mPrice;
    private int mNum;
    private String mSuppier;
    private int mArea;

    public PartBean(int id, String partName, int price, int num, String suppier, int area) {
        mId = id;
        mPartName = partName;
        mPrice = price;
        mNum = num;
        mSuppier = suppier;
        mArea = area;
    }

    public int getId() {
        return mId;
    }

    public void setId(int id) {
        mId = id;
    }

    public String getPartName() {
        return mPartName;
    }

    public void setPartName(String partName) {
        mPartName = partName;
    }

    public int getPrice() {
        return mPrice;
    }

    public void setPrice(int price) {
        mPrice = price;
    }

    public int getNum() {
        return mNum;
    }

    public void setNum(int num) {
        mNum = num;
    }

    public String getSuppier() {
        return mSuppier;
    }

    public void setSuppier(String suppier) {
        mSuppier = suppier;
    }

    public int getArea() {
        return mArea;
    }

    public void setArea(int area) {
        mArea = area;
    }

}

PartAdapter 适配器 建议重新建一个适配器包

java 复制代码
package com.example.listviewtest.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.listviewtest.R;
import com.example.listviewtest.bean.PartBean;

import java.util.List;

public class PartAdapter extends BaseAdapter {
    private Context mContext;
    private List<PartBean> mPartBeanList;

    public PartAdapter(Context context, List<PartBean> partBeanList) {
        mContext = context;
        mPartBeanList = partBeanList;
    }

    @Override
    public int getCount() {
        return mPartBeanList == null ? 0 : mPartBeanList.size();
    }

    @Override
    public Object getItem(int i) {
        return getItem(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (view == null) {
            view = View.inflate(mContext, R.layout.pat_manger_item, null);
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
        PartBean partBean = mPartBeanList.get(i);
        viewHolder.mPartId.setText(String.valueOf(partBean.getId()));
        viewHolder.mPartName.setText(partBean.getPartName());
        viewHolder.mPrice.setText(String.valueOf(partBean.getPrice()));
        viewHolder.mNum.setText(String.valueOf(partBean.getNum()));
        viewHolder.mSuppier.setText(partBean.getSuppier());
        viewHolder.mArea.setText(String.valueOf(partBean.getArea()));
        return view;
    }

    static
    class ViewHolder {
        View mView;
        TextView mPartId;
        TextView mPartName;
        TextView mPrice;
        TextView mNum;
        TextView mSuppier;
        TextView mArea;

        ViewHolder(View view) {
            this.mView = view;
            this.mPartId = view.findViewById(R.id.partId);
            this.mPartName = (TextView) view.findViewById(R.id.partName);
            this.mPrice = (TextView) view.findViewById(R.id.price);
            this.mNum = (TextView) view.findViewById(R.id.num);
            this.mSuppier = (TextView) view.findViewById(R.id.suppier);
            this.mArea = (TextView) view.findViewById(R.id.area);
        }
    }
}

activity_main.xml 顶部标题和 ListView

java 复制代码
<?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=".MainActivity"
    android:layout_margin="5dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#BBD5E1">

        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="ID"
            android:textSize="14dp" />

        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="材料名称"
            android:textSize="14dp" />
        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="价格"
            android:textSize="14dp" />
        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="数量"
            android:textSize="14dp" />
        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="供应商"
            android:textSize="14dp" />
        <TextView
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="占地"
        android:textSize="14dp" />

    </LinearLayout>

    <ListView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>

pat_manger_item ListView中数据的展示格式

java 复制代码
<?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">
    <TextView
        android:id="@+id/partId"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="ID"
        android:textSize="10dp" />

    <TextView
        android:id="@+id/partName"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="材料名称"
        android:textSize="10dp" />
    <TextView
        android:id="@+id/price"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="价格"
        android:textSize="10dp" />
    <TextView
        android:id="@+id/num"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="数量"
        android:textSize="10dp" />
    <TextView
        android:id="@+id/suppier"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="供应商"
        android:textSize="10dp" />
    <TextView
        android:id="@+id/area"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="占地"
        android:textSize="10dp" />
</LinearLayout>
相关推荐
m0_7482359536 分钟前
CentOS 7使用RPM安装MySQL
android·mysql·centos
ac-er88884 小时前
Yii框架中的队列:如何实现异步操作
android·开发语言·php
xqlily5 小时前
通过 Visual Studio Code 启动 IPython
ide·vscode·ipython
流氓也是种气质 _Cookie6 小时前
uniapp 在线更新应用
android·uniapp
梓沂6 小时前
idea修改模块名导致程序编译出错
java·ide·intellij-idea
zhangphil8 小时前
Android ValueAnimator ImageView animate() rotation,Kotlin
android·kotlin
dot.Net安全矩阵8 小时前
拒绝 Github 投毒,通过 Sharp4SuoBrowser 分析 Visual Studio 隐藏文件
ide·安全·web安全·github·.net·.netcore·visual studio
徊忆羽菲9 小时前
CentOS7使用源码安装PHP8教程整理
android
江西理工大学小杨9 小时前
VScode连接远程Linux服务器环境配置
ide·vscode·编辑器
编程、小哥哥10 小时前
python操作mysql
android·python