安卓作业:使用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来获得数据
时间: 2024-10-12 11:26:25