第一,主视图如下:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" > <ListView android:id="@+id/demo_list_view" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout>
条件视图,用于列表的视图如下
<?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:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:id="@+id/age" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:id="@+id/sex" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout>
用于主activity, 代码
package com.example.myapplicationlf; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.ListView; import android.widget.SimpleAdapter; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ListView demoListView = findViewById(R.id.demo_list_view); List<Map<String, ?>> dataList = new ArrayList<>(); for(int i = 0; i<50; i++){ Map<String, String> map = new HashMap<>(); map.put("name", "1"); map.put("sex", "男"); dataList.add(map); } String[] from ={"name","age","sex"}; int[] to = {R.id.name,R.id.age,R.id.sex}; SimpleAdapter simpleAdapter = new SimpleAdapter(this, dataList,R.layout.item,from,to); demoListView.setAdapter(simpleAdapter); } }这
这儿适配器不是自定义的,所以一个主actity 就完事,所以称简单适配器,这儿功能简单 ,不能根据不同条件显示不同的子列表
标签:java,适配器,id,import,android,安卓,SimpleAdapter From: https://www.cnblogs.com/fgxwan/p/17779417.html