eatwhatApp開發實戰(三)

來源:互聯網
上載者:User

標籤:

在實戰二中我們在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:
//初始化控制項listviewshop_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開發實戰(三)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.