菜鸟的博客

纵有疾风起,人生不言弃。

导航

科技政策查询系统--MainActivity

 

所花时间(包括上课):  5  h左右
代码量(行):  1000     左右
搏客量(篇): 1
了解到的知识点:  springboot+android的简单开发
备注(其他):  
package com.example.policyquery;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.policyquery.api.ApiService;
import com.example.policyquery.api.RetrofitClient;
import com.example.policyquery.DataModel.Policy;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MainActivity extends AppCompatActivity {

    private LinearLayout llOverview;
    private LinearLayout llCategory;
    private ListView policyListView;
    private AutoCompleteTextView searchInput;
    private TextView resultTextView;
    private ApiService apiService;
    private ArrayAdapter<String> policyAdapter;
    private List<Policy> policyList;
    private List<String> policyNames;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 初始化视图
        llOverview = findViewById(R.id.LL1);
        llCategory = findViewById(R.id.LL2);
        policyListView = findViewById(R.id.policyListView);
        searchInput = findViewById(R.id.searchInput);
        resultTextView = findViewById(R.id.resultTextView);

        Button overviewButton = findViewById(R.id.overviewButton);
        Button categoryButton = findViewById(R.id.categoryButton);
        Button searchButton = findViewById(R.id.searchButton);

        // 初始化ApiService
        apiService = RetrofitClient.getRetrofitInstance().create(ApiService.class);

        // 初始化政策列表和适配器
        policyList = new ArrayList<>();
        policyNames = new ArrayList<>();
        policyAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, policyNames);
        policyListView.setAdapter(policyAdapter);

        // 在程序启动时调用getAllPolicies
        getAllPolicies();

        // 设置按钮点击事件监听器
        overviewButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 显示政策概览界面,隐藏政策分类界面
                llOverview.setVisibility(View.VISIBLE);
                llCategory.setVisibility(View.GONE);
            }
        });

        categoryButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 显示政策分类界面,隐藏政策概览界面
                llOverview.setVisibility(View.GONE);
                llCategory.setVisibility(View.VISIBLE);
                // 可以在这里添加获取政策分类的方法
            }
        });

        searchButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 执行查询操作
                performSearch();
            }
        });

        // 设置政策名称点击事件监听器
        policyListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // 跳转到详情页
                Intent intent = new Intent(MainActivity.this, PolicyDetailActivity.class);
                Policy selectedPolicy = policyList.get(position);
                intent.putExtra("policyId", selectedPolicy.getId());
                intent.putExtra("policyName", selectedPolicy.getName());
                intent.putExtra("policyType", selectedPolicy.getType());
                intent.putExtra("policyCategory", selectedPolicy.getCategory());
                intent.putExtra("policyRange", selectedPolicy.getRange());
                intent.putExtra("policyDocument", selectedPolicy.getDocument());
                intent.putExtra("policyForm", selectedPolicy.getForm());
                intent.putExtra("policyOrgan", selectedPolicy.getOrgan());
                intent.putExtra("policyText", selectedPolicy.getText());
                startActivity(intent);
            }
        });
    }

    private void getAllPolicies() {
        apiService.getAllPolicies().enqueue(new Callback<List<Policy>>() {
            @Override
            public void onResponse(Call<List<Policy>> call, Response<List<Policy>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    List<Policy> policies = response.body();
                    // 将政策添加到列表中并更新适配器
                    policyList.clear();
                    policyNames.clear();
                    policyList.addAll(policies);
                    for (Policy policy : policies) {
                        policyNames.add(policy.getName());
                    }
                    policyAdapter.notifyDataSetChanged();
                } else {
                    Log.e("MainActivity", "获取政策数据失败");
                }
            }

            @Override
            public void onFailure(Call<List<Policy>> call, Throwable t) {
                Log.e("MainActivity", "网络请求失败", t);
            }
        });
    }

    private void performSearch() {
        String query = searchInput.getText().toString();

        apiService.queryPolicy(query).enqueue(new Callback<List<Policy>>() {
            @Override
            public void onResponse(Call<List<Policy>> call, Response<List<Policy>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    List<Policy> policies = response.body();
                    // 显示查询结果并更新政策列表
                    policyList.clear();
                    policyNames.clear();
                    policyList.addAll(policies);
                    for (Policy policy : policies) {
                        policyNames.add(policy.getName());
                    }
                    policyAdapter.notifyDataSetChanged();
                } else {
                    Log.e("MainActivity", "查询政策数据失败");
                }
            }

            @Override
            public void onFailure(Call<List<Policy>> call, Throwable t) {
                Log.e("MainActivity", "网络请求失败", t);
            }
        });
    }
}

 

posted on 2024-05-28 22:19  hhmzd233  阅读(6)  评论(0编辑  收藏  举报