码迷,mamicode.com
首页 > 其他好文 > 详细

ListView和Adapter信息显示

时间:2017-05-02 23:44:44      阅读:328      评论:0      收藏:0      [点我收藏+]

标签:福州   tools   near   姓名   create   tool   count   上下文   set   

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

</LinearLayout>
</RelativeLayout>



<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/ListView"
>


</ListView>

</LinearLayout>

<?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/tv_xm"
/>
<TextView
android:id="@+id/tv_nl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="年龄:25"
/>
<TextView
android:id="@+id/tv_yx"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="邮箱:ffczk86@gmail.com"
/>
<TextView
android:id="@+id/tv_dz"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="地址:厦门市"
android:textSize="20sp"
/>
</LinearLayout>

package com.example.yz.myapplicatio9n;

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.LinearLayout,fragment);
transaction.commit();
}
}

import android.widget.ListView;

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

public class istViewFragment extends Fragment {
@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.ListView);
CustomAdapter adapter = new CustomAdapter(getActivity(),datas);
listView.setAdapter(adapter);
return view;
}

}


package com.example.yz.myapplicatio9n;

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.tv_yx);
TextView dz = (TextView)view.findViewById(R.id.tv_dz);
TextView xm=(TextView)view.findViewById(R.id.tv_xm);
TextView nl=(TextView)view.findViewById(R.id.tv_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;
}
}

package com.example.yz.myapplicatio9n;

/**
* 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 yx, String dx) {
this.xm=xm;
this.nl=nl;
this.yx=yx;
this.dz=dx;
}

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;
}
}















ListView和Adapter信息显示

标签:福州   tools   near   姓名   create   tool   count   上下文   set   

原文地址:http://www.cnblogs.com/CCyz/p/6798637.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!