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>
预览:
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步