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_548514773 小时前
2024.12.10——攻防世界Web_php_include
android·前端·php
凤邪摩羯3 小时前
Android-性能优化-03-启动优化-启动耗时
android
凤邪摩羯3 小时前
Android-性能优化-02-内存优化-LeakCanary原理解析
android
喀什酱豆腐3 小时前
Handle
android
m0_748232925 小时前
Android Https和WebView
android·网络协议·https
m0_748251725 小时前
Android webview 打开本地H5项目(Cocos游戏以及Unity游戏)
android·游戏·unity
带电的小王6 小时前
VSCode:VSCode安装 -- 最简洁的VSCode安装教程
ide·vscode·编辑器
m0_748254667 小时前
go官方日志库带色彩格式化
android·开发语言·golang
zhangphil7 小时前
Android使用PorterDuffXfermode模式PorterDuff.Mode.SRC_OUT橡皮擦实现“刮刮乐”效果,Kotlin(2)
android·kotlin
爱学测试的李木子7 小时前
从0到1搭建 Android 自动化 python+appium 环境
android·软件测试·python·测试工具·自动化