• Android第六周作业


    Android购物商城

    1.MainActivity.java

    package com.example.myapplication;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.annotation.SuppressLint;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.net.Uri;
    import android.os.Bundle;
    import android.widget.ImageView;
    import android.widget.ListView;
    import android.widget.TextView;
    
    public class MainActivity extends AppCompatActivity {
        private ListView mListView;
        private String[] titles={ "桌子","苹果","蛋糕","线衣","猕猴桃","围巾"};
        private String[] prices={ "1800元","10元/kg","300元","350元","10元/kg","280元"};
        private int[] icons={ R.mipmap.table,R.mipmap.apple,R.mipmap.cake,R.mipmap.wireclothes,R.mipmap.kiwifruit,R.mipmap.scarf};
        @SuppressLint("WrongViewCast")
        protected void onCreate(Bundle savedInstanceState){
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            mListView = (ListView) findViewById(R.id.Iv);
            MyBaseAdapter mAdapter=new MyBaseAdapter();
            mListView.setAdapter(mAdapter);
        }
        class MyBaseAdapter extends BaseAdapter{
            public int getCount(){
                return titles.length;
            }
    
            @Override
            public Object getItem(int position) {
                return titles[position];
            }
            public long getItemId(int position){
                return position;
    
            }
            public View getView(int position, View convertView, ViewGroup parent){
                View view=View.inflate(MainActivity.this,R.layout.layout2,null);
                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;
    
            }
        }
    
    }
    

      2.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"
            ></TextView>
        <ListView
            android:id="@+id/Iv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
        </ListView>
    
    </LinearLayout>
    

      3.layout2.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" ></ImageView>
            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_toRightOf="@+id/iv"
                android:layout_centerVertical="true">
                <TextView
                android:id="@+id/title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="桌子"
                android:textSize="20sp"
                android:textColor="#000000"></TextView>
                <TextView
                android:id="@+id/tv_price"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="价格:"
                android:textSize="20sp"
                android:layout_marginTop="10dp"
                android:layout_below="@+id/title"
                android:textColor="#FFEF03"></TextView>
                <TextView
                android:id="@+id/price"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="1000"
                android:textSize="20sp"
                android:layout_below="@+id/title"
                android:layout_toRightOf="@+id/tv_price"
                android:textColor="#FF8F03"
                android:layout_marginTop="10dp"></TextView>
            </RelativeLayout>
    </RelativeLayout>
    

      

  • 相关阅读:
    leetcode 300. 最长上升子序列
    JAVA基础系列:Arrays.binarySearch二分查找
    leetcode 674. 最长连续递增序列
    小红书:笔试题(棋盘最短路径,笔记本草稿栈,迷宫游戏)
    VIPKID:笔试题(数组中和为0的一对数的数量,十进制转二进制中1的个数)
    [******] 树问题:普通二叉树的创建与遍历
    [******] 链表问题:将单向链表按某值划分成左边小、中间相等、右边大的形式
    [******] java多线程连续打印abc
    快手:笔试题(版本号比较,平方和为1,合并两个流)
    京东:笔试题(合唱队找剩余的最小值,考场安排搬出的人数尽可能少)
  • 原文地址:https://www.cnblogs.com/a000/p/13796907.html
Copyright © 2020-2023  润新知