Android adds a ListView to dialog and implements the Click event for item in the ListView

Source: Internet
Author: User

LinearLayout Linearlayoutmain = new LinearLayout (this);//Customize a layout file
Linearlayoutmain.setlayoutparams (New Layoutparams (
Layoutparams.match_parent, layoutparams.wrap_content));
ListView ListView = New ListView (this);//this to get the current context
Listview.setfadingedgelength (0);

list<map<string, string>> namelist = new arraylist<map<string, string>> ();// Create an array to store the data displayed on the ListView
for (int m = 0; m < initdata.size (); m++) {//initdata is a data source of list type
map<string, string> namemap = new hashmap<string, string> ();
Namemap.put ("Name", Initdata.get (M). Get (Tagitem.name). toString ());
Namelist.add (NAMEMAP);
}

Simpleadapter adapter = new Simpleadapter (Uploadphotoactivity.this,
NameList, R.layout.item_uploadphoto_select_name,
New string[] {"Name"},
New int[] {r.id.tv_uploadphoto_select_name_item});
Listview.setadapter (adapter);

Linearlayoutmain.addview (listview);//Add a ListView to this layout

Final Alertdialog Dialog = new Alertdialog.builder (this)
. Settitle ("Select Patient Name"). Setview (Linearlayoutmain)//The layout of this ListView that is written here is loaded dialog
. Setnegativebutton ("Cancel", new Dialoginterface.onclicklistener () {

@Override
public void OnClick (Dialoginterface dialog, int which) {
TODO auto-generated Method Stub
Dialog.cancel ();
}
}). Create ();
Dialog.setcanceledontouchoutside (false);//Make it impossible to click anywhere but dialog
Dialog.show ();
Listview.setonitemclicklistener (New Onitemclicklistener () {//Response click event for item in ListView

@Override
public void Onitemclick (adapterview<?> arg0, View arg1, int arg2,
long arg3) {
//TODO auto-g enerated method Stub
TextView TV = (TextView) arg1
. Findviewbyid (R.id.tv_uploadphoto_select_name_item);// Gets the TextView control in each item
Et_name.settext (Tv.gettext (). toString ());
Dialog.cancel ();
}
});

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.