Use the ListView to populate the item in the Dialog dialog box

Source: Internet
Author: User

The content of the pop-up dialog is a reminder message, or a selection of "Confirm" and "Cancel", if the item is taken from the outside or a lot of things are done, the following is an introduction to using the ListView to populate the item with a click event!

In fact, the writing is very simple, write a adapter class, call can

<span style= "Font-family:microsoft yahei;font-size:14px;" >package com.example.dialoaglistview;import java.util.list;import Java.util.map;import android.content.Context; Import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;import Android.widget.baseadapter;import Android.widget.textview;public class Dialogitemsadapter extends BaseAdapter{ Private Context context;private list<map<string,object>> listitems;private layoutinflater listContainer; Public Dialogitemsadapter (Context context,list<map<string,object>> listItems) {this.context = context; Listcontainer = Layoutinflater.from (context); this.listitems = ListItems;} Public final class Listitemview{public TextView value;} @Overridepublic int GetCount () {return listitems.size ();} @Overridepublic Object getItem (int position) {if (Position < Listitems.size ()) {return listitems.get (position);} return null;} @Overridepublic long Getitemid (int position) {return position;} @OverridepublicView GetView (int position, view Convertview, ViewGroup parent) {Listitemview Listitemview = new Listitemview (); if (convert View = = null) {Convertview = Listcontainer.inflate (r.layout.listview_item,null); listitemview.value = (TextView) Convertview.findviewbyid (R.id.text_item); Convertview.settag (Listitemview);} else {Listitemview = (Listitemview) Convertview.gettag ();} ListItemView.value.setText (String) listitems.get (position). Get ("value")); return Convertview;}} </span>
then write a separate layout file for the ListView

<span style= "Font-family:microsoft yahei;font-size:14px;" ><?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/ Res/android "    android:layout_width=" match_parent "    android:layout_height=" Match_parent "    android: Gravity= "Center_horizontal"    android:orientation= "vertical" >        <textview android:id=        "@+id/text_ Item "        android:layout_width=" wrap_content "        android:layout_height=" wrap_content "        android:text=" style self-tuning "        android:textsize=" 20SP "/></linearlayout></span>
The rest is to call this adapter, the main code is as follows

<span style= "Font-family:microsoft yahei;font-size:14px;" >private void Showmydialog () {final String items[] = {"Lecture hall", "Multimedia Classroom", "room", "art Department Classroom", "ordinary Classroom", "training base", "laboratory", "Sports", "Music Department classroom", "Voice Room"}; list<map<string, object>> data = new arraylist<map<string,object>> (); for (int i = 0;i< items.length;i++) {map<string,object> item = new hashmap<string,object> (); Item.put ("Value", items[i]); Data.add (item);} ListView listview= New ListView (this);//Constructs a ListView object. Dialogitemsadapter adapter = new Dialogitemsadapter (this, data); Listview.setadapter (adapter); final Alertdialog alertdialog = new Alertdialog.builder (this). Create (); Alertdialog.setcanceledontouchoutside (false);//Make it possible to click Alertdialog.settitle except for dialog ("Select classroom Type"); Alertdialog.setview (ListView); Alertdialog.show (); Listview.setonitemclicklistener (new Onitemclicklistener () {//              The Click event in response to item in the ListView @Override public void Onitemclick (adapterview<?> arg0, View arg1, int arg2,        Long Arg3) {  TODO auto-generated Method Stub TextView TV = (TextView) arg1. Findviewbyid (R.id.text_item)          ;//Gets the TextView control Textview.settext (Tv.gettext (). toString ()) in each item;      Alertdialog.cancel ();  }  }); }</span>
the items above can be obtained from an external file or from a network, in short, getting more than one item can


Code download

Use the ListView to populate the item in the Dialog dialog box

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.