在实战二中我们在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开发实战(三)的更多相关文章

  1. spring-cloud-square开发实战(三种类型全覆盖)

    欢迎访问我的GitHub 这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos 本篇概览 前文<五分钟搞懂spring-clou ...

  2. Python自动化运维开发实战 三、python文件类型

    导语: python常用的有3种文件类型 1. 源代码 py 2. 字节代码 pyc 3. 优化代码 pyo 源代码: python源代码的文件以”py"为扩展名,由python程序解释,不 ...

  3. eatwhatApp开发实战(二)

    上期,我们做了个小app“eatwhat”,接下来每期都会为其添加新的功能.本期,我们为店铺增加添加店铺的功能. 还是先设置个布局: <RelativeLayout android:layout ...

  4. eatwhatApp开发实战(一)

    开发背景: 当你想用抛硬币来决定事情的时候,那么硬币抛起的瞬间,你就有答案了.一样的,吃啥?eatwhat点开,按钮一点,你就可以知道你中午要吃啥. 话不多说,项目开发走起 ADT点开,New==&g ...

  5. eatwhatApp开发实战(十四)

    之前我们就输入框EditText做了优化,而这次,我们为app添加拨打电话的功能. 首先是布局,将activity_shop_info.xml中对应的电话那一栏进行重新设计: <Relative ...

  6. eatwhatApp开发实战(十三)

    这次内容,我们就项目中添加商店名称的EditText进行修改,让添加按钮随着edittext的内容而改变. 上代码,首先是xml文件上对两个控件的修改: <RelativeLayout andr ...

  7. eatwhatApp开发实战(十二)

    上次我们介绍了跳转activity并且实现传值的功能,今天我们来实现双击返回键退出app的功能,上代码: 这里我们有两种方式去实现点击事件: 第一种方式: /** * 返回键的监听(系统提供的) */ ...

  8. eatwhatApp开发实战(十一)

    之前我们实现了点击item项跳转activity,接下来我们再其基础上添加参数的传递. 在MainActivity里面的onItemClick()中: String name = shopList.g ...

  9. eatwhatApp开发实战(十)

    android应用中,很少有一个activity的app,这次我们设置一个activity,通过listview的点击跳转并显示对应的商店信息. 首先创建类ShopInfoActivity,对应设置其 ...

随机推荐

  1. POJ3614防晒霜 这个贪心有点东西(贪心+优先队列)

    这个题是说有C头牛去晒太阳,带了L瓶防晒霜,每瓶防晒霜都有一个SPF值(每瓶防晒霜都能解决一个最短路 ) 每头牛给出了他可以接受防晒霜的上限,和下限,每种防晒霜都给出了SPF值与数量. 从防晒霜的sp ...

  2. Android原生多语言切换方案,兼容Android10

    前言 一个应用若需要国际化,至少需要支持中文和英语这两种语言,而同时随着谷歌的系统的更新,安卓系统可以设置当前语言的首选语言.因此,本文立足于此,多语言的切换方案为:App固定的文字内容,跟随系统,中 ...

  3. 《Docker从入门到跑路》之Dockerfile基本操作

    一.简介 Dockerfile是一个文本文件,里面包含一条条指令,每一条指令就是一层镜像.一般情况下,Dockerfile分为4个部分: 基础镜像 维护者信息 镜像操作指令 容器启动时执行命令 例如: ...

  4. spring cloud系列教程第一篇-介绍

    spring cloud系列教程第一篇-介绍 前言: 现在Java招聘中最常见的是会微服务开发,微服务已经在国内火了几年了,而且也成了趋势了.那么,微服务只是指spring boot吗?当然不是了,微 ...

  5. OpenCV Error: Unspecified Error(The Function is not implemented)

    Ubuntu 或者 Debian 系统显示窗口的时候遇到了这个问题 error: (-2:Unspecified error) The function is not implemented. Reb ...

  6. java web 开发之 office(excel、doc等)文件转pdf

    一.开发工具:office 16.jacob-1.18-M2.jboss 1.6 二.开发配置: 1.解压缩---> 2.配置jacob: A C:\Windows\System32 jacob ...

  7. js 控制窗口跳转

    使用iframe 或者使用子窗口时,session 失效时是浏览器地址转换 相关js代码:if(top!=self){ if(top.location != self.location) top.lo ...

  8. 【漫画】CAS原理分析!无锁原子类也能解决并发问题!

    本文来源于微信公众号[胖滚猪学编程].转载请注明出处 在漫画并发编程系统博文中,我们讲了N篇关于锁的知识,确实,锁是解决并发问题的万能钥匙,可是并发问题只有锁能解决吗?今天要出场一个大BOSS:CAS ...

  9. 「雕爷学编程」Arduino动手做(17)---人体感应模块

    37款传感器和模块的提法,在网络上广泛流传,其实Arduino能够兼容的传感器模块肯定是不止37种的.鉴于本人手头积累了一些传感器与模块,依照实践出真知(动手试试)的理念,以学习和交流为目的,这里准备 ...

  10. webpack-dev-server简记

    webpack -v 3.4.1 npm -v 3.10.10 /////////////////////////////////////// webpack的webpack-dev-server可以 ...