Android - 简单listview
Posted 生如逆旅 一苇以航
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android - 简单listview相关的知识,希望对你有一定的参考价值。
1 //MainActivity.java 2 package com.example.zc.listviewdemo; 3 4 import android.support.v7.app.AppCompatActivity; 5 import android.os.Bundle; 6 import android.widget.ArrayAdapter; 7 import android.widget.ListView; 8 9 public class MainActivity extends AppCompatActivity { 10 11 private ListView listView; 12 private String[] adapterData; 13 14 15 16 @Override 17 protected void onCreate(Bundle savedInstanceState) 18 { 19 super.onCreate(savedInstanceState); 20 setContentView(R.layout.activity_main); 21 22 listView = (ListView) findViewById(R.id.main); 23 24 /* 我们要在listView上面没条显示的数据,放到一个数组中 */ 25 adapterData = new String[] { 26 "Afghanistan", 27 "Albania", 28 "Algeria", 29 "American Samoa", 30 "Andorra", 31 "Angola", 32 "Anguilla", 33 "Antarctica", 34 "Antigua and Barbuda", 35 "Argentina", "Armenia", 36 "Aruba", 37 "Australia", "Austria", 38 "Azerbaijan", "Bahrain", 39 "Bangladesh", "Barbados", 40 "Belarus", "Belgium", "Belize", 41 "Benin", "Bermuda", "Bhutan", "Bolivia", 42 "Bosnia and Herzegovina", "Botswana", 43 "Bouvet Island" }; 44 45 46 /* 这个是数组string类型的数组 */ 47 // ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>( 48 // ArrayListDemo.this, android.R.layout.simple_list_item_1, 49 // adapterData); 50 51 52 /* 设置ListView的Adapter */ 53 listView.setAdapter(new ArrayAdapter<String>(MainActivity.this, 54 android.R.layout.simple_list_item_1, adapterData)); 55 56 //@param1 listview上一个Activity 57 //@param2 系统自带的xml,简单的一行 58 //@param3 写入的数据 59 60 61 62 63 64 65 } 66 }
//activity_main.xml <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.zc.listviewdemo.MainActivity"> <ListView android:id="@+id/main" android:layout_width="wrap_content" android:layout_height="wrap_content"> </ListView> </android.support.constraint.ConstraintLayout>
预览:
以上是关于Android - 简单listview的主要内容,如果未能解决你的问题,请参考以下文章
片段中的Android ListView - 获取新数据后刷新表的问题
Listview 项目未在 Fragment Android 中显示