标签:
方法,在xml文件中添加一个ListView,然后在MainActivity中
private ListView listView;
private ArrayAdapter<String> arr_adapter;
listView = (ListView) findViewById(R.id.listView1);
String[] arr_data = {"明明","丽媛", "彭辉", "倩倩", "迪迪","李琰", "赛磊"};
/*
* 新建一个适配器
* ArrayAdapter(上下文,当前ListView加载的每一个列表项所对应的布局文件,数据源);
* 适配器加载数据源
*/
arr_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arr_data);
//视图加载适配器
listView.setAdapter(arr_adapter);
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" > </ListView> </RelativeLayout>
MainActivity
package com.thinker.arrayadapter; import android.app.Activity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.SimpleAdapter; public class MainActivity extends Activity { private ListView listView; private ArrayAdapter<String> arr_adapter; private SimpleAdapter simpleAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = (ListView) findViewById(R.id.listView1); String[] arr_data = {"明明","丽媛", "彭辉", "倩倩", "迪迪","李琰", "赛磊"}; /* * 新建一个适配器 * ArrayAdapter(上下文,当前ListView加载的每一个列表项所对应的布局文件,数据源); * 适配器加载数据源 */ arr_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arr_data); //视图加载适配器 listView.setAdapter(arr_adapter); } }
标签:
原文地址:http://www.cnblogs.com/rain-1/p/5164283.html