在实战二中我们在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());

这样就完成了这次的显示店名。

最新文章

  1. (转)html5开发之viewport使用
  2. 使用Ready2Search来定制Firefox和Chrome的搜索框
  3. HDU 1222(数论,最大公约数)
  4. Oracle SQL操作计划基线总结(SQL Plan Baseline)
  5. java基本数据类型的包装类
  6. 在github上搭建一个静态的个人网站
  7. Python中的闭包 - Closure
  8. django模板语言之Template
  9. Cracking The Coding Interview 5.5
  10. java-线程(runoob.com)
  11. Alpha版本测试
  12. 导入appiumlibrary显红
  13. C++并发编程实战---阅读笔记
  14. BZOJ1297: [SCOI2009]迷路 矩阵快速幂
  15. AndroidStudio 导包遇到so文件的解决方案----------JPush推送
  16. PLSQL连接本机oracle 11g 64 数据库的步骤
  17. 2015/11/2用Python写游戏,pygame入门(2):游戏中的事件和显示
  18. 791. Custom Sort String
  19. 模拟form表单请求上传文件
  20. 关于搭建HTTPS服务器服务

热门文章

  1. 图论--差分约束--POJ 3159 Candies
  2. 一只简单的网络爬虫(基于linux C/C++)————开篇
  3. 面试被问为什么使用Spring Boot?答案好像没那么简单
  4. 最长递增子序列(Longest increasing subsequence)
  5. 【面试题】String类、包装类的不可变性
  6. 数据结构--队列(Java实现)
  7. 【HBase】通过Java代码实现HBase数据库中数据的增删改查
  8. [zoj3629]找规律
  9. 浅谈mybatis如何半自动化解耦和ORM实现
  10. layui 关闭弹出层方法