《Android移动应用基础教程》之Android购物商城

《Android移动应用基础教程》之Android购物商城,第1张

概述activity_main.xml代码如下:<?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apkes/android"android:layout_width="match_parent"android:layout_height=&quot activity_main.xml代码如下:
<?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:layout_wIDth="match_parent"        androID:layout_height="45dp"        androID:text="购物商城"        androID:textSize="18sp"        androID:textcolor="#FFFFFF"        androID:background="#FF8F03"        androID:gravity="center"/>    <ListVIEw        androID:ID="@+ID/lv"        androID:layout_wIDth="match_parent"        androID:layout_height="wrap_content"/></linearLayout>
List_item.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"    androID:padding="16dp">    <ImageVIEw        androID:ID="@+ID/iv"        androID:layout_wIDth="120dp"        androID:layout_height="90dp"        androID:layout_centerVertical="true" />    <relativeLayout        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:layout_centerVertical="true"        androID:layout_marginleft="10dp"        androID:layout_toRightOf="@+ID/iv">        <TextVIEw            androID:ID="@+ID/Title"            androID:layout_wIDth="wrap_content"            androID:layout_height="wrap_content"            androID:text="桌子"            androID:textcolor="#000000"            androID:textSize="20sp" />        <TextVIEw            androID:ID="@+ID/tv_price"            androID:layout_wIDth="wrap_content"            androID:layout_height="wrap_content"            androID:layout_below="@+ID/Title"            androID:layout_margintop="10dp"            androID:text="价格:"            androID:textcolor="#FF8F03"            androID:textSize="20sp" />        <TextVIEw            androID:ID="@+ID/price"            androID:layout_wIDth="wrap_content"            androID:layout_height="wrap_content"            androID:layout_below="@+ID/Title"            androID:layout_margintop="10dp"            androID:layout_toRightOf="@+ID/tv_price"            androID:text="1000"            androID:textcolor="#FF8F03"            androID:textSize="20sp" />    </relativeLayout></relativeLayout>
MainActivity.java代码如下:
package com.itcast.ListvIEw;import androID.app.Activity;import androID.os.Bundle;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androID.Widget.BaseAdapter;import androID.Widget.ImageVIEw;import androID.Widget.ListVIEw;import androID.Widget.TextVIEw;public class MainActivity extends Activity {    private ListVIEw mListVIEw;    //需要适配的数据    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};    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.activity_main);        //初始化ListVIEw控件        mListVIEw = (ListVIEw) findVIEwByID(R.ID.lv);        //创建一个Adapter的实例        MyBaseAdapter mAdapter = new MyBaseAdapter();        //设置Adapter        mListVIEw.setAdapter(mAdapter);    }    //创建一个类继承BaseAdapter    class MyBaseAdapter extends BaseAdapter {        //得到item的总数        @OverrIDe        public int getCount() {            //返回ListVIEw Item条目的总数            return Titles.length;        }        //得到Item代表的对象        @OverrIDe        public Object getItem(int position) {            //返回ListVIEw Item条目代表的对象            return Titles[position];        }        //得到Item的ID        @OverrIDe        public long getItemID(int position) {            //返回ListVIEw Item的ID            return position;        }        //得到Item的VIEw视图        @OverrIDe        public VIEw getVIEw(int position, VIEw convertVIEw, VIEwGroup parent) {         /*   //将List_item.xml文件找出来并转换成VIEw对象            VIEw vIEw  = VIEw.inflate(MainActivity.this,                    R.layout.List_item, null);            //找到List_item.xml中创建的TextVIEw            TextVIEw Title = (TextVIEw) vIEw.findVIEwByID(R.ID.Title);            TextVIEw price = (TextVIEw) vIEw.findVIEwByID(R.ID.price);            ImageVIEw iv = (ImageVIEw) vIEw.findVIEwByID(R.ID.iv);            Title.setText(Titles[position]);            price.setText(prices[position]);            iv.setBackgroundResource(icons[position]);            return vIEw;*/            VIEwHolder holder = null;            if(convertVIEw == null){                //将List_item.xml文件找出来并转换成VIEw对象                convertVIEw  = VIEw.inflate(MainActivity.this, R.layout.List_item, null);                //找到List_item.xml中创建的TextVIEw                holder = new VIEwHolder();                holder.Title = (TextVIEw) convertVIEw.findVIEwByID(R.ID.Title);                holder.price = (TextVIEw) convertVIEw.findVIEwByID(R.ID.price);                holder.iv = (ImageVIEw) convertVIEw.findVIEwByID(R.ID.iv);                convertVIEw.setTag(holder);            }else{                holder = (VIEwHolder) convertVIEw.getTag();            }            holder.Title.setText(Titles[position]);            holder.price.setText(prices[position]);            holder.iv.setBackgroundResource(icons[position]);            return convertVIEw;        }    }    static class VIEwHolder{        TextVIEw Title;        TextVIEw price;        ImageVIEw iv;    }}

实现效果如下:

总结

以上是内存溢出为你收集整理的《Android移动应用基础教程》之Android购物商城全部内容,希望文章能够帮你解决《Android移动应用基础教程》之Android购物商城所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1062934.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-26
下一篇2022-05-26

发表评论

登录后才能评论

评论列表(0条)

    保存