Android dynamically adds controls and implements click events for each child control

Source: Internet
Author: User

Requirements: We want to click into a shop, according to different stores, showing the sub-items of different numbers such as: disinfection room, washing room, etc... These are more or less, so you need to add them dynamically:

First, customize view (LinearLayout):

Package Cn.qust.fang.widget;import Io.vov.vitamio.mediaplayer;import Io.vov.vitamio.widget.mediacontroller;import Io.vov.vitamio.widget.videoview;import Android.app.activity;import Android.content.contentresolver;import Android.content.contentvalues;import Android.content.context;import Android.net.uri;import Android.util.attributeset;import Android.view.layoutinflater;import Android.view.view;import Android.widget.edittext;import Android.widget.linearlayout;import Android.widget.textview;import Android.widget.toast;import Cn.qust.fang.changliang.changliang;import Cn.qust.fang.db.dbmanager;import Cn.qust.fang.domain.shop;import Cn.qust.fang.main.mvplayactivity123;import Cn.qust.fang.main.r;public Class Righttopchildslayouts extends LinearLayout {shop shop=null;private TextView Text_name_sub,textview_mvurl;    View view;    MvplayActivity123 currentactivity;    /** * The disinfection between the dishes between what the * * String zi_name; /** * Video URL for disinfection between the dishes */String zi_mvurl;public RighttopchildslayOuts (context context, AttributeSet Attrs,int Defstyle) {Super (context, attrs, defstyle);//TODO auto-generated Constructor stub}//Custom Constructor public righttopchildslayouts (MvplayActivity123 context,shop shop,string zi_name,string zi_ Mvurl) {super (context); view = Layoutinflater.from (context). Inflate (r.layout.sub_right_top, null); Text_name_sub= ( TextView) View.findviewbyid (r.id.textview1)/** * Save Mv_url */textview_mvurl= (TextView) View.findviewbyid ( R.id.textview_gone);//load the acquired view into this control addview (view); this.currentactivity=context;this.shop=shop;this.zi_mvurl= Zi_mvurl;this.zi_name=zi_name;text_name_sub.settext (Zi_name); Textview_mvurl.settext (Zi_mvurl); View.setonclicklistener (New Onclicklistener () {//click event @overridepublic void OnClick (View v) {//TODO auto-generated Method Stubif (!getzi_mvurl (). Equals ("collect")) {//If not favorites, switch video System.out.println ("Click =====righttopchildslayouts== zi_name======== "+getzi_name ()); Videoview Mvideoview =currentactivity.getmvideoview (); Mvideoview.pause (); Uri uri = Uri.parse (geTzi_mvurl ()); Mvideoview.setvideouri (URI); Mvideoview.start ();} else {/** * is collected here if collection shows Favorites */system.out.println ("click = Favorites ====righttopchildslayouts==zi_name========" +getzi_name ()) ; if (getshop () = null) Toast.maketext (GetContext (), "collection Failed", Toast.length_short). Show (); New DBManager (GetContext ()). Exits (Getshop (). getshop_id ()))) {save2db (Getshop ());} else {//toast.maketext (context, " Collection ", Toast.length_short). Show (); Text_name_sub.settext (r.string.haven_collected);}});} private void save2db (Shop shop) {Contentresolver resolver = GetContext (). Getcontentresolver (); Contentvalues values = new Contentvalues (),//System.out.println ("Baocun11 ago--------"), Values.put ("Shop_name", Shop.getshopname ()); Values.put ("Address", shop.getaddress ()), Values.put ("Mv_url", Shop.getmv_url ()); Values.put (" Logo_url ", Shop.getlogo_url ()); Values.put (" Msg ", shop.getmsg ()); Values.put (" shop_id ", shop.getshop_id ());             Values.put ("Shop_phone", Shop.getshop_phone ()); Uri url = uri.parse ("Content://fang. ContentprOvider. mycontentprovider_media/"+ changliang.table); Resolver.insert (URL, values);//System.out.println (" Baocun11 successfully-----------"),//toast.maketext (GetContext ()," collection succeeded ", Toast.length_short). Show (); Public Shop getshop () {return shop;} public void Setshop (shop shop) {this.shop = shop;} Public String Getzi_name () {return zi_name;} public void Setzi_name (String zi_name) {this.zi_name = Zi_name;} Public String Getzi_mvurl () {return zi_mvurl;} public void Setzi_mvurl (String zi_mvurl) {this.zi_mvurl = Zi_mvurl;} This might not be appropriate. @overridepublic void Setonclicklistener (Onclicklistener l) {//TODO auto-generated method Stubsuper.setonclicklistener (l);//this.currentactivity.finish (); Videoview Mvideoview =currentactivity.getmvideoview (); Mvideoview.pause (); Uri uri = Uri.parse (zi_name); Mvideoview.setvideouri (URI); Mvideoview.start (); Mvideoview.setonpreparedlistener (new Mediaplayer.onpreparedlistener () {@Overridepublic void onprepared (MediaPlayer MediaPlayer) {//optional need Vitamio 4.0mediaplayer.setplaybackspeeD (1.0f);}});}} 
The sub_right_top.xml used above are as follows:

<pre name= "code" class= "HTML" ><relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android" xmlns:tools= "Http://schemas.android.com/tools" android:id= "@+id/container" android:layout_width= "Match_parent" an droid:layout_height= "Match_parent" tools:context= "com.example.aaaa.MainActivity" tools:ignore= "Mergerootframe" ><linearlayout android:layout_width= "match_parent" android:layout_height= "wrap_content" > <!- -<imageview android:id= "@+id/imageview1" android:layout_width= "Wrap_content" Android:layout_heigh t= "Wrap_content" android:layout_gravity= "center_vertical" android:layout_marginleft= "8DP" android:src = "@drawable/mm_title_btn_compose_normal"/>--<textview android:id= "@+id/textview1" Android:la Yout_width= "Wrap_content" android:layout_height= "wrap_content" android:padding= "8DP" android:text= "wash vegetables Room "android:textcolor=" #fff "       Android:textsize= "18sp"/> <textview android:id= "@+id/textview_gone" Android:layout_wid        Th= "Wrap_content" android:layout_height= "wrap_content" android:padding= "8DP" android:text= "hidden"                    Android:textcolor= "#fff" android:textsize= "18sp" android:visibility= "Gone"/> </LinearLayout> </RelativeLayout>



Here's the dynamically added code: Start adding dynamically when their parent layout is loaded:

public class Maintoprightdialog extends Activity {//private mydialog dialog;private linearlayout layout;@ overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.main_top_right_dialog);//Dialog=new Mydialog (this); layout = (LinearLayout) Findviewbyid (r.id.main_dialog_ Layout); Shop SHOP1 = (shop) getintent (). Getserializableextra ("Shop_inmvplayer"); MvplayActivity123 mvplayActivity123 = Mvplayactivity123.getinstance (); list<map<string, object>> mv_urls_list = null; String mv_urls = Shop1.getmv_url (); if (!mv_urls.equals ("null") && mv_urls! = null &&!mv_urls.equals ("") {try {mv_urls_list = (list<map<string, object>>) New Parsejson2list (). Parsejson2list (Shop1.getmv_url (). toString ());} catch (Jsonexception e) {//TODO auto-generated catch Blocke.printstacktrace ();}} else {mv_urls_list = Null;return;} Righttopchildslayouts layout_childs = null;/** * According to the Zi_mvurl distinguish is the URL or collection if the collection is collect */righTtopchildslayouts layout_collect = null; for (int i = 0; i < mv_urls_list.size (); i++) {layout_childs = new righttopchildslayouts (mvplayActivity123, SHOP1,MV _urls_list.get (i). Get ("_skey"). ToString (), Mv_urls_list.get (i). Get ("_svalue"). ToString ()); if (layout_childs! = NULL) {/* android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:layout_alignpare Ntright= "true" android:layout_alignparenttop= "true" */layout.addview (layout_childs,130, LinearLayout.LayoutParams.WRAP_CONTENT);}} /** * Add a favorite if you have a favorite display */layout_collect=new righttopchildslayouts (mvplayActivity123, SHOP1, (new DBManager (this). Exits ( SHOP1.GETSHOP_ID ())? " Collection ":"), "collect"); Layout.addview (layout_collect,130,linearlayout.layoutparams.wrap_content);/* Layout.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {//TODO auto-generated method Stubtoast.maketext (Getapplicationcontext (), "Hint: Click outside the window to close the window!" ", Toast.length_short). Show ();}}); */} @Overridepublic boOlean Ontouchevent (Motionevent event) {finish (); return true;}} 
This layout file (Main_dialog_layout) is as follows:
<?xml version= "1.0" encoding= "UTF-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/       Android "Android:layout_width=" Fill_parent "android:layout_height=" fill_parent "> <relativelayout                     Android:layout_width= "Fill_parent" android:layout_height= "fill_parent" android:layout_margintop= "46DP" > <linearlayout android:id= "@+id/main_dialog_layout" android:layout_width = "Wrap_content" android:layout_height= "Wrap_content" android:layout_alignparentright= "true" android:l Ayout_alignparenttop= "true" android:orientation= "vertical" android:background= "@drawable/title_function_bg"         > <!--<linearlayout android:layout_width= "match_parent" android:layout_height= "Wrap_content"        android:layout_marginbottom= "3DP" > <textview android:layout_width= "wrap_content" Android:layout_height= "Wrap_conteNT "android:padding=" 8DP "android:text=" collection "android:textcolor=" #fff "android:textsize=" 18SP " /> </LinearLayout> </LinearLayout> </re Lativelayout></relativelayout>





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.