ListView is one of the commonly used components in Android development. It is used to show the required items in a vertical list.
Next, let's take a look at the implementation method of ListView:
FirstIt is a common way to define in XML and then use findViewById in the activity to obtain it (this is quite basic, direct code)
XML:
Activity:
Package com. example. listview1; import java. util. arrayList; import java. util. list; import android. app. activity; import android. OS. bundle; import android. widget. arrayAdapter; import android. widget. listView; public class MainActivity extends Activity {ListView listView = null; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); listView = (ListView) findViewById (R. id. list1); List
Data = new ArrayList
(); Data. add ("Test 1"); data. add ("Test 2"); data. add ("Test 3"); data. add ("Test 4"); ArrayAdapter
Adpter = new ArrayAdapter
(This, android. R. layout. simple_list_item_1, data); listView. setAdapter (adpter );}}
SecondIs implemented by inheriting ListActivity with activity
XML:
Activity:
Package com. example. listview2; import java. util. arrayList; import java. util. list; import android. app. listActivity; import android. OS. bundle; import android. widget. arrayAdapter; public class MainActivity extends ListActivity {protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); List
Data = new ArrayList
(); Data. add ("Test 1"); data. add ("Test 2"); data. add ("Test 3"); data. add ("Test 4"); ArrayAdapter
Adpter = new ArrayAdapter
(This, android. R. layout. simple_list_item_1, data); setListAdapter (adpter );}}
In the second type of Activity, we didn't declare ListView, but allowed Activy to inherit the ListActivity. setContentView was used to bind the ListView style and setListAdpter to bind data.
Note: In xml of the second writing method, the ListView id is written as android: id = "@ android: id/list", which is a fixed writing method, in this way, the program can find the definition of the ListView style.
We can see that the style shown above is the same.
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + signature + 1tC7udC0wcvPwsPm1eK2zsXk1sOhozwvcD4KPHA + PC9wPgo8cHJlIGNsYXNzPQ = "brush: java;"> The TextView id is written as @ android: id/empty. The TextView is displayed by default when the content of my ListView is empty.
In the API, Optionally, your custom view can contain another view object of any type to display when the list view is empty. this "empty list" notifier must have an id "android: id/empty ". note that when an empty view is present, the list view will be hidden when there is no data to display.
After I comment out the code for adding data to ArrayList In the second Activity, the running effect is as follows:
The preceding figure shows the basic usage of ListView. The question about Data Binding in ListView is described in the next article.