原生布局并未多做修改
<?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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.kimdemon.listview.MainActivity">
<ListView
android:id="@+id/yf_list"
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="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="3dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="0">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/TextView1"
android:id="@+id/yf_name"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/TextView2"
android:id="@+id/yf_age"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/TextView3"
android:id="@+id/yf_com"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/TextView4"
android:id="@+id/yf_address"/>
</LinearLayout>
Java中,对于清单的定义
package com.example.kimdemon.listview;
public class Infomation {
private String yf_name;
private int yf_age;
private String yf_com;
private String yf_address;
public Infomation(String yf_name,String yf_com,String yf_address,int yf_age){
this.yf_name = yf_name;
this.yf_age = yf_age;
this.yf_com = yf_com;
this.yf_address = yf_address;
}
public String getYf_name(){
return yf_name;
}
public void setYf_name(String yf_name){
this.yf_name = yf_name;
}
public int getYf_age(){
return yf_age;
}
public void setYf_age(int yf_age){
this.yf_age = yf_age;
}
public String getYf_com(){
return yf_com;
}
public void setYf_com(String yf_com){
this.yf_name = yf_com;
}
public String getYf_address(){
return yf_address;
}
public void setYf_address(String yf_address){
this.yf_address = yf_address;
}
}
ListView的部分定义
package com.example.kimdemon.listview;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.List;
public class listview extends BaseAdapter {
private List<Infomation> info;
private Context context;
public listview(Context context,List info){
this.info = info;
this.context = context;
}
@Override
public int getCount(){
return info.size();
}
public Object getItem(int i){
return info.get(i);
}
public long getItemId(int i){
return i;
}
public View getView(int i, View view, ViewGroup viewGroup){
GiveWith with = null;
with = new GiveWith();
view = LayoutInflater.from(context).inflate(R.layout.listview,null);
with.yf_name = (TextView) view.findViewById(R.id.yf_name);
with.yf_age = (TextView) view.findViewById(R.id.yf_age);
with.yf_com = (TextView) view.findViewById(R.id.yf_com);
with.yf_address = (TextView) view.findViewById(R.id.yf_address);
Infomation infomation = info.get(i);
with.yf_name.setText(infomation.getYf_name());
with.yf_age.setText(infomation.getYf_age());
with.yf_com.setText(infomation.getYf_com());
with.yf_address.setText(infomation.getYf_address());
return view;
}
class GiveWith{
TextView yf_name;
TextView yf_age;
TextView yf_com;
TextView yf_address;
}
}
原生Java的代码则是
package com.example.kimdemon.listview;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private listview listViewit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<Infomation> info = getInfomation();
listViewit = new listview(MainActivity.this,info);
ListView listView = (ListView)findViewById(R.id.yf_list);
listView.setAdapter(listViewit);
}
public ArrayList<Infomation> getInfomation(){
ArrayList<Infomation> info = new ArrayList<>();
info.add(new Infomation("李易峰","未知","北京",29));
info.add(new Infomation("杨洋","未知","北京",26));
info.add(new Infomation("司南","未知","未知",20));
info.add(new Infomation("颜真","未知","未知",20));
return info;
}
}