List

这次作业大致就是实现列表,编码如下

首先是基础的xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context="com.example.listzy.MainActivity">

<ListView

android:layout_width="match_parent"

android:layout_height="match_parent"

android:id="@+id/list" android:scrollbars="vertical"/>

<LinearLayout

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="vertical">

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/title1"

android:text="@string/name"/>

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/title2" android:text="@string/age"/>

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/title3"

android:text="@string/mail"/>

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/title4"

android:text="@string/address"/>

</LinearLayout>

</LinearLayout>

然后就是java代码的实现

public class Classinfo

{

private String title1;

private String title2;

private String title3;

private String title4;

public Classinfo(String title1, String title2, String title3,String title4)

{

this.title1=title1; this.title2=title2;

this.title3=title3; this.title4=title4;

}

创建Adapter

adapter = new SimpleAdapter(

getActivity(),

getData(),

R.layout.activity_main,

new String[]{"title1", "title2", "title3", "title4"},

new int[]{R.id.title1, R.id.title2, R.id.title3, R.id.title4});

ListView listView = (ListView)view.findViewById(R.id.list);

listView.setAdapter(adapter);

添加监听事件

@Override public void onItemClick(AdapterView<?> adapterView,View view, int position, long id)

{

String data = (String) adapterView.getItemAtPosition(position);

使用Map接口

private List<HashMap<String, Object>> getData(){
    List<HashMap<String, Object>> datas = new ArrayList<>(); // 给list增加一条数据 HashMap<String, Object> data = new HashMap<>(); // Map映射添加数据 data.put("title1", "蔡志坤"); data.put("title2", "25"); data.put("title3", "ffczk86@gmail.com"); data.put("title4", "厦门市"); // 将这个map放到list中 datas.add(data); data = new HashMap<>(); data.put("title1", "李杰华"); data.put("title2", "25"); data.put("title3", "aa@bb.com"); data.put("title4", "漳州市"); datas.add(data); data = new HashMap<>(); data.put("title1", "张亮"); data.put("title2", "25"); data.put("title3", "cc@gmail.com"); data.put("title4", "厦门市"); datas.add(data); data = new HashMap<>(); data.put("title1", "刘玄德"); data.put("title2", "25"); data.put("title3", "ffczk86@gmail.com"); data.put("title4", "福州市"); datas.add(data); return datas; } }

 CusomAdapter

public class CustomAdapter extends BaseAdapter {

private List dates; private Context context;

public CustomAdapter(Context context, List dates) {

this.dates = dates; this.context = context;

}

@Override public int getCount() {

return dates.size();

 }

@Override

public Object getItem(int i) {

return dates.get(i);

}

@Override

public long getItemId(int i) { return i;

}

@Override

public View getView(int i, View view, ViewGroup parent) {

if (view == null) {

view = LayoutInflater.from(context).inflate(R.layout.activity_main, null);

}

 TextView title1 = (TextView) view.findViewById(R.id.title1);

TextView title2 = (TextView) view.findViewById(R.id.title2);

TextView title3 = (TextView) view.findViewById(R.id.title3);

TextView title4 = (TextView) view.findViewById(R.id.title4);

】Classinfo classInfo = datas.get(i);

title1.setText(classinfo.gettitle1());

title2.setText(classinfo.getTitle2());

title3.setText(classinfo.gettitle3());

title4.setText(classinfo.gettitle4());

return null;

}

 

转载于:https://www.cnblogs.com/hero-c/p/6795788.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值