1. 程式人生 > 實用技巧 >Android購物商城

Android購物商城

android簡易購物商城

 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 
 8     <TextView
 9
android:layout_width="match_parent" 10 android:layout_height="45dp" 11 android:text="購物商城" 12 android:textSize="18sp" 13 android:textColor="#ffffff" 14 android:background="#ff8f03" 15 android:gravity="center" 16 /> 17 18 <ListView
19 android:layout_width="match_parent" 20 android:layout_height="match_parent" 21 android:id="@+id/lv" 22 /> 23 24 </LinearLayout>

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width
="match_parent" 4 android:layout_height="match_parent" 5 android:padding="16dp" 6 > 7 <ImageView 8 android:layout_width="120dp" 9 android:layout_height="90dp" 10 android:layout_centerVertical="true" 11 android:id="@+id/iv" 12 /> 13 <RelativeLayout 14 android:layout_width="wrap_content" 15 android:layout_height="wrap_content" 16 android:layout_marginLeft="10dp" 17 android:layout_toRightOf="@+id/iv" 18 android:layout_centerVertical="true" 19 > 20 <TextView 21 android:id="@+id/title" 22 android:layout_width="wrap_content" 23 android:layout_height="wrap_content" 24 android:text="桌子" 25 android:textSize="20sp" 26 android:textColor="#000000" 27 /> 28 <TextView 29 android:layout_width="wrap_content" 30 android:layout_height="wrap_content" 31 android:id="@+id/tv_price" 32 android:text="價格:" 33 android:textSize="20sp" 34 android:layout_marginTop="10dp" 35 android:layout_below="@+id/title" 36 android:textColor="#ff8f03" 37 /> 38 <TextView 39 android:layout_width="wrap_content" 40 android:layout_height="wrap_content" 41 android:id="@+id/price" 42 android:text="1000" 43 android:textSize="20sp" 44 android:layout_below="@+id/title" 45 android:layout_toRightOf="@+id/tv_price" 46 android:textColor="#ff8f03" 47 android:layout_marginTop="10dp" 48 /> 49 50 51 </RelativeLayout> 52 53 </RelativeLayout>

 1 package com.example.myapplication;
 2 
 3 import androidx.appcompat.app.AppCompatActivity;
 4 
 5 import android.os.Bundle;
 6 import android.view.View;
 7 import android.view.ViewGroup;
 8 import android.widget.BaseAdapter;
 9 import android.widget.ImageView;
10 import android.widget.ListView;
11 import android.widget.TextView;
12 
13 public class MainActivity extends AppCompatActivity {
14     private String[] titles ={"草莓","荔枝","火龍果","橙子","芒果","櫻桃"};
15     private String[] prices ={"15元/kg","10元/kg","30元/kg","5元/kg","20元/kg","40元/kg"};
16     private  int[] icons ={R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d,R.drawable.e,R.drawable.f};
17 
18     @Override
19     protected void onCreate(Bundle savedInstanceState) {
20         super.onCreate(savedInstanceState);
21         setContentView(R.layout.activity_main);
22         ListView listView=  findViewById(R.id.lv);
23         MyBaseAdapter adapter =new MyBaseAdapter();
24         listView.setAdapter(adapter);
25     }
26 
27     class MyBaseAdapter extends BaseAdapter{
28 
29         @Override
30         public int getCount() {
31             return titles.length;
32         }
33 
34         @Override
35         public Object getItem(int position) {
36             return titles[position];
37         }
38 
39         @Override
40         public long getItemId(int position) {
41             return position;
42         }
43 
44         @Override
45         public View getView(int position, View convertView, ViewGroup parent) {
46             ViewHolder holder;
47             if(convertView==null){
48                 convertView= View.inflate(MainActivity.this,R.layout.list_item,null);
49                 holder =new ViewHolder();
50                 holder.title = convertView.findViewById(R.id.title);
51                 holder.price = convertView.findViewById(R.id.price);
52                 holder.iv= convertView.findViewById(R.id.iv);
53                 convertView.setTag(holder);
54             }else {
55                 holder=(ViewHolder) convertView.getTag();
56             }
57             holder.title.setText(titles[position]);
58             holder.price.setText(prices[position]);
59             holder.iv.setImageResource(icons[position]);
60             return convertView;
61         }
62     }
63     class ViewHolder{
64         TextView title;
65         TextView price;
66         ImageView iv;
67     }
68 }