今天计网考试,然后做建民老师的测试,科技查询政策的安卓端,将相关代码发在下面
package com.example.keji import android.os.Bundle import android.util.Log import androidx.fragment.app.Fragment import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.Button import android.widget.EditText import android.widget.LinearLayout import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import com.example.keji.Service.PolicyService import com.example.keji.adapter.LeftAdapter import com.example.keji.pojo.Policy import com.example.network.ServiceCreator import retrofit2.Call import retrofit2.Callback import retrofit2.Response class LeftFragment : Fragment() { lateinit var searchInput:EditText lateinit var searchButton:Button lateinit var recyclerView:RecyclerView override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { return inflater.inflate(R.layout.fragment_left,container,false) } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) searchInput=view.findViewById(R.id.search_input) searchButton=view.findViewById(R.id.search_button) recyclerView=view.findViewById(R.id.recycler_view) val policyService=ServiceCreator.create<PolicyService>() searchButton.setOnClickListener { policyService.selectbyname(searchInput.text.toString()).enqueue(object :Callback<List<Policy>>{ override fun onResponse( call: Call<List<Policy>>, response: Response<List<Policy>> ) { val result=response.body() Log.e("TAG", "onResponse: ${result}", ) if (result != null) { Log.e("TAG", "onResponse: ${result.size}", ) } recyclerView.layoutManager=LinearLayoutManager(context) val dataList = result// 示例数据 recyclerView.adapter=LeftAdapter(dataList) } override fun onFailure(call: Call<List<Policy>>, t: Throwable) { Log.e("TAG", "onFailure: ${t.printStackTrace()}", ) } }) } } }
package com.example.keji import MyExpandableListAdapter import android.os.Bundle import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.ExpandableListView import android.widget.Toast import androidx.fragment.app.Fragment import com.example.keji.Service.PolicyService import com.example.keji.pojo.Policy import com.example.network.ServiceCreator import retrofit2.Call import retrofit2.Callback import retrofit2.Response import kotlin.math.log class RightFragment : Fragment() { lateinit var expandableListView:ExpandableListView override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { return inflater.inflate(R.layout.fragment_right, container, false) } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) expandableListView=view.findViewById(R.id.expandableListView) val policyService=ServiceCreator.create<PolicyService>() val firsts= listOf<String>("综合","科研机构改革","科技计划管理","科技经费与财务","基础研究","平台基地","产业","科技人才") // 创建一个空的可变 Map val data1: MutableMap<String, MutableList<String>> = mutableMapOf() for (first in firsts){ policyService.gettree(first).enqueue(object :Callback<List<Policy>>{ override fun onResponse( call: Call<List<Policy>>, response: Response<List<Policy>> ) { val result=response.body() val seconds =mutableListOf<String>() if (result != null) { for(res in result){ // Log.e("TAG", "name: ${res.name}", ) res.name?.let { seconds.add(it) } Log.e("TAG", "firsts: ${firsts}", ) Log.e("TAG", "seconds: ${seconds}", ) } } Log.e("TAG", "onResponse: ${seconds}", ) data1[first]?.addAll(seconds) Log.e("TAG", "data1: ${data1}", ) } override fun onFailure(call: Call<List<Policy>>, t: Throwable) { Log.e("TAG", "onFailure: ", ) } }) } val data = mapOf( "综合" to listOf("子项1.1", "子项1.2", "子项1.3"), "科研机构改革" to listOf("子项2.1", "子项2.2"), "科技计划管理" to listOf("子项3.1", "子项3.2", "子项3.3", "子项3.4"), "科技经费与财务" to listOf("子项3.1", "子项3.2", "子项3.3", "子项3.4"), "基础研究" to listOf("子项3.1", "子项3.2", "子项3.3", "子项3.4"), "平台基地" to listOf("子项3.1", "子项3.2", "子项3.3", "子项3.4"), "产业" to listOf("子项3.1", "子项3.2", "子项3.3", "子项3.4"), "科技人才" to listOf("子项3.1", "子项3.2", "子项3.3", "子项3.4"), ) val adapter = MyExpandableListAdapter(data) expandableListView.setAdapter(adapter) // 设置子项点击事件 expandableListView.setOnChildClickListener { parent, v, groupPosition, childPosition, id -> val groupName = adapter.getGroup(groupPosition) val childName = adapter.getChild(groupPosition, childPosition) Toast.makeText(context, "点击了子项:$groupName - $childName", Toast.LENGTH_SHORT).show() true } } }