ListView小结

ListView简单用法

定义ListView控件

ListView listview=(ListView) findViewById(R.id.listview);

以显示字符串的ListView为例,数据传递需要借助适配器,我们先定义字符串数组,将数据传给ArrayAdapter,再用ListView的setAdapter()方法,将适配器对象传递进去。

String[] data={"abc","def","ghi","jkl"};
//因为传递字符串,所以ArrayAdapter泛型指定为
ArrayAdapter adapter=
new ArrayAdapter(MainActivity.this,android.R.layout.simple_list_item_1,data);
listview.setAdapter(adapter);

ListView点击事件

listview.setOnItemClickListener(new AdapterView.OnItemClickListener(){
 
            @Override
            public void onItemClick(AdapterView parent, View view, int position,long arg3) {
              //具体实现
  String s=data.get(position);
  Toast.makeText(MainActivity.this,s,Toast.LENGTH_SHORT).show();
           }
             
        });

你可能感兴趣的:(ListView小结)