2024/05/28
今日编程时长:4小时
代码行数:613行
发表博客数量:1篇
今天完成了专业主任王老师的极限测试的第一阶段:在手机上实现远程查询mysql数据,数据库的内容都是有老师提供,要求能实现标题名称的模糊匹配。
今天的任务中使用到了两个fragment,这可以这样就可以通过下方的导航栏来实现页面的切换,方便第二阶段的编写。
其次就是安卓中listview的使用,根据要求的不同通过创建每一行的视图来使用适配器进行绑定,这样也方便设置行点击事件。
//主要的功能代码 package com.example.limit_text.fragment; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.Button; import android.widget.EditText; import android.widget.ListView; import androidx.fragment.app.Fragment; import com.example.limit_text.Adapter.NameAdpter; import com.example.limit_text.Pojo.PoilcyPojo; import com.example.limit_text.PolicyView; import com.example.limit_text.R; import com.example.limit_text.util.JDBCUtils; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; public class FragmentPolicy extends Fragment { private EditText key1; private Button query; private ListView query_result; private List<PoilcyPojo> list; private NameAdpter nameAdpter; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Log.e("已启动",""); select(); View policy =LayoutInflater.from(getActivity()).inflate(R.layout.activity_policy,container,false); key1=policy.findViewById(R.id.key1); query=policy.findViewById(R.id.query); query_result=policy.findViewById(R.id.query_result); query.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { select(); } }); query_result.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Intent intent=new Intent(getActivity(), PolicyView.class); intent.putExtra("id",list.get(position).getId()); startActivity(intent); } }); return policy; } private void select() { list=new ArrayList<>(); new Thread(new Runnable() { @Override public void run() { Connection coon=null; coon= JDBCUtils.getConn(); String sql; if(!key1.getText().toString().isEmpty()) { sql = "select id,name from policy where name like '%"+key1.getText().toString()+"%'"; } else{ sql="select id,name from policy"; } try { PreparedStatement ps=coon.prepareStatement(sql); ResultSet resultSet=ps.executeQuery(); while (resultSet.next()) { PoilcyPojo policy_item=new PoilcyPojo(); policy_item.setId(resultSet.getInt(1)); policy_item.setName(resultSet.getString(2)); list.add(policy_item); } getActivity().runOnUiThread(new Runnable() { @Override public void run() { nameAdpter=new NameAdpter(list,getActivity()); query_result.setAdapter(nameAdpter); } }); } catch (SQLException e) { throw new RuntimeException(e); } } }).start(); } }
//适配器 package com.example.limit_text.Adapter; import android.annotation.SuppressLint; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; import com.example.limit_text.Pojo.PoilcyPojo; import com.example.limit_text.R; import java.util.List; public class NameAdpter extends BaseAdapter { private List<PoilcyPojo> list; private Context context; public NameAdpter(List<PoilcyPojo> list, Context context) { this.list = list; this.context = context; } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return list.get(position); } @Override public long getItemId(int position) { return (long)list.get(position).getId(); } @SuppressLint("ViewHolder") @Override public View getView(int position, View convertView, ViewGroup parent) { convertView = LayoutInflater.from(context).inflate(R.layout.name_view,null); PoilcyPojo poilcyPojo=list.get(position); TextView name=convertView.findViewById(R.id.name); name.setText(poilcyPojo.getName()); return convertView; } }