eatwhatApp开发实战(三)
在实战二中我们在eatwhatApp上增加了“添加店铺的功能”。接下来,我们来将添加的店铺显示出来,这里我们用到控件--ListView。
先上演示图:

首先,我们先设置布局:
<RelativeLayout
android:id="@+id/et_relative"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/add_shop_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="@string/add_shop_btn_text"
android:onClick="addShop"/>
<EditText
android:id="@+id/addshop_et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/add_shop_btn"
android:textSize="18sp"/>
</RelativeLayout>
<TextView
android:id="@+id/shop_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/shop_name"
android:layout_below="@id/et_relative"
android:paddingTop="20dp"
android:textSize="18sp" />
<Button
android:id="@+id/random_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/shop_name"
android:text="@string/random_btn_text"/>
<ListView
android:id="@+id/lv_shop"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/random_btn"
/>
设置listview里面每个item项的布局,布局文件:item_shop.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:orientation="vertical" > <TextView
android:id="@+id/item_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:layout_centerInParent="true"
android:text="店名"/>
</RelativeLayout>
接下来在java代码中,首先添加一个Shop类,里面包含属性:String name;
public class Shop {
private String name;
public Shop(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
在MainActivity中定义一个内部类ShopInfoAdapter继承BaseAdapter:
//内部类 适配器继承BaseAdapter
class ShopInfoAdapter extends BaseAdapter{ @Override
public int getCount() { return shopList.size();
} @SuppressLint("ViewHolder")
@Override
public View getView(int position, View converView, ViewGroup parent) { View v = View.inflate(MainActivity.this, R.layout.item_shop, null); TextView tv_name = (TextView)v.findViewById(R.id.item_text); tv_name.setText(shopList.get(position).getName()); return v;
}
@Override
public Object getItem(int arg0) { return null;
} @Override
public long getItemId(int arg0) { return 0;
}
}
在开头定义:
//Shop类型
private List<Shop> shopList;
//listview控件
private ListView shop_lv;
//适配器
private ShopInfoAdapter shopAdapter;
inti()中初始化lsitview和adapter并设置adapter:
//初始化控件listview
shop_lv = (ListView) findViewById(R.id.lv_shop); //初始化适配器
shopAdapter = new ShopInfoAdapter(); //设置适配器
shop_lv.setAdapter(shopAdapter);
修改addShop():
if (addName != null && !"".equals(addName)){
//List shop添加店名
Shop shop = new Shop(addName);
//添加新实例化的shop对象
shopList.add(shop);
//刷新listview
shopAdapter.notifyDataSetChanged();
//清空文本框内容
addshop_et.setText("");
String toast_text = "添加店铺:" + addName;
Toast.makeText(MainActivity.this, toast_text, Toast.LENGTH_SHORT).show();
} else{
Toast.makeText(MainActivity.this, "添加内容为空", Toast.LENGTH_SHORT).show();
}
在random_btn的点击事件中修改显示店名的逻辑:
shop_name.setText(shopList.get(num).getName());
这样就完成了这次的显示店名。
eatwhatApp开发实战(三)的更多相关文章
- spring-cloud-square开发实战(三种类型全覆盖)
欢迎访问我的GitHub 这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos 本篇概览 前文<五分钟搞懂spring-clou ...
- Python自动化运维开发实战 三、python文件类型
导语: python常用的有3种文件类型 1. 源代码 py 2. 字节代码 pyc 3. 优化代码 pyo 源代码: python源代码的文件以”py"为扩展名,由python程序解释,不 ...
- eatwhatApp开发实战(二)
上期,我们做了个小app“eatwhat”,接下来每期都会为其添加新的功能.本期,我们为店铺增加添加店铺的功能. 还是先设置个布局: <RelativeLayout android:layout ...
- eatwhatApp开发实战(一)
开发背景: 当你想用抛硬币来决定事情的时候,那么硬币抛起的瞬间,你就有答案了.一样的,吃啥?eatwhat点开,按钮一点,你就可以知道你中午要吃啥. 话不多说,项目开发走起 ADT点开,New==&g ...
- eatwhatApp开发实战(十四)
之前我们就输入框EditText做了优化,而这次,我们为app添加拨打电话的功能. 首先是布局,将activity_shop_info.xml中对应的电话那一栏进行重新设计: <Relative ...
- eatwhatApp开发实战(十三)
这次内容,我们就项目中添加商店名称的EditText进行修改,让添加按钮随着edittext的内容而改变. 上代码,首先是xml文件上对两个控件的修改: <RelativeLayout andr ...
- eatwhatApp开发实战(十二)
上次我们介绍了跳转activity并且实现传值的功能,今天我们来实现双击返回键退出app的功能,上代码: 这里我们有两种方式去实现点击事件: 第一种方式: /** * 返回键的监听(系统提供的) */ ...
- eatwhatApp开发实战(十一)
之前我们实现了点击item项跳转activity,接下来我们再其基础上添加参数的传递. 在MainActivity里面的onItemClick()中: String name = shopList.g ...
- eatwhatApp开发实战(十)
android应用中,很少有一个activity的app,这次我们设置一个activity,通过listview的点击跳转并显示对应的商店信息. 首先创建类ShopInfoActivity,对应设置其 ...
随机推荐
- 疯子的算法总结(七) 字符串算法之 manacher 算法 O(N)解决回文串
有点像DP的思想,写写就会做. #include<bits/stdc++.h> using namespace std; const int maxn=1e7+5; char a[maxn ...
- 编写简单的内核模块及内核源码下载,内核模块Makefile编写
CentOS的内核源码默认是没有下载的,需要自己下载,首先安装linux的时候就应该知道linux的版本,我装的是Centos7的 下面查一下内核的版本,使用下面的命令 [scut_lcw@local ...
- Linux下使用Rsync进行文件同步
数据备份方案 1.需要备份的文件目录有(原则上,只要运维人员写入或更改的数据都需要备份)./data,/etc/rc.local,/var/spool/cron/root等,根据不同都服务器做不同的调 ...
- Programming Languages_05 FWAE
FWAE : Concrete syntax <FWAE> ::= <num> | {+ <FWAE> <FWAE>} | {- <FWAE> ...
- C. Anton and Fairy Tale(数学推式子)
\(数学题,式子并不难推,但边界是真的烦\) \(\color{Red}{Ⅰ.其实可以发现,当m>=n时,每次都可以粮食补到n,所以一定是在第n天消耗完毕}\) \(\color{Purple} ...
- Coursera课程笔记----Write Professional Emails in English----Week 3
Introduction and Announcement Emails (Week 3) Overview of Introduction & Announcement Emails Bas ...
- Day_13【IO流】扩展案例2_统计键盘录入字符在指定项目文件中出现的次数
分析以下需求,并用代码实现 键盘录入一个字符(以字符串的形式录入) 判断当前字符在info3.txt当中是否存在 如果不存在, 给出提示 如果存在, 请统计出该字符出现的次数 Info3.txt内容如 ...
- Day_12【集合】扩展案例1_利用集合的知识对长度为10的int数组进行去重,产生新数组,不能改变数组中原来数字的大小顺序
分析以下需求,并用代码实现 1.定义一个长度为10的int数组,并存入10个int类型的数据,其中有一些数据是重复的 2.利用集合的知识对数组进行去重,产生新数组,不能改变数组中原来数字的大小顺序 3 ...
- spring-boot如何去获取前端传递的参数
本文主要讨论spring-boot如何获取前端传过来的参数,这些参数主要有两大类,一类是URL里的参数,一个是请求body里的参数 url里的参数 通过url里传过来的参数一般有三种方式,下面我们来看 ...
- [hihoCoder1231 2015BeijingOnline]求圆与多边形公共部分的周长
题意:如题 思路:离散.将所有交点求出来,相当于将多变形的边切成了很多条元边,对每条元边,有两种情况 在圆内,答案加上此边长 在圆外,答案加上此边相对于圆心的"有向转弧" #inc ...