eatwhatApp开发实战(三)
2024-09-01 02:05:55
在实战二中我们在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());
这样就完成了这次的显示店名。
最新文章
- (转)html5开发之viewport使用
- 使用Ready2Search来定制Firefox和Chrome的搜索框
- HDU 1222(数论,最大公约数)
- Oracle SQL操作计划基线总结(SQL Plan Baseline)
- java基本数据类型的包装类
- 在github上搭建一个静态的个人网站
- Python中的闭包 - Closure
- django模板语言之Template
- Cracking The Coding Interview 5.5
- java-线程(runoob.com)
- Alpha版本测试
- 导入appiumlibrary显红
- C++并发编程实战---阅读笔记
- BZOJ1297: [SCOI2009]迷路 矩阵快速幂
- AndroidStudio 导包遇到so文件的解决方案----------JPush推送
- PLSQL连接本机oracle 11g 64 数据库的步骤
- 2015/11/2用Python写游戏,pygame入门(2):游戏中的事件和显示
- 791. Custom Sort String
- 模拟form表单请求上传文件
- 关于搭建HTTPS服务器服务