Android listview and ListAdapter are used together. androidlistadapter

Source: Internet
Author: User

Android listview and ListAdapter are used together. androidlistadapter

The data display control that comes with Android in ListView. To use ListView to fill data, you must use an adapter to fill the data. Here we will introduce the ListAdapter adapter, as shown below:

Java source code:

Package com. example. listview1; import java. util. arrayList; import java. util. list; import android. OS. bundle; import android. app. activity; import android. view. menu; import android. view. view; import android. widget. adapter; import android. widget. adapterView; import android. widget. adapterView. onItemClickListener; import android. widget. arrayAdapter; import android. widget. listAdapter; import android. widget. listView; I Mport android. widget. simpleAdapter; import android. widget. toast; public class MainActivity extends Activity {private ListView listview; private ListAdapter adapter; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); listview = (ListView) findViewById (R. id. listView1); listview. setChoiceMode (ListView. CHOICE_MODE_SIN GLE); adapter = new ArrayAdapter <String> (MainActivity. this, android. r. layout. simple_list_item_single_choice, getsource (); listview. setAdapter (adapter); listview. setOnItemClickListener (new OnItemClickListener () {@ Override public void onItemClick (AdapterView <?> Arg0, View arg1, int arg2, long arg3) {// TODO automatically generated method stub Toast. makeText (MainActivity. this, ">" + adapter. getItem (arg2), 1 ). show () ;}}) ;}@ Override public boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true;} public List <String> getsource () {List <String> list = new ArrayList <String> (); for (int I = 0; I <20; I ++) {list. add ("Welcome," + I);} return list ;}}

 

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.