ListView 的使用

package com.mylistview.pk;


import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;


public class MainViewActivity extends Activity {
/** Called when the activity is first created. */
private ListView listView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//这里还可以传入对象不过最好冲写该对象的toString()方法 替换STR
String[]str = {"zzz","xxx","ssss"};

listView =(ListView)findViewById(R.id.listView);
ArrayAdapter <String>adapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, str);
listView.setAdapter(adapter);

listView.setOnItemClickListener(new OnItemClickListener() {


@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String text =listView.getItemAtPosition(position).toString(); //得到点击的值
Toast.makeText(MainViewActivity.this, text, 1).show();
}
});

}


}


-------------------------------------------------------------------------------------------------------------------------------------------------------

XML mian.xml



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ListView
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="match_parent"

/>


</LinearLayout>




你可能感兴趣的:(ListView)