• 购物


    <?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">
    
        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:background="#FF3949AB"
            android:gravity="center"
            android:text="购物清单"
            android:textColor="#FFC0CA33"
            android:textSize="25sp" />
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical">
    
            <ListView
                android:id="@+id/fruit_item"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </LinearLayout>
    </LinearLayout>
    <?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"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <ImageView
            android:id="@+id/imageView"
            android:layout_width="100dp"
            android:layout_height="100dp"
            app:srcCompat="@mipmap/caomei" />
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:gravity="left|center_vertical"
            android:orientation="vertical"
            android:scrollbarSize="8dp">
    
            <TextView
                android:id="@+id/textName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="草莓" />
    
            <TextView
                android:id="@+id/textPrice"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="价格:20元1斤"
                android:textSize="16sp" />
        </LinearLayout>
    
    </LinearLayout>
    package com.example.shoppinglist;
    
    public class Fruit {
        private int imageId;
        private String Name,Price;
        public Fruit(String Name, String Price, int imageId) {
            super();
            this.imageId = imageId;
            this.Name = Name;
            this.Price = Price;
        }
    
        public int getImageId() {
            return imageId;
        }
    
        public void setImageId(int imageId) {
            this.imageId = imageId;
        }
    
        public String getName() {
            return Name;
        }
    
        public void setName(String name) {
            Name = name;
        }
    
        public String getPrice() {
            return Price;
        }
    
        public void setPrice(String price) {
            Price = price;
        }
    
        public Object getItem(int position){
            return position;
        }
    
    }
    package com.example.shoppinglist;
    
    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.ImageView;
    import android.widget.TextView;
    
    import java.util.List;
    
    public class FruitAdapte extends ArrayAdapter {
        int resourceId;
        public FruitAdapte(Context context, int resource, List<Fruit> objects){
            super(context, resource,objects);
            resourceId=resource;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent){
            Fruit fruit = (Fruit)getItem(position);
            View view = LayoutInflater.from(getContext()).inflate(resourceId,null);
            ImageView iv = (ImageView)view.findViewById(R.id.imageView);
            TextView tv = (TextView)view.findViewById(R.id.textName);
            TextView pv = (TextView)view.findViewById(R.id.textPrice);
            iv.setImageResource(fruit.getImageId());
            tv.setText((CharSequence) fruit.getName());
            pv.setText((CharSequence)fruit.getPrice());
    
            return  view;
        }
    
    }
    package com.example.shoppinglist;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.ListView;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class MainActivity extends Activity {
        final List<Fruit> fruitList = new ArrayList<Fruit>();
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            initFruits();
            FruitAdapte adapter = new FruitAdapte(this,R.layout.item,fruitList);
            ListView lv = (ListView)findViewById(R.id.fruit_item);
            lv.setAdapter(adapter);
    
        }
    
        public void initFruits(){
            Fruit caomei = new Fruit("草莓","20元1斤",R.mipmap.caomei);
            fruitList.add(caomei);
            Fruit huoluoguo = new Fruit("火龙果","15元1斤",R.mipmap.huolongguo);
            fruitList.add(huoluoguo);
            Fruit lanmei = new Fruit("蓝莓","5元1斤",R.mipmap.lanmei);
            fruitList.add(lanmei);
            Fruit ningmeng = new Fruit("柠檬","3元1斤",R.mipmap.ningmeng);
            fruitList.add(ningmeng);
            Fruit pingguo = new Fruit("苹果","4.2元1斤",R.mipmap.pingguo);
            fruitList.add(pingguo);
            Fruit yangtao = new Fruit("杨桃","18.5元1斤",R.mipmap.yangtao);
            fruitList.add(yangtao);
    
        }
    }

  • 相关阅读:
    java并发编程的艺术笔记-第二章
    java并发编程的艺术笔记-第一章
    Class字节码的加载
    Maven学习笔记(1)
    深入分析java web技术内幕读书笔记-第一章
    VMware虚拟机扩展内存和磁盘
    【避坑】for循环删除列表元素的注意事项--remove函数逆序删除
    虚拟机无法上网问题解决
    jmeter变量跨线程组使用--设置全局变量
    常见慢查询原因
  • 原文地址:https://www.cnblogs.com/menfanbo/p/14167663.html
Copyright © 2020-2023  润新知