ListView和Adapter信息显示

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ListView和Adapter信息显示相关的知识,希望对你有一定的参考价值。

<?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="邮箱:[email protected]"
/>
<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","邮箱:[email protected]","地址:厦门市"));
datas.add(new Classinfo("姓名:李杰华","年龄:25","邮箱:[email protected]","地址:漳州市"));
datas.add(new Classinfo("姓名:张亮","年龄:25","邮箱:[email protected]","地址:厦门市"));
datas.add(new Classinfo("姓名:刘玄德","年龄:25","邮箱:[email protected]","地址:福州市"));
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信息显示的主要内容,如果未能解决你的问题,请参考以下文章

Listview和Adapter作业

android listview数据刷新后保持原来的位置

Android中,我在Activity中用ListView来显示数据,adapter继承BaseAdapter,第一次可显示,重复运行不显示

android开发之路08(ListView&Adapter)

自定义Adapter显示listView

View(视图)——ListView概述Adapter分类