Android实现购物商城

来自:网络
时间:2021-04-22
阅读:

activity_main.xml

    <ListView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:listSelector="#B5DCFA">
    </ListView>

1
2
3
4
5
6
7
listview2.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:id="@+id/iv"
        android:layout_width="120dp"
        android:layout_height="90dp"
        android:background="@drawable/table"
        android:layout_marginRight="10dp">
    </ImageView>
    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/iv"
        android:text="桌子"
        android:textSize="20dp"
        android:layout_marginTop="10dp">
    </TextView>
    <TextView
        android:id="@+id/price"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/iv"
        android:layout_below="@id/title"
        android:text="价格:   "
        android:textSize="15dp"
        android:textColor="#FF8F03"
        android:layout_marginTop="15dp">
    </TextView>
    <TextView
        android:id="@+id/count"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/price"
        android:layout_toRightOf="@id/price"
        android:textSize="15dp"
        android:text="1000"
        android:textColor="#FF8F03">
    </TextView>
</RelativeLayout>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
MainActivity.java

public class MainActivity extends AppCompatActivity {
    private ListView listView; //ListView控件
    //商品名称、价格、图片集合
    private String[] titles={"桌子","苹果","蛋糕","线衣","猕猴桃","围巾"};
    private String[] prices={"1800元","10元/kg","300元","350元","10元/kg","280元"};
    private int[] icons={R.drawable.table,R.drawable.apple,R.drawable.cake,R.drawable.wireclothes,R.drawable.kiwifruit,R.drawable.scarf};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listView=findViewById(R.id.lv);       //获取ListView控件
        MallAdapter adapter=new MallAdapter();    //创建一个Adapter实例
        listView.setAdapter(adapter);    //设置adapter,将适配器指定给ListView对象
    }
    //创建一个MallAdapter类继承自BaseAdapter类,并重写类中的一些方法
    public class MallAdapter extends BaseAdapter {

        @Override
        public int getCount() {   //获取item条数
            return titles.length;    //返回ListView Item条目的总数
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
                /*
        position:当前的item的位置
        convertView:指定的单元格布局
        parent:用于加载xml布局
         */
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView==null){
                //通过inflate()方法加载列表条目的布局文件
                convertView=View.inflate(MainActivity.this,R.layout.listview2,null);
            }
            //获取列表条目上的控件
            TextView title=convertView.findViewById(R.id.title);
            TextView price=convertView.findViewById(R.id.count);
            ImageView iv=convertView.findViewById(R.id.iv);
            //设置界面上的文本图片和数据信息
            title.setText(titles[position]);
            price.setText(prices[position]);
            iv.setBackgroundResource(icons[position]);
            return convertView;
        }
    }
}

返回顶部
顶部