购物菜单
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical"> 6 7 <TextView 8 android:id="@+id/textView" 9 android:layout_width="match_parent" 10 android:layout_height="35dp" 11 android:background="#009688" 12 android:gravity="center" 13 android:text="购物清单" 14 android:textColor="#E91E63" 15 android:textSize="25sp" /> 16 17 <LinearLayout 18 android:layout_width="match_parent" 19 android:layout_height="wrap_content" 20 android:layout_weight="1" 21 android:orientation="vertical"> 22 23 <ListView 24 android:id="@+id/fruit_item" 25 android:layout_width="match_parent" 26 android:layout_height="match_parent" /> 27 </LinearLayout> 28 </LinearLayout>
1 package com.example.shoppinglist; 2 3 import androidx.appcompat.app.AppCompatActivity; 4 5 import android.app.Activity; 6 import android.os.Bundle; 7 import android.widget.ListView; 8 9 import java.util.ArrayList; 10 import java.util.List; 11 12 public class MainActivity extends Activity { 13 final List<Fruit> fruitList = new ArrayList<Fruit>(); 14 15 @Override 16 protected void onCreate(Bundle savedInstanceState) { 17 super.onCreate(savedInstanceState); 18 setContentView(R.layout.activity_main); 19 initFruits(); 20 FruitAdapte adapter = new FruitAdapte(this,R.layout.item,fruitList); 21 ListView lv = (ListView)findViewById(R.id.fruit_item); 22 lv.setAdapter(adapter); 23 24 } 25 26 public void initFruits(){ 27 Fruit annona squamosa = new Fruit("释迦果","20元1斤",R.mipmap.annona squamosa); 28 fruitList.add(annona squamosa); 29 Fruit apple= new Fruit("苹果","5元1斤",R.mipmap.apple); 30 fruitList.add(apple); 31 Fruit banana = new Fruit("香蕉","9元1斤",R.mipmap.banana); 32 fruitList.add(banana); 33 Fruit strawberry = new Fruit("草莓","30元1斤",R.mipmap.strawberry ); 34 fruitList.add(strawberry ); 35 Fruit orange= new Fruit("橙子","10元1斤",R.mipmap.orange); 36 fruitList.add(orange); 37 Fruit watermelon = new Fruit("西瓜","20元1斤",R.mipmap.watermelon); 38 fruitList.add(watermelon); 39 Fruit hami melon= new Fruit("哈密瓜","15元1斤",R.mipmap.hami melon); 40 fruitList.add(hami melon); 41 42 } 43 }
fruit
1 package com.example.shoppinglist; 2 3 public class Fruit { 4 private int imageId; 5 private String Name,Price; 6 public Fruit(String Name, String Price, int imageId) { 7 super(); 8 this.imageId = imageId; 9 this.Name = Name; 10 this.Price = Price; 11 } 12 13 public int getImageId() { 14 return imageId; 15 } 16 17 public void setImageId(int imageId) { 18 this.imageId = imageId; 19 } 20 21 public String getName() { 22 return Name; 23 } 24 25 public void setName(String name) { 26 Name = name; 27 } 28 29 public String getPrice() { 30 return Price; 31 } 32 33 public void setPrice(String price) { 34 Price = price; 35 } 36 37 public Object getItem(int position){ 38 return position; 39 } 40 41 }
fruitadapte
1 package com.example.shoppinglist; 2 3 import android.content.Context; 4 import android.view.LayoutInflater; 5 import android.view.View; 6 import android.view.ViewGroup; 7 import android.widget.ArrayAdapter; 8 import android.widget.ImageView; 9 import android.widget.TextView; 10 11 import java.util.List; 12 13 public class FruitAdapte extends ArrayAdapter { 14 int resourceId; 15 public FruitAdapte(Context context, int resource, List<Fruit> objects){ 16 super(context, resource,objects); 17 resourceId=resource; 18 } 19 20 @Override 21 public View getView(int position, View convertView, ViewGroup parent){ 22 Fruit fruit = (Fruit)getItem(position); 23 View view = LayoutInflater.from(getContext()).inflate(resourceId,null); 24 ImageView iv = (ImageView)view.findViewById(R.id.imageView); 25 TextView tv = (TextView)view.findViewById(R.id.textName); 26 TextView pv = (TextView)view.findViewById(R.id.textPrice); 27 iv.setImageResource(fruit.getImageId()); 28 tv.setText((CharSequence) fruit.getName()); 29 pv.setText((CharSequence)fruit.getPrice()); 30 31 return view; 32 } 33 34 }
item
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" > 6 7 <ImageView 8 android:id="@+id/imageView" 9 android:layout_width="100dp" 10 android:layout_height="100dp" 11 app:srcCompat="@mipmap/grape" /> 12 13 <LinearLayout 14 android:layout_width="match_parent" 15 android:layout_height="100dp" 16 android:gravity="left|center_vertical" 17 android:orientation="vertical" 18 android:scrollbarSize="8dp"> 19 20 <TextView 21 android:id="@+id/textName" 22 android:layout_width="wrap_content" 23 android:layout_height="wrap_content" 24 android:text="草莓" /> 25 26 <TextView 27 android:id="@+id/textPrice" 28 android:layout_width="wrap_content" 29 android:layout_height="wrap_content" 30 android:text="价格:30元1斤" 31 android:textSize="16sp" /> 32 </LinearLayout> 33 34 </LinearLayout>