团队冲刺(三)

一、认领任务

经过团队讨论,我认领到的工作是:对昨天的食物展示增加一个查询功能(这里用了模糊查询)

二、任务完成时间估算

页面布局 2h
从数据库提取 4h

 

 

查询功能的实现:

展示如下:

主要的实现代码:

SActivity

package com.example.myapplication5;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.util.Base64;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

public class SActivity extends AppCompatActivity {
int l=0;
ListView nameList;
ListView caloryList;
private String[] childName = new String[0];
private String[] childReliang = new String[0];

@RequiresApi(api = Build.VERSION_CODES.O)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search);
nameList = findViewById(R.id.nameList);
caloryList = findViewById(R.id.caloryList);

Intent intent = getIntent();
String name = intent.getStringExtra("key1");
String name1 = null ;
try {
name1 = URLEncoder.encode(name,"GBK");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
// String name1 = JSON.toJSONString(name);
// String name1 = java.util.Base64.getUrlEncoder().encodeToString(name.getBytes());
// String name1= Base64.encodeToString(name.getBytes(),Base64.DEFAULT);
String password ="wu";
String password1 = null;
try {
password1 = URLEncoder.encode(password,"GBK");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
// String password1= java.util.Base64.getUrlEncoder().encodeToString(password.getBytes());
// String password1 = JSON.toJSONString(password);
System.out.println(name);
String path="http://10.0.2.2:8080/testhttp/Search.Servlet";//这里是本地web的地址,先运行httptest的Servlet再启动,才不会闪退。而http://120.79.40.20/testhttp/

//调用postTask,把获取到的数据与路径放入方法中,这里没有放数据。
new postTask().execute(name1,password1,path);;

}
class postTask extends AsyncTask {
@Override
protected Object doInBackground(Object[] params) {
//依次获取用户名,密码与路径
String name=params[0].toString();
String password=params[1].toString();
String path=params[2].toString();

try {
//获取网络上get方式提交的整个路径
URL url=new URL(path);
//打开网络连接
HttpURLConnection conn= (HttpURLConnection) url.openConnection();

//设置提交方式
conn.setRequestMethod("POST");
//设置网络超时时间
conn.setConnectTimeout(5000);
//界面上所有的参数名加上他的值
String s="name="+name+"&password="+password;
//获取请求头
conn.setRequestProperty("Content-Length",s.length()+"");//键是固定的
conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded; charset=utf-8");//键和值是固定的
//设置允许对外输出数据
conn.setDoOutput(true);
//把界面上的所有数据写出去
OutputStream os=conn.getOutputStream();
os.write(s.getBytes());
if(conn.getResponseCode()==200){
//用io流与web后台进行数据交互
InputStream is=conn.getInputStream();
//字节流转字符流
BufferedReader br=new BufferedReader(new InputStreamReader(is));
//读出每一行的数据
String str=br.readLine();
//返回读出的每一行的数据
return str;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

@Override
protected void onPostExecute(Object o) {
super.onPostExecute(o);
//获取Android studio与web后台数据交互获得的值
String s= (String) o;
if(s ==null||s.length()<=0){
Toast.makeText(SActivity.this, "找不到此类食物", Toast.LENGTH_SHORT).show();
}
else{

List<Shiwu> shopInfos = JSON.parseArray(s, Shiwu.class);
l=shopInfos.size();
// if(shopInfos.size()>0){
// l=shopInfos.size();
int i=0;
List<String> list11 = new ArrayList<>();
List<String> list22 = new ArrayList<>();
for (Shiwu shiwu : shopInfos) {


System.out.println(shiwu.getClasses());
System.out.println(shiwu.getName());
System.out.println(shiwu.getCalory());

list11.add(new String(shiwu.getName()));
list22.add(new String(shiwu.getCalory()));

//
}
childName=list11.toArray(new String[list11.size()]);
childReliang=list22.toArray(new String[list22.size()]);

// Toast.makeText(SActivity.this, s, Toast.LENGTH_SHORT).show();
dosome();

}

}

}
public void dosome(){
//传递参数
nameList.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, childName));
caloryList.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, childReliang));
setListViewOnTouchAndScrollListener(nameList,caloryList);
}
public void setListViewOnTouchAndScrollListener(final ListView listView1,final ListView listView2){


//设置listview2列表的scroll监听,用于滑动过程中左右不同步时校正
listView2.setOnScrollListener(new AbsListView.OnScrollListener() {

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
//如果停止滑动
if(scrollState == 0 || scrollState == 1){
//获得第一个子view
View subView = view.getChildAt(0);

if(subView !=null){
final int top = subView.getTop();
final int top1 = listView1.getChildAt(0).getTop();
final int position = view.getFirstVisiblePosition();

//如果两个首个显示的子view高度不等
if(top != top1){
listView1.setSelectionFromTop(position, top);
}
}
}

}

public void onScroll(AbsListView view, final int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
View subView = view.getChildAt(0);
if(subView != null){
final int top = subView.getTop();

// //如果两个首个显示的子view高度不等
int top1 = listView1.getChildAt(0).getTop();
if(!(top1 - 7 < top &&top < top1 + 7)){
listView1.setSelectionFromTop(firstVisibleItem, top);
listView2.setSelectionFromTop(firstVisibleItem, top);
}

}
}
});

