android listView 总结
16lz
2021-01-26
android 中的listview是最常用的控件之一。listview 显示数据,需要使用adapter进行适配。常用的adapter有Arrayadapter,simpleAdapter ,和BaseAdapter 。
Arrayadapter 主要用于显示只有文字的,simpleAdapter 和BaseAdapter 可以用来显示文字,图片 以及在list的每个item中添加 其他的控件如Button,Checkbox 并对其作出监听。
Arrayadapter 的效果图:
下面是Arrayadapter中的代码
protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.arrayadapter);lv = (ListView) findViewById(R.id.lvArrayAdapter);lv.setAdapter(new ArrayAdapter<String >(this, android.R.layout.simple_list_item_1, getDatas()));//this,表示上下文,android.R.layout.simple_list_item1,表示行布局文件,这里调用的是系统自带的,也可以使用自定义的;//getDatas(),返回的是填充到adapter中的数据。}private ArrayList<String> getDatas(){ArrayList<String> datas = new ArrayList<String>();datas.add("test1");datas.add("test2");datas.add("test3");datas.add("test4");datas.add("test5");datas.add("test6");return datas;}
simpleAdapeter 的效果图:
再贴出来simpleAdapter中的代码
protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.simple_adapter);lv = (ListView)findViewById(R.id.lvsimple);SimpleAdapter adapter = new SimpleAdapter(this,//this 表示上下文,getData(),//adapter 中填充的数据R.layout.simple_item,//list 中自定义的行布局文件new String[] {"img","name","age"},//key 值//value 值,必须与key 值相对应new int[]{R.id.imgSimpleItemImg,R.id.tvSimpleItemName,R.id.tvSimpleItemAge});lv.setAdapter(adapter);}public List<Map<String, Object>> getData(){List<Map<String ,Object>> list = new ArrayList<Map<String,Object>>();Map<String, Object> map = new HashMap<String, Object>();map.put("name", "imesong1");map.put("age","21");map.put("img", R.drawable.img1);list.add(map);map = new HashMap<String, Object>();map.put("name", "imesong2");map.put("age","22");map.put("img", R.drawable.img2);list.add(map);map = new HashMap<String, Object>();map.put("name", "imesong3");map.put("age","23");map.put("img", R.drawable.img3);list.add(map);return list;}
R.layout.simple_item 自定义的布局文件代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal"android:layout_width="match_parent" android:layout_height="match_parent"><ImageView android:id="@+id/imgSimpleItemImg" android:layout_width="wrap_content" android:layout_height="wrap_content" /><View android:layout_width="30dp" android:layout_height="match_parent" /><LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content"android:orientation="vertical"><TextView android:id="@+id/tvSimpleItemName" android:layout_width="wrap_content" android:layout_height="wrap_content" /><TextView android:id="@+id/tvSimpleItemAge" android:layout_width="wrap_content" android:layout_height="wrap_content" /></LinearLayout></LinearLayout>
baseAdapter 的效果图:
下面是baseadapter中的代码,这个是自定义的myAdapter,重写了baseadapter中的方法,主要是getview()方法。
public class myAdapter extends BaseAdapter {private Context context;private LayoutInflater minflater;private ArrayList<Person> datas = new ArrayList<Person>();public myAdapter(Context context, ArrayList<Person> datas) {super();this.context = context;this.datas = datas;this.minflater = LayoutInflater.from(context);}@Overridepublic int getCount() {// TODO Auto-generated method stubreturn datas.size();}@Overridepublic Object getItem(int position) {// TODO Auto-generated method stubreturn datas.get(position);}@Overridepublic long getItemId(int position) {// TODO Auto-generated method stubreturn position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {Holder holder = null;if(convertView == null){//先判断convertView是否为空holder = new Holder();convertView = minflater.inflate(R.layout.base_item, null);//使用自定义的行布局文件//实例化控件holder.img = (ImageView) convertView.findViewById(R.id.imgBaseItemImg);holder.name = (TextView) convertView.findViewById(R.id.tvBaseItemName);holder.age = (TextView) convertView.findViewById(R.id.tvBaseItemAge);convertView.setTag(holder);}else {holder = (Holder) convertView.getTag();}//为控件赋值holder.img.setBackgroundResource(datas.get(position).getImg());holder.name.setText(datas.get(position).getName());holder.age.setText(datas.get(position).getAge()+"");//年龄为int 类型,转换 为String,不然会报bugreturn convertView;}class Holder {ImageView img;TextView name;TextView age;}}
下面是myBaseAdapter中的java代码
public class myBaseAdapter extends Activity {private ListView lv;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.base_adapter);ArrayList<Person> datas = new ArrayList<Person>();datas.add(new Person("imesong1",21,R.drawable.img1));datas.add(new Person("imesong2", 22, R.drawable.img2));datas.add(new Person("imesong3", 23, R.drawable.img3));lv = (ListView) findViewById(R.id.lvbase);lv.setAdapter(new myAdapter(this, datas));}//表示个人信息的内部类class Person {String name;int age;int img;public Person(String name, int age, int img) {this.name = name;this.age = age;this.img = img;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public int getImg() {return img;}public void setImg(int img) {this.img = img;}}}myAdapter 中自定义的行布局文件 代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal"android:layout_width="match_parent" android:layout_height="match_parent"><ImageView android:id="@+id/imgBaseItemImg" android:layout_width="wrap_content" android:layout_height="wrap_content" /><View android:layout_width="30dp" android:layout_height="match_parent" /><LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content"android:orientation="horizontal"><TextView android:id="@+id/tvBaseItemName" android:layout_width="wrap_content" android:layout_height="wrap_content" /><View android:layout_width="30dp" android:layout_height="match_parent" /><TextView android:id="@+id/tvBaseItemAge" android:layout_width="wrap_content" android:layout_height="wrap_content" /></LinearLayout></LinearLayout>
代码基本上都已经贴出来了,又不知失误之处,请指教~
更多相关文章
- listview 常用智识总结
- android 使用mediaplayer播放报java.io.IOException: setDataSou
- android 控件 单项选择(RadioGroup,RadioButton)
- node.js+Android(安卓)http请求响应
- Android(安卓)UI 基本布局 weight 在 LinearLayout 中
- android 引导界面的实现
- Android(安卓)自动化测试―robotium(四)CheckBox控件
- listview每一条中间有空隙问题解决
- Android的应用程序的异常处理2