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

Android 布局文件

<?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>

  java类(三个)

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;
}
}

Android作业0930的更多相关文章

  1. Android作业分组与选题

    期末大作业 序号 题目 组员分工 完成度 1 基于安卓系统的游戏开发 2 设计一个安卓手机小游戏 3 Android平台应用——音乐播放器设计 4 基于Android技术的个人博客 5 电子阅读器 6 ...

  2. 简单计算器 安卓 Android 作业

    Android老师布置的课程作业——简单计算器 功能要求实现四则运算,参考界面见下图: 首先给各位老铁提供apk安装包以及项目压缩包,略表诚意~ github地址:https://github.com ...

  3. Android作业list

    作业1. 请在自己的电脑上完成Android的安装与配置,并完成Hello Android项目.上交自己与项目的合照,将照片传至QQ群中. ------------------------------ ...

  4. Android作业

    一.设置跑马灯功能   使用滚动字幕显示标题“请选择你喜欢哪种花” <?xml version="1.0" encoding="utf-8"?>&l ...

  5. Android作业 0923

    计算器小应用 package com.example.myhomework2; import androidx.appcompat.app.AppCompatActivity; import andr ...

  6. Android作业10/07

    1.多个Activity界面实现数据的传递 <?xml version="1.0" encoding="utf-8"?> <androidx. ...

  7. 错误提示”void is an invalid type for the variable“

    今晚做android作业,出现错误提示:void is an invalid type for the variable, invalid:无效的  variable:变量,在网上找了一下后知道是 方 ...

  8. Android——手机内部文件存储(作业)

    作业:把用户的注册信息存储到文件里,登录成功后读出并显示出来 activity_practice2的layout文件: <?xml version="1.0" encodin ...

  9. Android——SharedPreferences存储(作业)

    作业:制作一个登录界面,以SP方式存储用户名.用户下次登录时自动显示上次填写的用户名 layout文件: <?xml version="1.0" encoding=" ...

随机推荐

  1. Linux系统下部署项目流程

    一.系统架构 linux系统 centOS 6.9 应用服务器:Tomcat /JDK 数据库服务器:MySQL 二.连接远程工具FinalShell 1.Centos 6: 启动服务:service ...

  2. Mono生命周期小实验

    今天在写代码的时候,遇到一个初始化顺序问题,于是做了一个实验,下面记录结果: 情景: 1.在 脚本A中实例化 一个预制体,该预制体挂有脚本B 2.在 脚本A中,获取实例化物体 身上的 脚本B,并且设置 ...

  3. C# Beanstalkd Client

    http://bestmike007.com/Beanstalkd.Client/ Other Message Queue http://queues.io

  4. 使用代码给Unity中的动画片段绑定回调函数

    在制作动作游戏的时候,需要播放许多动画,同时还有个需求,那就是动画播放到一定时间时,给一个回调函数,好做对应的状态变更, 我查了一下,发现如果使用的是unity自带的动画系统,要做到这样的话,需要这样 ...

  5. 想在java接口自动化里用上Python的requests?这样做就可以了

    相信现在很多的公司自动化测试重点都在接口层,因为接口测试更加接近代码底层,相对于UI自动化,接口自动化有着开发更快.覆盖更全.回报率高等优点. 接口自动化代码实现不难,本质上就是代码模拟发送请求,然后 ...

  6. 分布式文件存储:FastDFS简单使用与原理分析

    引言 FastDFS 属于分布式存储范畴,分布式文件系统 FastDFS 非常适合中小型项目,在我接手维护公司图片服务的时候开始接触到它,本篇文章目的是总结一下 FastDFS 的知识点. 用了 2 ...

  7. el-select 封装

    这里打算封装一个全局el-select组件 MySelect.vue <template> <el-select v-if="options.length!==0" ...

  8. springboot x.x.x RELEASE pom 第一行报错解决办法

    springboot x.x.x RELEASE pom 第一行报错解决办法 在pom.xml 文件的properties中加入maven jar插件的版本号 <properties> & ...

  9. Nginx 路由--Location的使用

    一. 路由--Location的使用 9.1. Location语法规则 语法规则: location [=|~|~*|^~] /uri/ {… } 首先匹配 =,其次匹配^~,其次是按文件中顺序的正 ...

  10. [程序员代码面试指南]第9章-在两个长度相等的排序数组中找到第k小的数(二分)

    题目 给定两个有序数组arr1和arr2,再给定一个整数k,返回所有的数中第k小的数. 题解 利用题目"在两个长度相等的排序数组中找到第上中位数"的函数 分类讨论 k < 1 ...