4.27

xml主页面

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.ershiqiapplication.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:id="@+id/out"
        >

    </LinearLayout>
</RelativeLayout>

list_view

<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.ershiqiapplication.ListViewFragment">

    <!-- TODO: Update blank fragment layout -->
    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/lv"
        >


    </ListView>

</LinearLayout>

list_item

<?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">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="姓名:蔡志坤"
        android:textSize="20sp"
        android:id="@+id/xm"
        />
    <TextView
        android:id="@+id/nl"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:text="年龄:25"
        />
    <TextView
        android:id="@+id/yx"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="邮箱:ffczk86@gmail.com"
        />
    <TextView
        android:id="@+id/dz"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="地址:厦门市"
        android:textSize="20sp"
        />
</LinearLayout>

MainActivity

package com.example.ershiqiapplication;

import android.app.Fragment;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        Fragment fragment=new ListViewFragment();
        transaction.add(R.id.out,fragment);
        transaction.commit();//显示fragment
    }
}

listviewfragment代码

package com.example.ershiqiapplication;


import android.app.Fragment;
import android.os.Bundle;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

public class ListViewFragment extends Fragment {
    private CustomAdapter adapter;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_list_view, container, false);

        List<Classinfo>datas= new ArrayList<>();//集合框架
        datas.add(new Classinfo("姓名:蔡志坤","年龄:25","邮箱:ffczk86@gmail.com","地址:厦门市"));
        datas.add(new Classinfo("姓名:李杰华","年龄:25","邮箱:aa@bb.com","地址:漳州市"));
        datas.add(new Classinfo("姓名:张亮","年龄:25","邮箱:cc@gmail.com","地址:厦门市"));
        datas.add(new Classinfo("姓名:刘玄德","年龄:25","邮箱:ffczk86@gmail.com","地址:福州市"));
        ListView listView =(ListView)view.findViewById(R.id.lv);//获取list控件
        adapter = new CustomAdapter(getActivity(),datas);//加载适配器
        listView.setAdapter(adapter);
        return view;
    }

}

Classinfo 的代码

package com.example.ershiqiapplication;

/**
 * Created by 栋栋栋 on 2017/4/27.
 */

public class Classinfo {
    private String xm;
    private String nl;
    private String yx;
    private String dz;

    public Classinfo(String xm, String nl, String s2, String s3) {
        this.xm=xm;
        this.nl=nl;
        this.yx=s2;
        this.dz=s3;
    }

    public String getXm() {
        return xm;
    }

    public void setXm(String xm) {
        this.xm = xm;
    }

    public String getNl() {
        return nl;
    }

    public void setNl(String nl) {
        this.nl = nl;
    }

    public String getYx() {
        return yx;
    }

    public void setYx(String yx) {
        this.yx = yx;
    }

    public String getDz() {
        return dz;
    }

    public void setDz(String dz) {
        this.dz = dz;
    }
}

CustomAdater自定义

package com.example.ershiqiapplication;

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 栋栋栋 on 2017/4/27.
 */

public class CustomAdapter extends BaseAdapter {
    private List<Classinfo> datas ;
    private Context content;
    //通过构造方法获取需要的对象:上下文的数据
    public CustomAdapter(Context content, List datas){
        this.content=content;
        this.datas=datas;
    }

    @Override
    public int getCount() {
        return datas.size();//一定要返回list数据的长度,非常重要!!!
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
    //自定义Adapter一定要重写getView()方法
    //int i :datas的索引,为了获取所在位置的数据
    //View view:这条数据的显示的载体
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        if (view ==null){
            view= LayoutInflater.from(content).inflate(R.layout.list_item,null);
        }
        //获取控件
        TextView yx=(TextView)view.findViewById(R.id.yx);
        TextView dz = (TextView)view.findViewById(R.id.dz);
        TextView xm=(TextView)view.findViewById(R.id.xm);
        TextView nl=(TextView)view.findViewById(R.id.nl);
        //赋值
        Classinfo classInfo=datas.get(i);
        yx.setText(classInfo.getYx());
        dz.setText(classInfo.getDz());
        xm.setText(classInfo.getXm());
        nl.setText(classInfo.getNl());
        //返回view
        return view;
    }
}

效果图
1127051-20170427125908147-704682253.png

转载于:https://www.cnblogs.com/wgdcl/p/6773682.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值