学习笔记:Android控件ListView的简单使用方法。
近段时间利用工余时间继续Android的学习,试了一下ListView的用法。从网上查看的资料非常多,方法也很灵活,我先学一下它的最简单方法,为ListView添加数据。这次试了用两种方法向ListView添加字符串数组,一次只用一个哈。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //给ListView添加内容的一种方法。 ListView list1 = new ListView(this); list1.findViewById(R.id.listView1); String[] array_str = new String[] { "a", "b", "c" }; ArrayAdapter<String> str_list1 = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,array_str); list1.setAdapter(str_list1); setContentView(list1); //给ListView添加内容的另一种方法,这种方法比较灵活,想看运行效果就一次只运行一个哈 ListView list2 = new ListView(this); list2.findViewById(R.id.listView2); ArrayAdapter<String> str_list2 = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1); str_list2.add("A"); str_list2.add("B"); str_list2.add("C"); list2.setAdapter(str_list2); setContentView(list2); } @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; } } |