Android dynamically adds controls and implements click events for each sub-control

Source: Internet
Author: User

Requirement: Click to enter a shop. Different sub-entries are displayed for different stores, such as disinfection room and washing room... These are more or less, so you need to dynamically Add:

First, customize the 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; impo Rt 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_su B, textview_mvurl; View view; MvplayActivity123 currentactivity;/*** the disinfection room of the food center */String zi_name; /** \ * video url of the disinfection room of the food center */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) {s Uper (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 obtained view to 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.setTex T (zi_mvurl); view. setOnClickListener (new OnClickListener () {// Click Event @ Overridepublic void onClick (View v) {// TODO Auto-generated method stubif (! GetZi_mvurl (). equals ("collect") {// if it is not a favorite, switch to the 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 {/*** add to favorites here. If you have added a favorite to your favorites, add it to favorites */System. out. println ("Click = favorites ==== RightTopChildsLayouts = zi_name ======" + getZi_name (); I F (getShop () = null) Toast. makeText (getContext (), "failed to add to Favorites", Toast. LENGTH_SHORT). show (); if (! (New DBManager (getContext ()). exits (getShop (). getShop_id () {save2db (getShop ();} else {// Toast. makeText (context, "added to Favorites", 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 before --------"); 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 successful -----------"); // Toast. makeText (getContext (), "successfully added to Favorites", 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 may be inappropriate @ 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 is as follows:

  
       
        
             
                        
       
  



The following code is dynamically added: when their parent layout is loaded, the code is dynamically added:

Public class MainTopRightDialog extends Activity {// private MyDialog dialog; private LinearLayout; @ 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
  
   
> 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
   
    
>) 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; /*** use zi_mvurl to identify the url or favorites as collect */RightTopChildsLayouts layout_collect = null; // for (int I = 0; I <mv_urls_list.size (); I ++) {layout_childs = new RightTopC HildsLayouts (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_alignParentRight = "true" android: layout_alignParentTop = "true" */layout. addView (layout_childs, 130, LinearLayout. layoutParams. WRAP_CONTENT) ;}}/*** Add a favorite item if it has been added to favorites */layout_collect = new RightTopChildsLayouts (mvplayActivity123, shop1, (new DBManager (this ). exits (shop1.getShop _ id ())? "Added to Favorites": "added to Favorites"), "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 (), "prompt: Click the window to close the window! ", Toast. LENGTH_SHORT). show () ;}}); * //} @ Overridepublic boolean onTouchEvent (MotionEvent event) {finish (); return true ;}}
   
  
The layout file main_dialog_layout is as follows:
  
        
               
       
                                 
        
   
  





Related Article

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.