1.java代码
package com.yun.activity;
import java.util.ArrayList;
import java.util.List;
import android.R;
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.ListView;
import android.widget.Toast;
public class MyListViewActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
ListView listView=new ListView(this);
//listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, getData()));
//listView.setAdapter(new ArrayAdapter<String>(this, R.layout.simple_list_item_1, getData()));
listView.setAdapter(new ArrayAdapter<String>(this, R.layout.simple_list_item_single_choice, getData()));
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
setTitle("你点击了第"+arg2+"行"+arg3);
Toast.makeText(MyListViewActivity.this, "你点击了第"+String.valueOf(arg2)+"行", Toast.LENGTH_SHORT).show();
}
});
setContentView(listView);
}
private List<String> getData(){
List<String> data=new ArrayList<String>();
for(int i=0;i<10;i++){
data.add("这是第"+String.valueOf(i)+"行");
}
return data;
}
}
2.执行结果