6.16

少爷放过我吧

今天实现科技政策查询的前端,我前端设计的页面包括输入框,全国,河北省,外省的按钮,这样方便有代表性的查询。

package com.zhen;

import android.util.Log;
import android.view.View;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.google.gson.reflect.TypeToken;
import com.zhen.adapter.PolicyListAdapter;
import com.zhen.net.OkHttpUtils;
import com.zhen.pojp.IP;
import com.zhen.pojp.Policy;
import com.google.gson.Gson;

import java.util.List;


public class activity_text extends AppCompatActivity {
    private EditText searchBox;
    private ImageButton searchBtn;
    private Button nationBtn, hebeiBtn, waishengBtn;
    private ListView mainLv;
    private PolicyListAdapter policyListAdapter;
    private IP ip;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_text);
        ip = new IP();
        ip.setIp();
        initView();
        initEvent();
    }

    private void initEvent() {
        showMain();
        searchBu();
        nationBu();
        hebeiBu();
        waishengBu();
    }

    private void waishengBu() {
        waishengBtn.setOnClickListener(view -> OkHttpUtils.getInstance().doGet("http://" + ip.getIp() + ":8080/policy/getWaisheng", new CallBack() {
            @Override
            public void onSuccess(String result) {
                show1(result);
            }

            @Override
            public void onError(Exception e) {
                Toast.makeText(activity_text.this, "查询不到结果!", Toast.LENGTH_SHORT).show();
            }
        }));

    }

    private void hebeiBu() {
        hebeiBtn.setOnClickListener(view -> OkHttpUtils.getInstance().doGet("http://" + ip.getIp() + ":8080/policy/getHebei", new CallBack() {
            @Override
            public void onSuccess(String result) {
                show1(result);
            }

            @Override
            public void onError(Exception e) {
                Toast.makeText(activity_text.this, "查询不到结果!", Toast.LENGTH_SHORT).show();
            }
        }));
    }

    private void nationBu() {
        nationBtn.setOnClickListener(view -> OkHttpUtils.getInstance().doGet("http://" + ip.getIp() + ":8080/policy/getNation", new CallBack() {
            @Override
            public void onSuccess(String result) {
                show1(result);
            }

            @Override
            public void onError(Exception e) {
                Toast.makeText(activity_text.this, "查询不到结果!", Toast.LENGTH_SHORT).show();
            }
        }));
    }

    private void searchBu() {
        searchBtn.setOnClickListener(view -> {
            String searchText = searchBox.getText().toString();
            if (searchText.equals("")) {
                Toast.makeText(activity_text.this, "请输入搜索内容", Toast.LENGTH_SHORT).show();
            } else {
                OkHttpUtils.getInstance().doGet("http://" + ip.getIp() + ":8080/policy/getByName?name=" + searchText, new CallBack() {
                    @Override
                    public void onSuccess(String result) {
                        show1(result);
                    }

                    @Override
                    public void onError(Exception e) {
                        Toast.makeText(activity_text.this, "查询不到结果!", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
    }

    private void showMain() {
        OkHttpUtils.getInstance().doGet("http://" + ip.getIp() + ":8080/policy/getAll", new CallBack() {
            @Override
            public void onSuccess(String result) {
                show1(result);
            }

            @Override
            public void onError(Exception e) {
                Toast.makeText(activity_text.this, "查询不到结果!", Toast.LENGTH_SHORT).show();
            }
        });
    }

    public void show1(String result) {
        Gson gson = new Gson();
        List<Policy> policyList = gson.fromJson(result, new TypeToken<List<Policy>>() {
        }.getType());
        policyListAdapter = new PolicyListAdapter(activity_text.this, R.layout.item_policy, policyList);
        mainLv.setAdapter(policyListAdapter);
    }

    private void initView() {
        searchBox = findViewById(R.id.search_box);
        searchBtn = findViewById(R.id.searchBtn);
        nationBtn = findViewById(R.id.nationBtn);
        hebeiBtn = findViewById(R.id.hebeiBtn);
        waishengBtn = findViewById(R.id.waishengBtn);
        mainLv = findViewById(R.id.main_lv);
    }
}

 

posted @ 2024-06-17 23:08  七安。  阅读(5)  评论(0编辑  收藏  举报