Listview和Adapter作业

Posted

tags:

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

安卓作业:使用ListView和自定义Adapter完成列表信息显示

技术分享

1.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="cn.edu.niit.liebiaoxinxixianshi.QiuActivity">

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

</LinearLayout>

</RelativeLayout>
2.四个TextView布局:
<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/List1"
android:text="@string/name"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/List2"
android:text="@string/age"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/List3"
android:text="@string/mail"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/List4"
android:text="@string/address"/>
3.定义String<resources>
    <string name="app_name">liebiaoxinxixianshi</string>
<string name="list">目录</string>
<string name="name">姓名</string>
<string name="age">年龄</string>
<string name="mail">邮箱</string>
</resources>
4.java文件
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
}
}
增加数据,数据准备:
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]","地址:厦门市"));
datas.add(new Classinfo("姓名:刘玄德","年龄:25","邮箱:[email protected]","地址:福州市"));

自定义Adapter 通过Adapter来获得数据

技术分享

技术分享

 























































































以上是关于Listview和Adapter作业的主要内容,如果未能解决你的问题,请参考以下文章

ListView作业

片段中的 Listview 适配器给出空指针异常

view、adapter、getActivity()、infragment 都是 null

转Android自定义Adapter的ListView的思路及代码

ListView和Adapter

android 打造ListView和Gridview万能adapter适配器(附源码)