5.16
今天的任务中使用到了两个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; } }