什么是适配器设计模式
初始化ListView
小案例:
布局:
<ListView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/lvStudent" />
<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" xmlns:android="http://schemas.android.com/apk/res/android" > <ImageView android:layout_width="64dp" android:layout_height="64dp" android:src="@drawable/image"/> <TableLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1.0" android:stretchColumns="*"> <TableRow android:layout_height="0dp" android:layout_weight="1.0"> <TextView android:id="@+id/tvId" android:layout_height="match_parent" android:gravity="center_vertical" android:textSize="20sp"/> <TextView android:id="@+id/tvName" android:layout_height="match_parent" android:gravity="center_vertical" android:textSize="20sp" /> </TableRow> <TableRow android:layout_height="0dp" android:layout_weight="1.0"> <TextView android:id="@+id/tvSex" android:layout_height="match_parent" android:gravity="center_vertical" android:textSize="20sp"/> <TextView android:id="@+id/tvAge" android:layout_height="match_parent" android:gravity="center_vertical" android:textSize="20sp"/> </TableRow> </TableLayout> </LinearLayout>
package com.example.listview; import java.util.ArrayList; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.webkit.WebView.FindListener; import android.widget.BaseAdapter; import android.widget.TextView; public class StudentAdapter extends BaseAdapter { private ArrayList<Student> stus; private LayoutInflater inflater; public StudentAdapter(Context context,ArrayList<Student> stus){ if(stus != null) this.stus = stus; else this.stus = new ArrayList<Student>(); this.inflater = LayoutInflater.from(context); } @Override public int getCount() { // TODO Auto-generated method stub return stus.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return null; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub View v = inflater.inflate(R.layout.item, null); TextView tvId = (TextView)v.findViewById(R.id.tvId); TextView tvName = (TextView)v.findViewById(R.id.tvName); TextView tvSex = (TextView)v.findViewById(R.id.tvSex); TextView tvAge = (TextView)v.findViewById(R.id.tvAge); Student stu = stus.get(position); tvId.setText(""+stu.getId()); tvName.setText(stu.getName()); tvSex.setText(stu.getSex()); tvAge.setText("" + stu.getAge()); return v; } }
学生类:
package com.example.listview; public class Student { private int id; private String name; private String sex; private int age; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Student(int id,String name,String sex,int age){ super(); this.id=id; this.name=name; this.sex=sex; this.age=age; } public Student(){ super(); } }
用来返回所有的信息:
package com.example.listview; import java.util.ArrayList; public class StudentBiz { public static ArrayList<Student> getStudents(){ ArrayList<Student> stus = new ArrayList<>(); for(int i=1; i <=10; i++){ Student stu = new Student(i, "同学"+ i,"男",23); stus.add(stu); } return stus; } }
package com.example.listview; import android.app.Activity; import android.os.Bundle; import android.widget.ListView; public class MainActivity extends Activity { private ListView lvStus; private StudentAdapter adapter; private void setupView(){ lvStus = (ListView) findViewById(R.id.lvStudent); adapter = new StudentAdapter(this,StudentBiz.getStudents()); lvStus.setAdapter(adapter); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setupView(); } }