import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
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 ActivityArray extends Activity {
//声明ListView控件
ListView lv;
//声明数组适配器
ArrayAdapter<String> adapter;
//声明一个List的集合 用来和arrayAdapter进行绑定
List<String> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_array);
//调用我们定义的初始化界面的方法
initView();
}
/**
* 定义一个初始化界面的方法
*/
private void initView(){
lv = (ListView) findViewById(R.id.array_lv);
list = new ArrayList<String>();
for (int i = 0; i < 100; i++) {
list.add("我是第" + i + "数据");
}
//实例化我们的ArrayAdapter
//参数1:上下文
//参数2:ListView的Item显示样式的布局
//参数3:需要显示的数据集合
adapter = new ArrayAdapter<String>(
this,
R.layout.array_item,
list);
//让ListView控件绑定一个适配器
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(ActivityArray.this,
list.get(position) + "", 0).show();
}
});
}
}