2024.5.28(河北省科技政策查询系统)
代码行数:524
博客量:1
今天上课测试,安卓端的一个河北省科技政策查询系统,代码如下:
package com.example.poliocy.fragment; import android.annotation.SuppressLint; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; import android.text.Html; import android.text.Spanned; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ListView; import android.widget.PopupWindow; import android.widget.TextView; import androidx.annotation.NonNull; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentTransaction; import androidx.lifecycle.viewmodel.CreationExtras; import com.example.poliocy.R; import com.example.poliocy.look; import com.example.poliocy.util.DBOpenHelper; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; public class Fragment_function1 extends Fragment { ArrayList<String> policy_name=new ArrayList<>(); private TextView keyword; private Button find,allcountry,hebei,outside; ListView listview; DBOpenHelper db=new DBOpenHelper(); public static final String TID="com.example.myapplication_002.MESSAGE"; @SuppressLint("MissingInflatedId") public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){ View function1= LayoutInflater.from(getActivity()).inflate(R.layout.activity_function1, container, false); keyword=function1.findViewById(R.id.keyword); find=function1.findViewById(R.id.find); allcountry=function1.findViewById(R.id.allcountry); hebei=function1.findViewById(R.id.hebei); outside=function1.findViewById(R.id.outside); listview=(ListView) function1.findViewById(R.id.list_view); Log.e("1","12uihuhnjnono"); new Thread(new Runnable() { @Override public void run() { try { Statement statement=db.getConn().createStatement(); String sql="select * from policy"; ResultSet res=statement.executeQuery(sql); while(res.next()){ policy_name.add(res.getString("name")); } } catch (SQLException e) { throw new RuntimeException(e); } getActivity().runOnUiThread(new Runnable() { @Override public void run() { Log.e("14564","12uihuhnjnono"); String[] name=new String[policy_name.size()]; for(int i=0;i<policy_name.size();i++){ name[i]=policy_name.get(i); } ArrayAdapter<String> adapter=new ArrayAdapter<>(function1.getContext(), android.R.layout.simple_list_item_1,name); listview.setAdapter(adapter); } }); } }).start(); find.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String key=keyword.getText().toString(); new Thread(new Runnable() { @Override public void run() { try { policy_name.clear(); Statement statement=db.getConn().createStatement(); String sql="select * from policy where name like '%"+key+"%'"; ResultSet res=statement.executeQuery(sql); while(res.next()){ policy_name.add(res.getString("name")); } } catch (SQLException e) { throw new RuntimeException(e); } getActivity().runOnUiThread(new Runnable() { @Override public void run() { Log.e("14564","12uihuhnjnono"); String[] name=new String[policy_name.size()]; for(int i=0;i<policy_name.size();i++){ name[i]=policy_name.get(i); } ArrayAdapter<String> adapter=new ArrayAdapter<>(function1.getContext(), android.R.layout.simple_list_item_1,name); listview.setAdapter(adapter); } }); } }).start(); } }); allcountry.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new Thread(new Runnable() { @Override public void run() { try { policy_name.clear(); Statement statement=db.getConn().createStatement(); String sql="select * from policy where policy.range like '全国%'"; ResultSet res=statement.executeQuery(sql); while(res.next()){ policy_name.add(res.getString("name")); } } catch (SQLException e) { throw new RuntimeException(e); } getActivity().runOnUiThread(new Runnable() { @Override public void run() { Log.e("14564","12uihuhnjnono"); String[] name=new String[policy_name.size()]; for(int i=0;i<policy_name.size();i++){ name[i]=policy_name.get(i); } ArrayAdapter<String> adapter=new ArrayAdapter<>(function1.getContext(), android.R.layout.simple_list_item_1,name); listview.setAdapter(adapter); } }); } }).start(); } }); hebei.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new Thread(new Runnable() { @Override public void run() { try { policy_name.clear(); Statement statement=db.getConn().createStatement(); String sql="select * from policy where policy.range like '河北%'"; ResultSet res=statement.executeQuery(sql); while(res.next()){ policy_name.add(res.getString("name")); } } catch (SQLException e) { throw new RuntimeException(e); } getActivity().runOnUiThread(new Runnable() { @Override public void run() { Log.e("14564","12uihuhnjnono"); String[] name=new String[policy_name.size()]; for(int i=0;i<policy_name.size();i++){ name[i]=policy_name.get(i); } ArrayAdapter<String> adapter=new ArrayAdapter<>(function1.getContext(), android.R.layout.simple_list_item_1,name); listview.setAdapter(adapter); } }); } }).start(); } }); outside.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String key=keyword.getText().toString(); new Thread(new Runnable() { @Override public void run() { try { policy_name.clear(); Statement statement=db.getConn().createStatement(); String sql="select * from policy where policy.range not like '河北%'"; ResultSet res=statement.executeQuery(sql); while(res.next()){ policy_name.add(res.getString("name")); } } catch (SQLException e) { throw new RuntimeException(e); } getActivity().runOnUiThread(new Runnable() { @Override public void run() { Log.e("14564","12uihuhnjnono"); String[] name=new String[policy_name.size()]; for(int i=0;i<policy_name.size();i++){ name[i]=policy_name.get(i); } ArrayAdapter<String> adapter=new ArrayAdapter<>(function1.getContext(), android.R.layout.simple_list_item_1,name); listview.setAdapter(adapter); } }); } }).start(); } }); listview.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { String result=((TextView)view).getText().toString(); final String[] tt={""}; new Thread(new Runnable() { @Override public void run() { try { policy_name.clear(); Statement statement=db.getConn().createStatement(); String sql="select * from policy where name = '"+result+"'"; ResultSet res=statement.executeQuery(sql); while(res.next()){ tt[0]=res.getString("text"); } } catch (SQLException e) { throw new RuntimeException(e); } getActivity().runOnUiThread(new Runnable() { @Override public void run() { Spanned spannedText = Html.fromHtml(tt[0]); String plainText = spannedText.toString(); showBlankDialog(plainText); } }); } }).start(); } }); return function1; } private void showBlankDialog(String temp){ AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); builder.setTitle("文本内容") .setMessage(temp) .setPositiveButton("关闭", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }) .show(); } }
package com.example.poliocy.fragment; import android.annotation.SuppressLint; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ListView; import android.widget.TextView; import androidx.annotation.NonNull; import androidx.fragment.app.Fragment; import androidx.lifecycle.viewmodel.CreationExtras; import com.example.poliocy.R; import com.example.poliocy.util.DBOpenHelper; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.Objects; public class Fragment_function2 extends Fragment { ArrayList<String> policy_name=new ArrayList<>(); private TextView keyword; private Button find; ListView listview; DBOpenHelper db=new DBOpenHelper(); @SuppressLint( "MissingInflatedId") public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){ View function2= LayoutInflater.from(getActivity()).inflate(R.layout.activity_function1, container, false); keyword=function2.findViewById(R.id.keyword); find=function2.findViewById(R.id.find); return function2; } }
package com.example.poliocy.util; import android.util.Log; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class DBOpenHelper { private static String TAG ="DbOpenHelper"; private static String ip = "10.99.114.68"; private static int port = 3306; private static String dbName = "test"; private static String url = "jdbc:mysql://" + ip + ":" + port + "/" + dbName+"?useUnicode=true&characterEncoding=utf-8&useSSL=false"; // 构建连接mysql的字符串 private static String user = "root"; private static String password = "123456"; public static Connection getConn(){ try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { throw new RuntimeException(e); } Connection conn=null; // 2.连接JDBC try { conn = DriverManager.getConnection(url, user, password); Log.e(TAG,"连接成功"); } catch (SQLException e) { Log.e(TAG,"连接失败"); throw new RuntimeException(e); } return conn; } // //数据库查询函数 ////前面加载完JDBC驱动了,现在开始使用JDBC, // //↓ List里只能放Map类型的对象,而这个Map类型的对象又只能放以String类型为键,以Object类型为值的键值对 // //就是说你定义了一个表list,然后表里得每一行数据都是HashMap,然后HashMap<标识符,你要保存的数据> // public static List<HashMap<String,Object>> getinfo(String dbname) throws SQLException { //// 定义List<HashMap<String,Object>>类型的数据并实例化 // List<HashMap<String,Object>> list=new ArrayList<HashMap<String, Object>>(); //// 调用连接函数,传入数据库名的形参,获得conn对象,因为getConnection的返回类型就是Connection及conn // //1.调用连接,不然你连不上数据库 // Connection conn=getConn(); // //// 通过connection对象conn获取statement对象sta,createStatement():创建基本的statement对象 // //2.你要用statement去执行sql语句,但你得连上数据库才能执行,所以要用连接对象创建statement对象。 // Statement sta=conn.createStatement(); // //// 定义sql语句 // String sql="select * from studyuser "; // //// 调用Statement对象执行sql语句,返回结果为结果集,就是一个带指针的表格,通过操作指针来进行数据操作 // //3.使用statement的三种方法中的executeQuery()方法,返回的就是一个结果集ResultSet对象,所以你要用ResultSet变量接收 // ResultSet result=sta.executeQuery(sql); // //// 判断一下是否为空 // if (result==null){ // return null; // } // //// 当这个结果集(就这个表)还有下一行时进入函数。这个表是有指针的,百度搜索ResultSet接口中的方法, // while (result.next()) { //// 每次循环都会新实例化一个HashMap对象,用于将遍历到的数据填进去 // HashMap<String,Object> map=new HashMap<>(); //// 往map中填数据,map的数据类型相当于键值对 // map.put("id",result.getString("id"));//ResultSet中的getString方法,获取指定字段的String类型值 // map.put("name",result.getString("name")); // map.put("phone",result.getString("phone")); // map.put("banji",result.getString("banji")); //// 每次循环完就添加到list中 // list.add(map); // } //// 把list返回出去,否则拿不到这个list // return list; // } private static String t_name=null; public static void update(){ } //查看数据 public static String select(String dbname,String s1){ // final String[] t_name={null}; // new Thread(new Runnable() { // @Override // public void run() { String t_name=null; // // 1.加载JDBC驱动 // try { // Class.forName("com.mysql.jdbc.Driver"); // } catch (ClassNotFoundException e) { // throw new RuntimeException(e); // } if(dbname.equals("studyuser")) { // 2.连接JDBC try { // Connection conn = DriverManager.getConnection(url, user, password); try { Connection conn = getConn(); Log.e(TAG, "连接成功"); } catch (Exception e) { Log.e(TAG, "连接失败"); throw new RuntimeException(e); } Statement statement = getConn().createStatement(); String sql = "select * from studyuser where id = '" + s1 + "'"; ResultSet res = statement.executeQuery(sql); if (res.next()) { t_name = res.getString("name"); } } catch (SQLException e) { throw new RuntimeException(e); } }else if(dbname.equals("everyday")){ try { // Connection conn = DriverManager.getConnection(url, user, password); try { Connection conn = getConn(); Log.e(TAG, "连接成功"); } catch (Exception e) { Log.e(TAG, "连接失败"); throw new RuntimeException(e); } String time="",df=""; Statement statement = getConn().createStatement(); String sql = "select * from everyday where id = '" + s1 + "'"; ResultSet res = statement.executeQuery(sql); if (res.next()) { time = res.getString("day"); df=res.getString("dayfinish"); } } catch (SQLException e) { throw new RuntimeException(e); } } // } // }).start(); // if(t_name[0]==null){ // Log.e("afdafe","12323243"); // } return t_name; } //插入数据 public static void insert(String dbname,String s1, String s2,String s3,String s4,String s5) { if (dbname.equals("studyuser")) { try { // Connection conn = DriverManager.getConnection(url, user, password); String sql = "insert into studyuser(id,name,phone,banji,password) values(?,?,?,?,?)"; PreparedStatement ps = null; ps = getConn().prepareStatement(sql); // 为两个 ? 设置具体的值 ps.setString(1, s1); ps.setString(2, s2); ps.setString(3, s3); ps.setString(4, s4); ps.setString(5, s5); // 执行语句 ps.executeUpdate(); } catch (SQLException e) { throw new RuntimeException(e); } }else if(dbname.equals("everyday")){ try { // Connection conn = DriverManager.getConnection(url, user, password); String sql = "insert into everyday(id,opentime,closetime,dayfinish,day) values(?,?,?,?,?)"; PreparedStatement ps = null; ps = getConn().prepareStatement(sql); // 为两个 ? 设置具体的值 ps.setString(1, s1); ps.setString(2, s2); ps.setString(3, s3); ps.setString(4, s4); ps.setString(5, s5); // 执行语句 ps.executeUpdate(); } catch (SQLException e) { throw new RuntimeException(e); } }else if(dbname.equals("everyweekaim")){ Log.e("tag","insert0"); try { // Connection conn = DriverManager.getConnection(url, user, password); String sql = "insert into everyweekaim(id,settime,aim) values(?,?,?)"; PreparedStatement ps = null; ps = getConn().prepareStatement(sql); Log.e("tag","insert1"); // 为两个 ? 设置具体的值 ps.setString(1, s1); ps.setString(2, s2); ps.setString(3, s3); // 执行语句 ps.executeUpdate(); } catch (SQLException e) { throw new RuntimeException(e); } } } }
package com.example.poliocy; import androidx.appcompat.app.AppCompatActivity; import android.annotation.SuppressLint; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.widget.ArrayAdapter; import android.widget.EditText; import android.widget.TextView; import com.example.poliocy.fragment.Fragment_function1; import com.example.poliocy.util.DBOpenHelper; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class look extends AppCompatActivity { EditText textl; DBOpenHelper db=new DBOpenHelper(); String ttext; public static final String TID="com.example.myapplication_002.MESSAGE"; @SuppressLint("MissingInflatedId") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_look); textl=findViewById(R.id.textlook); //保存接收到的字符 Intent intent=getIntent(); String resname=intent.getStringExtra(Fragment_function1.TID); Log.e("look",resname); new Thread(new Runnable() { @Override public void run() { try { Statement statement=db.getConn().createStatement(); String sql="select * from policy where name = '"+resname+"'"; ResultSet res=statement.executeQuery(sql); while(res.next()){ ttext=res.getString("text"); } } catch (SQLException e) { throw new RuntimeException(e); } look.this.runOnUiThread(new Runnable() { @Override public void run() { textl.setText(ttext); } }); } }).start(); } }
package com.example.poliocy; import androidx.appcompat.app.AppCompatActivity; import androidx.fragment.app.FragmentTransaction; import android.os.Bundle; import android.util.Log; import com.example.poliocy.fragment.Fragment_function1; import com.example.poliocy.fragment.Fragment_function2; import com.google.android.material.bottomnavigation.BottomNavigationView; public class MainActivity extends AppCompatActivity { private Fragment_function1 function1; private Fragment_function2 function2; BottomNavigationView bottomNavigationView = null; FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();//切换页面的核心 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initFragment(); } void initFragment() { //初始化2个页面 function1 = new Fragment_function1(); function2 = new Fragment_function2(); transaction.add(R.id.Main_fragment, function1); transaction.add(R.id.Main_fragment, function2); hideAllFragment(transaction);//隐藏全部界面 //默认显示第一个选项卡 transaction.show(function1); transaction.commit();//show()后一定要commit() //布局中的底部bottom_navigation bottomNavigationView = findViewById(R.id.bottom_navigation); //设置底部三个选项卡按钮进行切换的事件 bottomNavigationView.setOnNavigationItemSelectedListener(item -> { transaction = getSupportFragmentManager().beginTransaction(); hideAllFragment(transaction);//先隐藏全部界面 再根据按钮显示 if(item.getItemId()==R.id.function1) { transaction.show(function1); transaction.commit(); return true; } if(item.getItemId()==R.id.function2) { transaction.show(function2); transaction.commit(); return true; } return false; }); } void hideAllFragment(FragmentTransaction transaction) { if (function1 != null) transaction.hide(function1); if (function2 != null) transaction.hide(function2); } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="0dp" android:gravity="left" android:text="政策全文" android:textStyle="bold" android:textSize="50dp" > </TextView> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <EditText android:id="@+id/keyword" android:layout_width="250dp" android:layout_height="60dp" android:layout_centerInParent="true" android:hint="请输入关键字" android:inputType="text" android:layout_marginRight="20dp" android:maxLines="1" android:textColorHint="#6A050505" android:textSize="30dp"> </EditText> <Button android:id="@+id/find" android:layout_width="wrap_content" android:layout_height="wrap_content" android:backgroundTint="#03A9F4" android:text="查询" android:textSize="20dp"> </Button> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/allcountry" android:layout_width="wrap_content" android:layout_height="wrap_content" android:backgroundTint="#03A9F4" android:text="全国" android:textSize="20dp"> </Button> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=" "> </TextView> <Button android:id="@+id/hebei" android:layout_width="wrap_content" android:layout_height="wrap_content" android:backgroundTint="#03A9F4" android:text="河北" android:textSize="20dp"> </Button> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=" "> </TextView> <Button android:id="@+id/outside" android:layout_width="wrap_content" android:layout_height="wrap_content" android:backgroundTint="#03A9F4" android:text="外省" android:textSize="20dp"> </Button> </LinearLayout> <ListView android:id="@+id/list_view" android:layout_width="match_parent" android:layout_height="wrap_content"> </ListView> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="0dp" android:gravity="left" android:text="政策要点" android:textStyle="bold" android:textSize="50dp" > </TextView> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <EditText android:id="@+id/keyword" android:layout_width="250dp" android:layout_height="60dp" android:layout_centerInParent="true" android:hint="请输入关键字" android:inputType="text" android:layout_marginRight="20dp" android:maxLines="1" android:textColorHint="#6A050505" android:textSize="30dp"> </EditText> <Button android:id="@+id/find" android:layout_width="wrap_content" android:layout_height="wrap_content" android:backgroundTint="#03A9F4" android:text="查询" android:textSize="20dp"> </Button> </LinearLayout> <ListView android:id="@+id/list_view2" android:layout_width="match_parent" android:layout_height="wrap_content"> </ListView> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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="match_parent" tools:context=".look"> <ListView android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <EditText android:id="@+id/textlook" android:layout_width="match_parent" android:layout_height="match_parent" android:textSize="50dp"> </EditText> </ListView> </androidx.constraintlayout.widget.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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="match_parent" tools:context=".MainActivity"> <FrameLayout android:id="@+id/Main_fragment" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginBottom="60dp"> </FrameLayout> <LinearLayout android:id="@+id/linearLayout2" android:layout_width="match_parent" android:layout_height="60dp" android:padding="0dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent"> <com.google.android.material.bottomnavigation.BottomNavigationView android:id="@+id/bottom_navigation" android:layout_width="match_parent" android:layout_height="wrap_content" app:labelVisibilityMode="labeled" app:menu="@menu/bottom" style="@style/BottomNavigationTextStyle"/> </LinearLayout> </androidx.constraintlayout.widget.ConstraintLayout>
 
                    
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号