Android学习第三天--listview方式一
第一种listview
xml文件中
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:id="@+id/listview1" android:entries="@array/info" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout>
在res下的values下添加下面的文件
<?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="info"> <item>test1</item> <item>test1</item> <item>test1</item> <item>test1</item> <item>test1</item> <item>test1</item> <item>test1</item> <item>ltest1n</item> <item>test1</item> <item>test1</item> <item>test1</item> <item>test1</item> </string-array> </resources>
主java文件中
package cn.core.test; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ListView; public class MainActivity extends Activity { ListView listView =null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView=(ListView)findViewById(R.id.listview1); listView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3) { System.out.println(arg0.getItemAtPosition(position).toString()); } }); } }