Android - 简单listview

非淡泊无以明志,非宁静无以致远。这篇文章主要讲述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 11private ListView listView; 12private String[] adapterData; 13 14 15 16@Override 17protected void onCreate(Bundle savedInstanceState) 18{ 19super.onCreate(savedInstanceState); 20setContentView(R.layout.activity_main); 21 22listView = (ListView) findViewById(R.id.main); 23 24/* 我们要在listView上面没条显示的数据,放到一个数组中 */ 25adapterData = https://www.songbingjia.com/android/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 */ 53listView.setAdapter(new ArrayAdapter< String> (MainActivity.this, 54android.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

文章图片

 

    推荐阅读