【Android新手笔记三】listview

来源:互联网 发布:java数组的冒泡排序法 编辑:程序博客网 时间:2024/06/08 09:33

listview显示列表项,点击不同项,跳转到 不同页面。

先在values文件夹下新建一个xml文件,然后定义一个string_array,作为列表项。

 <string-array name="childfile_list">        <item>预防接种卡</item>        <item>新生儿家庭访视记录表</item>        <item>1岁以内儿童健康检查记录表</item>        <item>1~2岁儿童健康检查记录表</item>        <item>3~6岁儿童健康检查记录表</item>    </string-array>
之后在listview所在的activity中写Java代码。采用arrayadapter的方式,对确定的选项进行显示。

public class child_management extends AppCompatActivity{    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.child_file);        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array                .childfile_list, android.R.layout.simple_list_item_1);        adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);        ListView lv = (ListView) findViewById(R.id.listView);        lv.setAdapter(adapter);        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {            @Override            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {        //点击事件                Intent intent = null;                switch (position) {                    case 0:                        intent = new Intent(child_management.this, vaccination.class);break;                    case 1:                        intent = new Intent(child_management.this,new_born.class);break;                    case 2:                        intent = new Intent(child_management.this,under_1year_visit.class);break;                    case 3:                        intent = new Intent(child_management.this,between1and2years.class);break;                    case 4:                        intent = new Intent(child_management.this,between3and6years.class);break;                }                startActivity(intent);            }        });    }}


0 0
原创粉丝点击