//设置listview1列表的scroll监听,用于滑动过程中左右不同步时校正
listView1.setOnScrollListener(new AbsListView.OnScrollListener() {

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if(scrollState == 0 || scrollState == 1){
//获得第一个子view
View subView = view.getChildAt(0);

if(subView !=null){
final int top = subView.getTop();
final int top1 = listView2.getChildAt(0).getTop();
final int position = view.getFirstVisiblePosition();

//如果两个首个显示的子view高度不等
if(top != top1){
listView1.setSelectionFromTop(position, top);
listView2.setSelectionFromTop(position, top);
}
}
}
}

@Override
public void onScroll(AbsListView view, final int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
View subView = view.getChildAt(0);
if(subView != null){
final int top = subView.getTop();
listView1.setSelectionFromTop(firstVisibleItem, top);
listView2.setSelectionFromTop(firstVisibleItem, top);

}
}
});
}
}

 

xml:

<?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="horizontal">

<ListView
android:id="@+id/nameList"
android:layout_width="210dp"
android:layout_height="match_parent" />

<ListView
android:id="@+id/caloryList"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>

下面是后台的实现:

dao层:


    public List<Shiwu> select1(String name){
        Connection conn = DBUtil.getConn(); //连接数据库
        List<Shiwu> list1 = new ArrayList<Shiwu>();
        try {
            String sql="select * from reliang where name like '%"+name+"%' ";
            Statement pstmt = (Statement) conn.createStatement();
            ResultSet rs = (ResultSet) pstmt.executeQuery(sql);
            while(rs.next()) {
                Shiwu Shiwu=new Shiwu();
                Shiwu.setClasses(rs.getString("classes"));
                Shiwu.setName(rs.getString("name"));
                Shiwu.setCalory(rs.getString("calory"));
                list1.add(Shiwu);
            }
            rs.close();
            pstmt.close();
            conn.close();
            System.out.println("ShowDaoselect1");

        }catch(SQLException e) {
            e.printStackTrace();
        }
        return list1;
    }

servlet层:

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.test.bean.Shiwu;
import com.test.dao.ShowDao;

import java.util.Base64;
import java.util.List;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;

/**
* Servlet implementation class SearchServlet
*/
@WebServlet("/SearchServlet")
public class SearchServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public SearchServlet() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("gbk");
response.setContentType("text/html;charset=utf-8");
response.setCharacterEncoding("utf-8");
ShowDao sd = new ShowDao();
String name=request.getParameter("name");

// String str2 = new String(Base64.decode(name.getBytes(), Base64.DEFAULT));
// String name="Easy";
System.out.println(name);
List<Shiwu> list = sd.select1(name);
String json = JSON.toJSONString(list);
// System.out.println(json);
PrintWriter pw=response.getWriter();
pw.write(json);
pw.close();
// String json1 = JSON.toJSONString(list);
System.out.println(json);
System.out.println("这是SearchServlet");
// request.setAttribute("list", list);
// request.getRequestDispatcher("search.jsp").forward(request, response);
// PrintWriter pw=response.getWriter();
// pw.write(json1);
// pw.close();
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}

}

这样子展示功能就能实现了。

期间遇到的困难有:

如何布局的问题,最后我用到两个ListView来显示食物的名称和热量,然后对两个ListView进行绑定,使之可以共同滑动

 

posted @ 2020-04-17 22:19  ziyuliu  阅读(150)  评论(0编辑  收藏  举报