list

1 activaty_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="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:textSize="26sp" />

    <ListView
        android:id="@+id/list_view"
        android:layout_width="match_parent"
        android:layout_height="658dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="结束!"
        android:textSize="26sp" />

</LinearLayout>

2 listview_item

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/fruit_name"
        android:layout_gravity="center_vertical"
        android:textSize="30sp"
        android:textColor="#000000"
        android:text="name"
        android:layout_marginLeft="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <TextView
        android:id="@+id/fruit_price"
        android:layout_gravity="center_vertical"
        android:textColor="#ff0000"
        android:text="price"
        android:textSize="30sp"
        android:layout_marginLeft="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <TextView
        android:id="@+id/fruit_id"
        android:layout_gravity="center_vertical"
        android:textColor="#ff0000"
        android:text="1"
        android:textSize="30sp"
        android:layout_marginLeft="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>

3 Activity_main

package com.example.listview;

import androidx.appcompat.app.AppCompatActivity;

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

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

public class MainActivity extends AppCompatActivity {
    ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //第一步:绑定控件
        listView=findViewById(R.id.list_view);
        //第二步:准别数据
        List<MyData> data = new ArrayList<>();
        for(int i=0;i<10;i++){
            MyData pineapple=new MyData(i,"菠萝","¥16.9 元/KG");
            data.add(pineapple);
        }
        //第三步:设计每一个列表项的子布局,即listview_xml文件
        //第五步:定义适配器 控件 -桥梁-数据
        ListViewAdapter adapter=new ListViewAdapter(MainActivity.this,R.layout.listview_item,data);
        listView.setAdapter(adapter);
    }
}

4 listviewadapter

package com.example.listview;

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

import java.util.List;

public class ListViewAdapter extends ArrayAdapter<MyData> {
    public  ListViewAdapter(Context context, int resource, List<MyData> objects) {
        super(context, resource, objects);
    }
    //每个子项被滚动到屏幕内的时候会被调用
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MyData data=getItem(position);//得到当前项的 Fruit 实例
        //为每一个子项加载设定的布局
        View view= LayoutInflater.from(getContext()).inflate(R.layout.listview_item,parent,false);

        //-----------------------待修改的部分开始-----------------------------------------------------
        //分别获取 image view 和 textview 的实例
        TextView id=view.findViewById(R.id.fruit_id);
        TextView fruitname =view.findViewById(R.id.fruit_name);
        TextView fruitprice=view.findViewById(R.id.fruit_price);

        fruitname.setText(data.getName());
        fruitprice.setText(data.getPrice());
        id.setText(String.valueOf(data.getImageID()));

        //-----------------------待修改的部分结束-----------------------------------------------------

        return view;
    }
}

5 Mydata

package com.example.listview;

public class MyData {
    private int imageID;
    private String name;
    private String price;
    public int getImageID() {
        return imageID;
    }
    public String getName() {
        return name;
    }
    public String getPrice() {
        return price;
    }
    public MyData(int imageID, String name, String price) {
        this.imageID = imageID;
        this.name = name;
        this.price = price;
    }
}

posted @ 2023-06-18 09:42  洛殊  阅读(34)  评论(0编辑  收藏  举报