1 package com.example.zhangmeng.listview; 2 3 import android.support.v7.app.AppCompatActivity; 4 import android.os.Bundle; 5 import android.widget.ArrayAdapter; 6 import android.widget.ListView; 7 8 public class MainActivity extends AppCompatActivity { 9 private String [] data={"red","green","blue","black","white"}; 10 11 @Override 12 protected void onCreate(Bundle savedInstanceState) { 13 super.onCreate(savedInstanceState); 14 setContentView(R.layout.activity_main); 15 16 17 ArrayAdapter<String> adapter=new ArrayAdapter<>(MainActivity.this,android.R.layout.simple_list_item_1,data); 18 ListView listView=(ListView) findViewById(R.id.lv); 19 listView.setAdapter(adapter); 20 } 21 }
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:paddingBottom="@dimen/activity_vertical_margin" 7 android:paddingLeft="@dimen/activity_horizontal_margin" 8 android:paddingRight="@dimen/activity_horizontal_margin" 9 android:paddingTop="@dimen/activity_vertical_margin" 10 tools:context="com.example.zhangmeng.listview.MainActivity"> 11 12 <ListView 13 android:id="@+id/lv" 14 android:layout_width="wrap_content" 15 android:layout_height="wrap_content" 16 /> 17 </RelativeLayout>