• Android第五次作业


    1.使用ListView和Adapter实现购物商城

    <?xml version="1.0" encoding="utf-8"?>
    
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    
        xmlns:app="http://schemas.android.com/apk/res-auto"
    
        xmlns:tools="http://schemas.android.com/tools"
    
        android:layout_width="match_parent"
    
        android:layout_height="match_parent"
    
        tools:context=".MainActivity">
    
    <LinearLayout
    
        android:layout_width="match_parent"
    
        android:layout_height="match_parent">
    
        <ListView
    
            android:id="@+id/lv1"
    
            android:layout_width="wrap_content"
    
            android:layout_height="wrap_content">
    
        </ListView>
    
    </LinearLayout>
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    <?xml version="1.0" encoding="utf-8"?>
    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    
        android:orientation="horizontal" android:layout_width="match_parent"
    
        android:layout_height="match_parent">
    
     
    
        <ImageView
    
            android:id="@+id/iv"
    
            android:layout_width="170dp"
    
            android:layout_height="150dp"
    
            >
    
     
    
        </ImageView>
    
     
    
     
    
        <TextView
    
            android:id="@+id/tv1"
    
            android:layout_width="wrap_content"
    
            android:layout_height="wrap_content"
    
            android:layout_margin="50dp"
    
            android:text="111">
    
     
    
        </TextView>
    
     
    
        <TextView
    
            android:id="@+id/tv2"
    
            android:layout_width="wrap_content"
    
            android:layout_height="wrap_content"
    
            android:layout_marginTop="100dp"
    
            android:layout_marginLeft="-75dp"
    
            android:text="222">
    
     
    
        </TextView>
    
     
    
    </LinearLayout>
    package com.example.myhomework3;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.os.Bundle;
    import android.widget.ListView;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class MainActivity extends AppCompatActivity {
    
        private List<shopping> shoppingList = new ArrayList<shopping>();
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            initShopping();
            shoppingAdapter shoppingAdapter = new shoppingAdapter(MainActivity.this,R.layout.item,shoppingList);
            ListView listView = findViewById(R.id.lv1);
            listView.setAdapter(shoppingAdapter);
        }
    
    
        private void initShopping(){
            shopping book  = new shopping("限定","价格:500元",R.drawable.book);
            shoppingList.add(book);
    
            shopping book1  = new shopping("书1","价格:26元",R.drawable.book1);
            shoppingList.add(book1);
    
            shopping book2  = new shopping("书2","价格:29元",R.drawable.book2);
            shoppingList.add(book2);
    
            shopping book3  = new shopping("书3","价格:36元",R.drawable.book3);
            shoppingList.add(book3);
    
            shopping book4  = new shopping("书4","价格:18元",R.drawable.book4);
            shoppingList.add(book4);
    
            shopping book5  = new shopping("书5","价格:33元",R.drawable.book5);
            shoppingList.add(book5);
    
            shopping book6  = new shopping("书6","价格:52元",R.drawable.book6);
            shoppingList.add(book6);
        }
    }
    package com.example.myhomework3;
    
    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.ImageView;
    import android.widget.TextView;
    
    import java.util.List;
    
    public class shoppingAdapter extends ArrayAdapter {
        private final int resourceId;
    
        public shoppingAdapter(Context context, int textViewResourceId, List<shopping> objects){
            super(context,textViewResourceId,objects);
            resourceId = textViewResourceId;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup viewGroup){
            shopping shopping = (com.example.myhomework3.shopping)getItem(position);
            View view = LayoutInflater.from(getContext()).inflate(resourceId,null);
            ImageView imageView = view.findViewById(R.id.iv);
            TextView textView1 = view.findViewById(R.id.tv1);
            TextView textView2 = view.findViewById(R.id.tv2);
            imageView.setImageResource(shopping.getImageId());
            textView1.setText(shopping.getName());
            textView2.setText(shopping.getPrice());
            return view;
        }
    package com.example.myhomework3;
    
    public class shopping {
        private String name;
        private String price;
        private int imageId;
    
    
        public shopping(String name, String price, int imageId) {
            this.name = name;
            this.price = price;
            this.imageId = imageId;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getPrice() {
            return price;
        }
    
        public void setPrice(String price) {
            this.price = price;
        }
    
        public int getImageId() {
            return imageId;
        }
    
        public void setImageId(int imageId) {
            this.imageId = imageId;
        }
    }
  • 相关阅读:
    13. Docker容器内不能联网的6种解决方案
    12. Docker修改默认存储位置
    11. Docker为容器分配指定物理IP地址
    10.修改sysctl.conf提示没权限
    9. 使用ssh登陆Docker
    8. Docker
    7. Docker
    6. Docker
    Python生成器&迭代器
    Python装饰器
  • 原文地址:https://www.cnblogs.com/student-yyh/p/14168078.html
Copyright © 2020-2023  润新知