Eatwhatapp Development Combat (III)

Source: Internet
Author: User

In combat two we added "Add Store function" on Eatwhatapp. Next, let's show the added store, where we use the control--listview. First on the demo chart:

First, let's set up the layout first:
<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"/>

Set the layout of each item item in the ListView, layout file: 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 = "Shop name"/></relativelayout>

Next in the Java code, first add a shop class, which contains the properties: 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;    }    } 

Define an inner class shopinfoadapter inherit Baseadapter in mainactivity:
//internal class adapter inheritance Baseadapter    classShopinfoadapterextendsbaseadapter{@Override Public intGetCount () {returnshoplist.size (); } @SuppressLint ("Viewholder") @Override PublicView GetView (intposition, 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 ()); returnv; } @Override PublicObject GetItem (intarg0) {            return NULL; } @Override Public LongGetitemid (intarg0) {            return0; }        }

At the beginning define:
    // Shop Type    Private List<shop> shoplist;     // ListView Control    Private ListView shop_lv;     // Adapter    Private Shopinfoadapter Shopadapter;

Initialize Lsitview and adapter in Inti () and set adapter:
// Initialize the control ListViewshop_lv = (listview) Findviewbyid (R.id.lv_shop);         // Initializing the adapter New Shopinfoadapter ();         // set Adapter shop_lv.setadapter (shopadapter);

Modify Addshop ():
if(AddName! =NULL&& "". Equals (AddName)) {            //List Shop Add store nameShop shop =NewShop (AddName); //add a newly instantiated shop objectShoplist.add (Shop); //Refresh ListViewshopadapter.notifydatasetchanged (); //Empty text box contentsAddshop_et.settext (""); String Toast_text= "Add Shop:" +AddName; Toast.maketext (mainactivity. This, Toast_text, Toast.length_short). Show (); } Else{toast.maketext (mainactivity). This, "Add content is empty", Toast.length_short). Show (); }

Modify the logic of the display store name in the RANDOM_BTN click event:
Shop_name.settext (Shoplist.get (num). GetName ());

This completes the display of the store name.

Eatwhatapp Development Combat (III)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.