五月二十九日

今天就完成老师上课要求的在手机端实现查询显示以及分类。

 

package com.example.myapplication4.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.myapplication4.R;
import com.example.myapplication4.bean.Policy;
import com.example.myapplication4.dao.Dao;

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

public class MainActivity extends AppCompatActivity {
LinearLayout line;
Dao dao = new Dao();

Button country,hebei,other;


List<Policy> policyList = new ArrayList<>();
    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        line = findViewById(R.id.line);
        country = findViewById(R.id.country);
        hebei = findViewById(R.id.hebei);
        other  = findViewById(R.id.other);

        inital();
//        dianji();

    }

    private void inital()
    {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                Policy policy = new Policy();
                policyList =  dao.selectAll();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                            for(Policy policy : policyList)
                            {
                                TextView textView = new TextView(MainActivity.this);
                                textView.setText(policy.getName()); // 设置文本内容
                                textView.setTextSize(20);
                                textView.setLayoutParams(new LinearLayout.LayoutParams(
                                        LinearLayout.LayoutParams.MATCH_PARENT,
                                        LinearLayout.LayoutParams.WRAP_CONTENT));
                                textView.setPadding(16, 8, 16, 8); // 设置内边距
                                textView.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        // 处理点击事件,比如显示更多信息或执行其他操作
                                        Toast.makeText(MainActivity.this,  policy.getText(), Toast.LENGTH_SHORT).show();
                                    }
                                });
                                line.addView(textView);
                            }
                        }



                });
            }
        });thread.start();
    }



    private void dianji()
    {
        country.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Thread thread = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Policy policy = new Policy();
                        policyList =  dao.selectAlld("全国");
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if(policyList !=null)
                                {
                                    for(Policy policy : policyList)
                                    {

                                    }
                                }


                            }
                        });
                    }
                });thread.start();
            }
        });
    }
}

  

 刚刚才完成在页面显示以及点击事件。

posted @ 2024-05-29 00:48  *太¥^白%  阅读(5)  评论(0编辑  收藏  举报