Android作业0930
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的更多相关文章
- Android作业分组与选题
期末大作业 序号 题目 组员分工 完成度 1 基于安卓系统的游戏开发 2 设计一个安卓手机小游戏 3 Android平台应用——音乐播放器设计 4 基于Android技术的个人博客 5 电子阅读器 6 ...
- 简单计算器 安卓 Android 作业
Android老师布置的课程作业——简单计算器 功能要求实现四则运算,参考界面见下图: 首先给各位老铁提供apk安装包以及项目压缩包,略表诚意~ github地址:https://github.com ...
- Android作业list
作业1. 请在自己的电脑上完成Android的安装与配置,并完成Hello Android项目.上交自己与项目的合照,将照片传至QQ群中. ------------------------------ ...
- Android作业
一.设置跑马灯功能 使用滚动字幕显示标题“请选择你喜欢哪种花” <?xml version="1.0" encoding="utf-8"?>&l ...
- Android作业 0923
计算器小应用 package com.example.myhomework2; import androidx.appcompat.app.AppCompatActivity; import andr ...
- Android作业10/07
1.多个Activity界面实现数据的传递 <?xml version="1.0" encoding="utf-8"?> <androidx. ...
- 错误提示”void is an invalid type for the variable“
今晚做android作业,出现错误提示:void is an invalid type for the variable, invalid:无效的 variable:变量,在网上找了一下后知道是 方 ...
- Android——手机内部文件存储(作业)
作业:把用户的注册信息存储到文件里,登录成功后读出并显示出来 activity_practice2的layout文件: <?xml version="1.0" encodin ...
- Android——SharedPreferences存储(作业)
作业:制作一个登录界面,以SP方式存储用户名.用户下次登录时自动显示上次填写的用户名 layout文件: <?xml version="1.0" encoding=" ...
随机推荐
- AQI分析
A Q I 分 析 1.背景信息 AOI( Air Quality Index),指空气质量指数,用来衡量空气清洁或污染的程度.值越小,表示空气质量越好.近年来,因为环境问题,空气质量也越来越受到人 ...
- win10找不到wifi
禁用->启用 就能用了.
- LinuxIP配置方法
一.双网卡双IP. eth0为电信,eth1为联通. # cd /etc/sysconfig/network-scripts/ # vi ifcfg-eth0 DEVICE=eth0 HWADDR=0 ...
- Unity WebGL
路过弄了个unity Unity导出WebGL不支持c#socket和unity的network 可以用javascript的websocket实现... c#一般通过www从phpserver获取. ...
- HDU多校1003-Divide the Stones(构造)
Problem Description There are n stones numbered from 1 to n.The weight of the i-th stone is i kilogr ...
- 面试【JAVA基础】多线程
本次整理的内容如下: 1.进程与线程的区别 进程是一个可执行的程序,是系统资源分配的基本单位:线程是进程内相对独立的可执行单元,是操作系统进行任务调度的基本单位. 2.进程间的通信方式 2.1.操作系 ...
- 11 vue 自定义全局方法
//global.js// 定义vue 全局方 // 定义vue 全局方法 建议自定义的全局方法加_ 以示区分 export default { install(Vue, options = ...
- webpack3.10.0(入门系列基本概念1)
一.概念 webpack的核心是一个用于现代JavaScript应用程序的静态模块打包程序.当webpack处理您的应用程序时,它会递归地构建一个依赖图,其中包含应用程序所需的每个模块,然后将所有这些 ...
- 小程序开发-页面导航栏navigation-bar组件
导航栏navigation-bar 页面导航条配置节点,用于指定导航栏的一些属性.只能是 page-meta 组件内的第一个节点,需要配合它一同使用. 通过这个节点可以获得类似于调用 wx.setNa ...
- WEBAPI 增加身份验证
1,在Webapi项目下添加如下引用: Microsoft.AspNet.WebApi.Owin Owin Microsoft.Owin.Host.SystemWeb Microsoft.Owin.S ...