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 ;}}