A summary of the combination of ListView and SimpleAdapter

Source: Internet
Author: User

Package com. example. l0909_4_listview_all; import java. util. arrayList; import java. util. hashMap; import java. util. list; import java. util. map; import android. app. activity; import android. app. alertDialog; import android. content. dialogInterface; import android. OS. bundle; import android. view. contextMenu; import android. view. contextMenu. contextMenuInfo; import android. view. menuItem; import android. view. view; import Ndroid. view. view. onCreateContextMenuListener; import android. widget. adapterView; import android. widget. adapterView. adapterContextMenuInfo; import android. widget. adapterView. onItemClickListener; import android. widget. adapterView. onItemLongClickListener; import android. widget. listView; import android. widget. simpleAdapter; import android. widget. toast; public class MainActivity extends Activity {private List View lv; private SimpleAdapter adapter; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); lv = (ListView) findViewById (R. id. lv); // use the adapter to configure the data to the control. adapter = new SimpleAdapter (MainActivity. this, getData (), R. layout. cell, new String [] {"Title", "Message"}, new int [] {R. id. TV _title, R. id. TV _mess}); lv. setAdap Ter (adapter); // Click Event lv. setOnItemClickListener (new OnItemClickListener () {@ Override public void onItemClick (AdapterView <?> Arg0, View arg1, int arg2, long arg3) {setTitle ("click" + (arg2 + 1) + "items"); Toast. makeText (MainActivity. this, "click" + (arg2 + 1) + "project", Toast. LENGTH_LONG ). show () ;}}); // The long-Click Event of AdapterView. We comment out the event because it has a higher priority than the long-click event with a sub-menu. Lv. setOnItemLongClickListener (new OnItemLongClickListener () {@ Override public boolean onItemLongClick (AdapterView <?> Arg0, View arg1, int arg2, long arg3) {setTitle ("click" + arg2 + "project"); Toast. makeText (MainActivity. this, "Long-click" + arg2 + "project", Toast. LENGTH_LONG ). show (); return true ;}}); // The long-Click Event lv with sub-menu options in AdapterView. setOnCreateContextMenuListener (new OnCreateContextMenuListener () {@ Override public void onCreateContextMenu (ContextMenu menu, View v, ContextMenuInfo menuInfo) {menu. setHeaderIcon (R. drawabl E. p5); // The image style menu. setHeaderTitle ("Long-pressed menu options:"); menu. add (0, 1, 0, "delete"); menu. add (0, 2, 0, "View ");}});} /*** method automatically called when the Context Menu Sub-option is selected */@ Override public boolean onContextItemSelected (MenuItem item) {// obtain the position int selectedPosition = (AdapterContextMenuInfo) item of the ListView corresponding to the menu item that appears. getMenuInfo ()). position; // obtain the final Map <String, String> map = (HashMap) lv of the position in the ListView. getItemAtPosi Tion (selectedPosition); switch (item. getItemId () {case 1: new AlertDialog. builder (this ). setTitle (map. get ("Title ")). setMessage (map. get ("are you sure you want to delete the option? ")). SetPositiveButton ("OK", new DialogInterface. onClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {map. remove ("Title"); map. remove ("Message"); // after deletion, refresh the data source to readapt to the data adapter. notifyDataSetChanged ();}}). setNegativeButton ("cancel", new DialogInterface. onClickListener () {@ Override public void onClick (DialogInterface dialog, int which ){}}). show (); break; case 2: new AlertDialog. builder (this ). setTitle (map. get ("Title ")). setMessage (map. get ("Message ")). setPositiveButton ("OK", new DialogInterface. onClickListener () {@ Override public void onClick (DialogInterface dialog, int which ){}}). show (); break;} return super. onContextItemSelected (item);}/*** Add a data source ** @ return */private List <Map <String, String> getData () {List <Map <String, string> list = new ArrayList <Map <String, String> (); Map <String, String> map1 = new HashMap <String, String> (); Map <String, string> map2 = new HashMap <String, String> (); Map <String, String> map3 = new HashMap <String, String> (); Map <String, string> map4 = new HashMap <String, String> (); Map <String, String> map5 = new HashMap <String, String> (); Map <String, string> map6 = new HashMap <String, String> (); map1.put ("Title", ""); map1.put ("Message", "18823450968 "); map2.put ("Title", "Zhang Fei"); map2.put ("Message", "13278904678"); map3.put ("Title", "Lee"); map3.put ("Message ", "15123458905"); map4.put ("Title", "Tana"); map4.put ("Message", "13789463869"); map5.put ("Title", "dog "); map5.put ("Message", "18747389046"); map6.put ("Title", ""); map6.put ("Message", "18009382758"); list. add (map1); list. add (map2); list. add (map3); list. add (map4); list. add (map5); list. add (map6); return list ;}}

1) Click the first item:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/100J94609-0.jpg "style =" float: none; "title =" Capture. JPG "/>

2) Long press list options:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/100J92154-1.jpg "style =" float: none; "title =" Capture 1.JPG"/>

3) Click "View" in the menu to display the dialog box:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/100J92129-2.jpg "style =" float: none; "title =" Capture 2.JPG"/>

4) Click "delete" in the menu to display the dialog box:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/100J932V-3.jpg "style =" float: none; "title =" Capture 4.JPG"/>

5) Click the delete OK button to update the ListView list:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/100Ja1J-4.jpg "style =" float: none; "title =" Capture 5.JPG"/>


This article is from the MySpace blog, please be sure to keep this source http://wangzhaoli.blog.51cto.com/7607113/1293420

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.