2017年4月23日作业,ListView和Adapter
作业要求, 使用ListView和自定义Adapter完成列表信息显示,示例效果如图所示
首先是写这次作业的思路,写了两个布局文件,一个是主布局文件,另一个是定义的ListView所需要显示出来的样子的模板。
主布局文件很简单,只写了一个ListView,代码和效果图如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
tools:context="com.example.a15083.testlistview.MainActivity">
<ListView
android:id="@+id/lt_info"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
ListView显示的模板布局代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="姓名:"/>
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="年龄:"/>
<TextView
android:id="@+id/tv_age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="邮箱:"/>
<TextView
android:id="@+id/tv_mail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="地址:"/>
<TextView
android:id="@+id/tv_addess"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"/>
</LinearLayout>
</LinearLayout>
<ImageView
android:id="@+id/iv_tu"
android:layout_width="90dp"
android:layout_height="100dp" />
</LinearLayout>
我是先封装了一个Person类,储存一些人物的信息:
package com.example.a15083.testlistview;
/**
* Created by 15083 on 2017/4/30 0030.
*/
public class PersonInfo {
private String name;
private String age;
private String mail;
private String addess;
private int tu;
public PersonInfo(String name, String age, String mail, String addess, int tu) {
this.name = name;
this.age = age;
this.mail = mail;
this.addess = addess;
this.tu = tu;
}
public String getName() {
return name;
}
public String getAge() {
return age;
}
public String getMail() {
return mail;
}
public String getAddess() {
return addess;
}
public int getTu() {
return tu;
}
}
然后是写了一个自定义的Adapter类:
package com.example.a15083.testlistview;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
/**
* Created by 15083 on 2017/4/30 0030.
*/
public class InfoAdapter extends BaseAdapter {
private List<PersonInfo> info;
private Context context;
public InfoAdapter(Context context , List info){
this.context = context;
this.info = info;
}
@Override
public int getCount() {
return info.size();
}
@Override
public Object getItem(int i) {
return info.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder holder = null;
//1.获取view
holder = new ViewHolder();
view= LayoutInflater.from(context).inflate(R.layout.list_item,null);
//2.获取控件对象
holder.name=(TextView) view.findViewById(R.id.tv_name);
holder.age = (TextView)view.findViewById(R.id.tv_age);
holder.mail = (TextView)view.findViewById(R.id.tv_mail);
holder.addess = (TextView)view.findViewById(R.id.tv_addess);
holder.tu = (ImageView)view.findViewById(R.id.iv_tu);
PersonInfo personInfo= info.get(i);
holder.name.setText(personInfo.getName());
holder.age.setText(personInfo.getAge());
holder.mail.setText(personInfo.getMail());
holder.addess.setText(personInfo.getAddess());
holder.tu.setImageResource(personInfo.getTu());
return view;
}
class ViewHolder{
TextView name;
TextView age;
TextView mail;
TextView addess;
ImageView tu;
}
}
然后是需要在MainActivity中实现想要的效果分为以下几个步骤:
1.创建Adapter并给Adapter传入一些数据
2.创建ListView
3.将数据加载到ListView
下面是代码:
package com.example.a15083.testlistview;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private InfoAdapter infoAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<PersonInfo> info = getInfo();
//1.创建adapter
infoAdapter =new InfoAdapter(MainActivity.this,info);
//2.创建ListView
ListView listView =(ListView)findViewById(R.id.lt_info);
//3.将数据加载到ListView
listView.setAdapter(infoAdapter);
}
//这里填写一些本来就写死的数据
public ArrayList<PersonInfo> getInfo() {
ArrayList<PersonInfo> info = new ArrayList<>();
info.add(new PersonInfo("王林","未知","wanglin@zhuque.com","朱雀星",R.drawable.wl));
info.add(new PersonInfo("李慕婉","未知","waner@zhuque.com","朱雀星",R.drawable.mw));
info.add(new PersonInfo("司徒南","未知","sinan@zhuque.com","朱雀星",R.drawable.sn));
info.add(new PersonInfo("清水","未知","qingshui@qingshui.com","清水国",R.drawable.qs));
return info;
}
}
下面附上程序运行效果图: