网上找了很多, 感觉很复杂 ,自己写了一个 , 觉得 很 好用
1, 这是正常的 第一个 list
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
LayoutInflater inflater = MainActivity.this.getLayoutInflater();
View view;
if (convertView==null) {
//因为getView()返回的对象,adapter会自动赋给ListView
view = inflater.inflate(R.layout.item_group, null);
}else{
view=convertView;
Log.i("info","有缓存,不需要重新生成"+position);
}
2, 在里面写 上 如下代码, 这 个就能 把 第二层 list 完美展示出来
View test_view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, null);
TextView tv_sheshin= test_view.findViewById(R.id.tv_sheshin);
TextView tv_mingxi= test_view.findViewById(R.id.tv_mingxi);
tv_sheshin.setText("设施名称 "+sheShiName);
LinerLaout_group.addView(test_view);```
3,这是 第二次 里需要 展示的 控件
<?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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<View
android:id="@+id/view"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="10dp"
android:background="#65000000" />
<TextView
android:id="@+id/tv_sheshin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/view"
android:layout_marginBottom="10dp"
android:layout_marginLeft="25dp"
android:text="TextView"
android:textSize="15sp" />
<TextView
android:id="@+id/tv_mingxi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/tv_sheshin"
android:layout_marginRight="20dp"
android:text="明细"
android:textColor="@color/colorzhang"
android:textSize="15sp" />
</RelativeLayout>
</LinearLayout>
详细代码 一个MainCaiJiActivity.java , 一个 item_group.xml
package com.myxiaomifeng.caiji;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.myxiaomifeng.bean.YueJianRenWu;
import com.myxiaomifeng.sql.DBHelper;
import com.myxiaomifeng.www.MainActivity;
import com.myxiaomifeng.www.R;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeSet;
public class MainCaiJiActivity extends AppCompatActivity {
//第一个listview
private RelativeLayout relat_ListView;
private DBHelper dbhelper;
private SQLiteDatabase sqldb;
private HashMap<String, TreeSet<String>> xitongNameMap=new HashMap();
List<String> xitongNameList=new ArrayList<>();
List<String> xiTongLeiXingList=new ArrayList<>();
List<String> sheShiNameList=new ArrayList<>();
List<String> jianceList=new ArrayList<>();
private String TABLE_NAME = "yuejianrenwu_biao";// 表名
private BaseAdapter adapter;//要实现的类
private LinearLayout LinerLaout_group;//https://blog.csdn.net/u011056436/article/details/55511382
ListView lv;
TextView tv_name, tv_leix,tv_sheshin;
private Handler handler;
//=============
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_cai_ji);
relat_ListView=findViewById(R.id.relat_ListView);
dbhelper = new DBHelper(this);
// 这里 空 指针 异常
try{
xitongNameList=newselect_xiTongName();
}catch (Exception e){
Toast.makeText(this, "没有数据,先采集吧。。。", Toast.LENGTH_SHORT).show();
MainCaiJiActivity.this.finish();
}
handler =new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
int arg1=msg.arg1;
String info= (String) msg.obj;
if (msg.what==1){
Toast.makeText(MainCaiJiActivity.this, info, Toast.LENGTH_SHORT).show();
}
}
};
lv=findViewById(R.id.listview_yj);
adapter = new BaseAdapter() {
@Override
public int getCount() {
// TODO Auto-generated method stub
return xitongNameList.size();//数目
}
@Override
public long getItemId(int position) {//取在列表中与指定索引对应的行id
return 0;
}
@Override
public Object getItem(int position) {//获取数据集中与指定索引对应的数据项
return xitongNameList.get(position);// 干 , 一个 系统名称里也就一个 系统类型啊 , 用个毛 集合呢
}
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
LayoutInflater inflater = MainCaiJiActivity.this.getLayoutInflater();
View view;
if (convertView==null) {
//因为getView()返回的对象,adapter会自动赋给ListView
view = inflater.inflate(R.layout.item_group, null);
}else{
view=convertView;
Log.i("info","有缓存,不需要重新生成"+position);
}
// tv_name, tv_leix,tv_sheshin
tv_name = (TextView) view.findViewById(R.id.tv_group_name);
tv_leix = (TextView) view.findViewById(R.id.tv_leix);
tv_sheshin = (TextView) view.findViewById(R.id.tv_sheshin);
LinerLaout_group= view.findViewById(R.id.LinerLaout_group);
LinerLaout_group.removeAllViews();
final String xiTongNam = (String) getItem(position); //设施名称
String xiTongLeiXing = null; //设施名称
tv_name.setText("系统名称 "+xiTongNam); //是否展开
xiTongLeiXingList =newselect_xiTongLeiXing(xiTongNam);
for (String temp :xiTongLeiXingList
) { //这里 需要优化一下, 如果 采集时 , 系统名称下 有多个系统类型时 这里 就 能将其分开 开始 不能将系统名称 分开
xiTongLeiXing=temp;
tv_leix.setText("系统类型 " + xiTongLeiXing); //是否展开
try{
sheShiNameList=newselect_sheShiName(xiTongNam,xiTongLeiXing);
for (int i=0;i<sheShiNameList.size();i++){
View test_view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, null);
TextView tv_sheshin= test_view.findViewById(R.id.tv_sheshin);
TextView tv_mingxi= test_view.findViewById(R.id.tv_mingxi);
TextView tv_panduanjieguo= test_view.findViewById(R.id.tv_panduanJieguo_bianse);
final String sheShiName=sheShiNameList.get(i);
tv_sheshin.setText("设施名称 "+sheShiName);
// 这里 对 判断结果进行 检测 符合 ,不符合,不能检查 进行变色 标识
// String temp= yue.getPanDuanJieGuo();
// if(!temp.equals("符合"))
// {
// tv_panduanjieguo.setTextColor(getResources().getColor(R.color.colorhong));
tv_panduanjieguo.setText("不符合/不能检查");
// tv_panduanjieguo.setText( yue.getPanDuanJieGuo());
// }
LinerLaout_group.addView(test_view);
final String finalXiTongLeiXing = xiTongLeiXing;
tv_mingxi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {// 明细按钮
Intent intent = new Intent();
intent.setClass(parent.getContext(), JianCeNeiRong_activity.class); // 进入第二个 列表里
Bundle bundle = new Bundle();
bundle.putString("xiTongNam",xiTongNam);
bundle.putString("xiTongLeiXing", finalXiTongLeiXing);
bundle.putString("sheShiName", sheShiName);
intent.putExtras(bundle);
parent.getContext().startActivity(intent);
}
});
}
}catch(Exception e){
System.out.println("包 异常了 194 行");
}
}
// 这里在进行一个 adapter 的展示 ,
return view;
}
};
lv.setAdapter(adapter);
init();
}
/**
* 分析: 1. 查询数据库 得到所 list
* 2. 便利 list ,
* 3,给 yuejianRenWuList 存入数据
*
*/
public void init() {
//2. webview展示网页
// if(edtUrl.getText().length()<=0){
//
// Toast.makeText(this, "输入ID有误,请重新输入", Toast.LENGTH_SHORT).show();
// }else{
// go(view);
// }
// yuejianRenWuList.clear();
// 着里面给需要的 集合 赋值
ArrayList<YueJianRenWu> list= selectany();
for (YueJianRenWu yueJianRenWu:
list ) {
// yuejianRenWuList.add(yueJianRenWu);
CeShiRenWuMethod(yueJianRenWu);
}
adapter.notifyDataSetChanged();
}
/**
* 清空 数据库
* @param view
*/
public void delTable(View view) {
// 这里可以吧 采集 按钮 还原
LinerLaout_group.removeAllViews();//清空布局
xitongNameList.clear();
xitongNameMap.clear();
adapter.notifyDataSetChanged();
del_biao();
}
/**
* 查询数据库的 所有数据 ( 可用于 判断 是否采集过)
*/
private ArrayList<YueJianRenWu> selectany() {
ArrayList<YueJianRenWu> list= new ArrayList<>();
sqldb = dbhelper.getReadableDatabase();
//创建游标
Cursor mCursor = sqldb.query(TABLE_NAME, new String[]{"yjId","xiTongName", "xiTongLeiXing","sheShiName", "jiance", "ceShiNeiRong", "ceShiNeiRong_url", "panDuanJieGuo", "wanZhengCeshiNeiRong", "ceShiTime"}, null, null, null, null, null);
//游标置顶
mCursor.moveToFirst();
//遍历
try{
do {
/*
'}';
*/
String yjId = mCursor.getString(mCursor.getColumnIndex("yjId"));
String xiTongName = mCursor.getString(mCursor.getColumnIndex("xiTongName"));
String xiTongLeiXing= mCursor.getString(mCursor.getColumnIndex("xiTongLeiXing"));
String sheShiName = mCursor.getString(mCursor.getColumnIndex("sheShiName"));
String jiance = mCursor.getString(mCursor.getColumnIndex("jiance"));
String ceShiNeiRong = mCursor.getString(mCursor.getColumnIndex("ceShiNeiRong"));
String ceShiNeiRong_url = mCursor.getString(mCursor.getColumnIndex("ceShiNeiRong_url"));
String panDuanJieGuo = mCursor.getString(mCursor.getColumnIndex("panDuanJieGuo"));
String wanZhengCeshiNeiRong = mCursor.getString(mCursor.getColumnIndex("wanZhengCeshiNeiRong"));
String ceShiTime = mCursor.getString(mCursor.getColumnIndex("ceShiTime"));
YueJianRenWu yueJianRenWu = new YueJianRenWu(yjId, xiTongName, xiTongLeiXing, sheShiName, jiance, ceShiNeiRong, ceShiNeiRong_url, panDuanJieGuo, wanZhengCeshiNeiRong, ceShiTime);
list.add(yueJianRenWu);// 这个是又创建的
} while (mCursor.moveToNext());
}catch(Exception e){
//System.out.println(e.printStackTrace());
}
return list;
}
/**
* 根据 系统名称, 系统类型 ,查找 对应的 设备名称
*/
private TreeSet<YueJianRenWu> selectanyLIst_Object(String xiTongName,String xiTongLeiXing) {
// MyPZJList.clear();
//select *from OnePian where chapte ='' and section='' 所有篇
//得到数据库对象
TreeSet<YueJianRenWu> treeSet= new TreeSet<>();
String temp = "xiTongName='" + xiTongName + "' and xiTongLeiXing='" + xiTongLeiXing + "'";
// 这里要判断 空
sqldb = dbhelper.getReadableDatabase();
//创建游标
Cursor mCursor = sqldb.query(TABLE_NAME, new String[]{"yjId", "sheShiName", "jiance", "ceShiNeiRong", "ceShiNeiRong_url", "panDuanJieGuo", "wanZhengCeshiNeiRong", "ceShiTime"}, temp, null, null, null, null);
//游标置顶
mCursor.moveToFirst();
//遍历
do {
/*
'}';
*/
String yjId = mCursor.getString(mCursor.getColumnIndex("yjId"));
// String xiTongName = mCursor.getString(mCursor.getColumnIndex("xiTongName"));
// String xiTongLeiXing= mCursor.getString(mCursor.getColumnIndex("xiTongLeiXing"));
String sheShiName = mCursor.getString(mCursor.getColumnIndex("sheShiName"));
String jiance = mCursor.getString(mCursor.getColumnIndex("jiance"));
String ceShiNeiRong = mCursor.getString(mCursor.getColumnIndex("ceShiNeiRong"));
String ceShiNeiRong_url = mCursor.getString(mCursor.getColumnIndex("ceShiNeiRong_url"));
String panDuanJieGuo = mCursor.getString(mCursor.getColumnIndex("panDuanJieGuo"));
String wanZhengCeshiNeiRong = mCursor.getString(mCursor.getColumnIndex("wanZhengCeshiNeiRong"));
String ceShiTime = mCursor.getString(mCursor.getColumnIndex("ceShiTime"));
YueJianRenWu yueJianRenWu = new YueJianRenWu(yjId, xiTongName, xiTongLeiXing, sheShiName, jiance, ceShiNeiRong, ceShiNeiRong_url, panDuanJieGuo, wanZhengCeshiNeiRong, ceShiTime);
treeSet.add(yueJianRenWu);// 这个是又创建的
} while (mCursor.moveToNext());
return treeSet;
}
/**
* 根据 系统名称, 系统类型 ,查找 对应的 设备名称
*/
private TreeSet<String> selectany(String xiTongName,String xiTongLeiXing) {
// MyPZJList.clear();
//select *from OnePian where chapte ='' and section='' 所有篇
//得到数据库对象
TreeSet<String> treeSet= new TreeSet<>();
String temp = "xiTongName='" + xiTongName + "' and xiTongLeiXing='" + xiTongLeiXing + "'";
// 这里要判断 空
sqldb = dbhelper.getReadableDatabase();
//创建游标
Cursor mCursor = sqldb.query(TABLE_NAME, new String[]{"yjId", "sheShiName", "jiance", "ceShiNeiRong", "ceShiNeiRong_url", "panDuanJieGuo", "wanZhengCeshiNeiRong", "ceShiTime"}, temp, null, null, null, null);
//游标置顶
mCursor.moveToFirst();
//遍历
do {
/*
'}';
*/
String yjId = mCursor.getString(mCursor.getColumnIndex("yjId"));
// String xiTongName = mCursor.getString(mCursor.getColumnIndex("xiTongName"));
// String xiTongLeiXing= mCursor.getString(mCursor.getColumnIndex("xiTongLeiXing"));
String sheShiName = mCursor.getString(mCursor.getColumnIndex("sheShiName"));
String jiance = mCursor.getString(mCursor.getColumnIndex("jiance"));
String ceShiNeiRong = mCursor.getString(mCursor.getColumnIndex("ceShiNeiRong"));
String ceShiNeiRong_url = mCursor.getString(mCursor.getColumnIndex("ceShiNeiRong_url"));
String panDuanJieGuo = mCursor.getString(mCursor.getColumnIndex("panDuanJieGuo"));
String wanZhengCeshiNeiRong = mCursor.getString(mCursor.getColumnIndex("wanZhengCeshiNeiRong"));
String ceShiTime = mCursor.getString(mCursor.getColumnIndex("ceShiTime"));
YueJianRenWu yueJianRenWu = new YueJianRenWu(yjId, xiTongName, xiTongLeiXing, sheShiName, jiance, ceShiNeiRong, ceShiNeiRong_url, panDuanJieGuo, wanZhengCeshiNeiRong, ceShiTime);
treeSet.add(yueJianRenWu.getSheShiName());// 这个是又创建的
} while (mCursor.moveToNext());
return treeSet;
}
/**
* // 查询 xiTongName 系统名称
* https://blog.csdn.net/catoop/article/details/7589204
*/
public ArrayList<String> newselect_xiTongName(){
ArrayList<String> list= new ArrayList<String>();
// 这里要判断 空
sqldb = dbhelper.getReadableDatabase();
//创建游标
String sql = "select distinct xiTongName from "+TABLE_NAME;
Cursor mCursor = sqldb.rawQuery(sql, null);
//游标置顶
mCursor.moveToFirst();
//遍历
do {
/*
'}';
*/
String xiTongName = mCursor.getString(mCursor.getColumnIndex("xiTongName"));
list.add(xiTongName);
} while (mCursor.moveToNext());
return list;
}
/**
* // 查询 xiTongLeiXing 系统类型
* 根据xiTongName 系统名称 查询
* https://blog.csdn.net/catoop/article/details/7589204
*/
public ArrayList<String> newselect_xiTongLeiXing (String xiTongName){
ArrayList<String> list= new ArrayList<String>();
// 这里要判断 空
sqldb = dbhelper.getReadableDatabase();
//创建游标
String sql = "select distinct xiTongLeiXing from "+TABLE_NAME +" where xiTongName='"+xiTongName+"'";
Cursor mCursor = sqldb.rawQuery(sql, null);
//游标置顶
mCursor.moveToFirst();
//遍历
do {
/*
'}';
*/
String xiTongLeiXing = mCursor.getString(mCursor.getColumnIndex("xiTongLeiXing"));
list.add(xiTongLeiXing);
} while (mCursor.moveToNext());
return list;
}
/**
* // 查询 sheShiName 设施名称
* 根据xiTongName xiTongLeiXing 设备名称 系统名称 查询
* https://blog.csdn.net/catoop/article/details/7589204
*/
public ArrayList<String> newselect_sheShiName (String xiTongName,String xiTongLeiXing){
ArrayList<String> list= new ArrayList<String>();
// 这里要判断 空
sqldb = dbhelper.getReadableDatabase();
//创建游标
String sql = "select distinct sheShiName from "+TABLE_NAME +" where xiTongName='"+xiTongName+"' and xiTongLeiXing='"+xiTongLeiXing+"' ";
Cursor mCursor = sqldb.rawQuery(sql, null);
//游标置顶
mCursor.moveToFirst();
//遍历
do {
/*
'}';
*/
String sheShiName = mCursor.getString(mCursor.getColumnIndex("sheShiName"));
list.add(sheShiName);
} while (mCursor.moveToNext());
return list;
}
/**
* // 查询 jiance 设施名称
* 根据xiTongName xiTongLeiXing sheShiName 设备名称 系统类型 系统名称 查询
* https://blog.csdn.net/catoop/article/details/7589204
*/
public ArrayList<String> newselect_jiance (String xiTongName,String xiTongLeiXing,String sheShiName){
ArrayList<String> list= new ArrayList<String>();
// 这里要判断 空
sqldb = dbhelper.getReadableDatabase();
//创建游标
String sql = "select distinct jiance from "+TABLE_NAME +" where xiTongName='"+xiTongName+"' and xiTongLeiXing='"+xiTongLeiXing+"' and sheShiName='"+sheShiName+"' ";
Cursor mCursor = sqldb.rawQuery(sql, null);
//游标置顶
mCursor.moveToFirst();
//遍历
do {
/*
'}';
*/
String jiance = mCursor.getString(mCursor.getColumnIndex("jiance"));
list.add(jiance);
} while (mCursor.moveToNext());
return list;
}
/**
*
* https://blog.csdn.net/carson_ho/article/details/53241633
*/
private void del_biao(){
//https://blog.csdn.net/freya1010/article/details/79627582
sqldb = dbhelper.getReadableDatabase();
// sqldb = dbhelper.getWritableDatabase();
sqldb.delete(TABLE_NAME, null, null);
Message message= handler.obtainMessage();
message.what=1;
message.obj="删除完成了………………";
handler.sendMessage(message);
// 参数1:表名(String)
// 参数2:WHERE表达式(String),需删除数据的行; 若该参数为 null, 就会删除所有行;?号是占位符
// 参数3:WHERE选择语句的参数(String[]), 逐个替换 WHERE表达式中 的“?”占位符;
// 注:也可采用SQL语句修改
// String sql = "delete from user where id="1";
// db.execSQL(sql);
}
/**
* 添加 测试任务的 列表
* @param yjrw
* 分析:// public HashMap<String,TreeSet<String>> xitongNameMap=new HashMap<>();// 系统名称,系统类型
// public HashMap<String,TreeSet<String>> xitongLeiXingMap=new HashMap<>();// 系统类型,设施名称
// public HashMap<String,TreeSet<String>> sheBeiNameMap=new HashMap<>();// 设施名称,检测内容
yueJianRenWu.setXiTongName(td_1_1[0]);系统名称
yueJianRenWu.setXiTongLeiXing(td_1_1[1]);系统类型
yueJianRenWu.setSheShiName(td_1_1[2]);设施名称
*/
private void CeShiRenWuMethod(YueJianRenWu yjrw) {
String xiTongNam=yjrw.getXiTongName();//系统名称
String xitongLeiXin=yjrw.getXiTongLeiXing();//系统类型
String sheBeiName=yjrw.getSheShiName();//设施名称
// 2, 进行 xitongNameMap 系统名称,系统类型 key ,set
if(!xitongNameMap.containsKey(xiTongNam)){//不包含
TreeSet<String> treeSet =new TreeSet<>();
treeSet.add(xitongLeiXin);// 加入 系统类型
xitongNameMap.put(xiTongNam,treeSet);
}else{
TreeSet<String> treeSet =xitongNameMap.get(xiTongNam);//根据系统类型, 调出 set
treeSet.add(xitongLeiXin);// 再加入系统类型
xitongNameMap.put(xiTongNam,treeSet);// 用不用再 装进去了?????????????????、
}
}
public void xxycaiji(View view) {
/**
*
* 分析: 1 。把数据库里的都调出来,都传给 AllCeShiNeiRong
*/
Intent intent = new Intent();
intent.setClass(this, DuanDianErCaiJi.class); // 进入第二个 列表里
startActivity(intent);
}
}
item_grou.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp"
android:background="@drawable/new_customer"
android:textSize="15sp"
android:orientation="vertical">
<TextView
android:id="@+id/tv_group_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="25dp"
/>
<TextView
android:id="@+id/tv_leix"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="25dp"
/>
<LinearLayout
android:id="@+id/LinerLaout_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"></LinearLayout>
</LinearLayout>
</LinearLayout>