android之【ArrayAdapter】

main.xml

<!-- 直接使用数组资源给出列表项 -->
<ListView  
	android:layout_width="fill_parent" 
	android:layout_height="wrap_content" 
	android:entries="@array/books"
	android:divider="@drawable/red"
	android:headerDividersEnabled="false"
	/>
<!-- 使用ArrayAdapter提供列表项的ListView -->
<ListView  
	android:id="@+id/list2"
	android:layout_width="fill_parent" 
	android:layout_height="wrap_content" 
	android:divider="@drawable/green"
	/>

arrays.xml

<?xml version="1.0" encoding="UTF-8"?>
<resources>
	<string-array name="books">
		<item>Java</item>
		<item>C#</item>
		<item>IOS</item>
		<item>Android</item>
	</string-array>
</resources>


public class ArrayAdapterList extends Activity
{ 
	@Override   
	protected void onCreate(Bundle savedInstanceState)
	{   
		super.onCreate(savedInstanceState);   
		setContentView(R.layout.main);   
		ListView list2 = (ListView)findViewById(R.id.list2);
		//定义一个数组
		String[] arr ={"AAA" , "BBB" , "CCC"};
		//将数组包装ArrayAdapter
		ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
			this , android.R.layout.simple_list_item_1 , arr);
		//为ListView设置Adapter
		list2.setAdapter(arrayAdapter);	
	}  
}



你可能感兴趣的:(android,String)