虽然上周完成了php大作业,但其他科目作业也接踵而至,本周基本就是一直在补作业,计网,数据库,MATLAB和web都有实验。
先看一看Android方面学到的东西吧:
适配器:
1 package com.example.toas; 2 3 import android.content.Context; 4 import android.view.LayoutInflater; 5 import android.view.View; 6 import android.view.ViewGroup; 7 import android.widget.BaseAdapter; 8 import android.widget.TextView; 9 10 import java.util.ArrayList; 11 12 public class MyNoteAdapter extends BaseAdapter { 13 private Context context; 14 private ArrayList<MyNoteBean> listData; 15 16 public MyNoteAdapter(Context context,ArrayList<MyNoteBean> listData){ 17 this.context=context; 18 this.listData=listData; 19 } 20 21 @Override 22 public int getCount() { return listData.size(); } 23 24 @Override 25 public Object getItem(int position) { return listData.get(position); } 26 27 @Override 28 public long getItemId(int position) { return position; } 29 30 @Override 31 public View getView(int position, View convertView, ViewGroup parent) { 32 MyNoteBean MNB = (MyNoteBean) getItem(position); 33 ViewHolder viewHolder=null; 34 if(convertView==null){ 35 LayoutInflater inflater=LayoutInflater.from(context); 36 //这里将ListView的格式xml文件名称写入,名字千万别写错,否则调用后显示的格式会发生错误 37 convertView=inflater.inflate(R.layout.list_item,null); 38 viewHolder=new ViewHolder(); 39 viewHolder.li_title=(TextView)convertView.findViewById(R.id.li_title); 40 viewHolder.li_kemu=(TextView)convertView.findViewById(R.id.li_kemu); 41 convertView.setTag(viewHolder); 42 } 43 viewHolder=(ViewHolder)convertView.getTag(); 44 MyNoteBean model=listData.get(position); 45 viewHolder.li_title.setText(model.getTitle()); 46 viewHolder.li_kemu.setText(model.getKemu()); 47 return convertView; 48 } 49 } 50 class ViewHolder{ 51 public TextView li_title; 52 public TextView li_kemu; 53 }
之所以只贴出这个代码,是因为我适配器以前只使用过一次,就是寒假做小作业的时候,那个时候未能完全理解,基本就是照着样子抄下来,一点一点改的。现在重新编写了一次,至少一些逻,辑关系还能明白一些。
下面再给出web的学习代码,主要是纯jsp完成数据库操作,以往写都是要通过servlet进行传输数据的,这里以添加数据入库为例:
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@ page import="java.sql.*"%> 4 <!DOCTYPE html> 5 <html> 6 <head> 7 <meta charset="UTF-8"> 8 <title>添加数据入库</title> 9 <script type="text/javascript" src="js/jquery-1.11.0.min.js"></script> 10 <style type="text/css"> 11 body{ 12 text-align:center; 13 } 14 </style> 15 </head> 16 <body> 17 <% 18 request.setCharacterEncoding("UTF-8"); 19 String submit=request.getParameter("submit"); 20 String snum=request.getParameter("snum"); 21 String sname=request.getParameter("sname"); 22 String sex=request.getParameter("sex"); 23 String birth=request.getParameter("birth"); 24 String addr=request.getParameter("addr"); 25 26 try{ 27 Class.forName("com.mysql.jdbc.Driver"); 28 Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8","root","Inazuma"); 29 30 PreparedStatement stmt=con.prepareStatement("insert into webstudent(snum,sname,sex,birthday,address)values(?,?,?,?,?)"); 31 stmt.setString(1,snum); 32 stmt.setString(2,sname); 33 stmt.setString(3,sex); 34 stmt.setString(4,birth); 35 stmt.setString(5,addr); 36 37 int i=stmt.executeUpdate(); 38 if(i==1){ 39 out.println("<h2>添加成功</h2><br/>"); 40 out.println("<a href='index.jsp'>返回首页</a>"); 41 }else{ 42 out.println("<h2>添加失败</h2><br/>"); 43 out.println("<a href='add.jsp'>重新添加</a>"); 44 } 45 stmt.close(); 46 con.close(); 47 }catch(Exception e){ 48 out.println("Exception:"+e.getMessage()); 49 } 50 %> 51 </body> 52 </html>
在进行编写的时候别忘了导入相应包: