java课程之团队开发冲刺阶段2.8
昨日总结:
1.具体情况已经写在了昨天的当日总结当中
遇到的问题:
1.toolbar的返回键与菜单键冲突,导致无法同时使用
今天的任务:
1.完整实现课程查询任务
当日总结:
1.完整实现,唯一的遗憾就是课程信息不完全,导致只能查询个别信息
2.首先实现的是listview中的子组件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginLeft="@dimen/activity_horizontal_margin" android:layout_marginRight="@dimen/activity_horizontal_margin" android:paddingLeft="5dp" android:paddingRight="5dp"> <!-- 类似于iOS开发中Cell的布局 --> <LinearLayout android:orientation="horizontal" android:gravity="center_vertical" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp"> <TextView android:id="@+id/lesson_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="left" android:textSize="20dp" android:text="课程名称" android:textColor="#ff0000" android:lines="1"/> <TextView android:id="@+id/teacher_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="right" android:textSize="15dp" android:text="教师名称" android:lines="1"/> </LinearLayout> <LinearLayout android:orientation="horizontal" android:gravity="center_vertical" android:layout_width="match_parent" android:layout_height="25dp"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:textSize="15dp" android:id="@+id/lesson_building" android:text="教学楼" android:lines="1"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:lines="1" android:id="@+id/lesson_classroom" android:textSize="15dp" android:text="教室" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:lines="1" android:textSize="15dp" android:id="@+id/lesson_nature" android:text="课程性质"/> </LinearLayout> <LinearLayout android:orientation="horizontal" android:gravity="center_vertical" android:layout_width="match_parent" android:layout_height="35dp" android:layout_marginBottom="10dp"> <TextView android:id="@+id/lesson_week" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:textSize="15dp" android:text="星期" android:lines="1"/> <TextView android:id="@+id/lesson_number" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:lines="1" android:textSize="15dp" android:text="节次" /> <TextView android:id="@+id/lesson_sumweek" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:lines="1" android:textSize="15dp" android:text="周次"/> </LinearLayout> </LinearLayout>
3,然后我们需要适配器
public LessonAdapter(Context context, int resource, List<LessonBean> objects) { super(context, resource, objects); resourceId = resource; } @Override /** * @param position 当前设置的Cell行数,类似于iOS开发中的indexPath.row */ public View getView(int position, View convertView, ViewGroup parent) { LessonBean lesson = getItem(position); View lessonView = LayoutInflater.from(getContext()).inflate(resourceId, null); TextView lesson_name = lessonView.findViewById(R.id.lesson_name); TextView teacher_name = lessonView.findViewById(R.id.teacher_name); TextView lesson_building = lessonView.findViewById(R.id.lesson_building); TextView lesson_classroom = lessonView.findViewById(R.id.lesson_classroom); TextView lesson_nature = lessonView.findViewById(R.id.lesson_nature); TextView lesson_week = lessonView.findViewById(R.id.lesson_week); TextView lesson_number = lessonView.findViewById(R.id.lesson_number); TextView lesson_sumweek = lessonView.findViewById(R.id.lesson_sumweek); lesson_name.setText(lesson.getLesson_name()); teacher_name.setText(lesson.getTeacher_name()); lesson_building.setText(lesson.getLesson_building()); lesson_classroom.setText(lesson.getLesson_classroom()); lesson_nature.setText(lesson.getLesson_nature()); lesson_week.setText(lesson.getLesson_week()); lesson_number.setText(lesson.getLesson_number()); lesson_sumweek.setText(lesson.getLesson_sumweek()); return lessonView; } }
4.当然,适配之前应该需要相应的Javabean,这个应该放在前面,顺序错了,就不改了
public class LessonBean { private String lesson_name; private String teacher_name; private String lesson_building; private String lesson_classroom; private String lesson_nature; private String lesson_week; private String lesson_number; private String lesson_sumweek; public LessonBean(String number1,String number2,String number3,String number4,String number5,String number6,String number7,String number8) { lesson_name=number1; teacher_name=number2; lesson_building=number3; lesson_classroom=number4; lesson_nature=number5; lesson_week=number6; lesson_number=number7; lesson_sumweek=number8; } public String getLesson_name() { return lesson_name; } public void setLesson_name(String lesson_name) { this.lesson_name = lesson_name; } public String getTeacher_name() { return teacher_name; } public void setTeacher_name(String teacher_name) { this.teacher_name = teacher_name; } public String getLesson_building() { return lesson_building; } public void setLesson_building(String lesson_building) { this.lesson_building = lesson_building; } public String getLesson_classroom() { return lesson_classroom; } public void setLesson_classroom(String lesson_classroom) { this.lesson_classroom = lesson_classroom; } public String getLesson_nature() { return lesson_nature; } public void setLesson_nature(String lesson_nature) { this.lesson_nature = lesson_nature; } public String getLesson_week() { return lesson_week; } public void setLesson_week(String lesson_week) { this.lesson_week = lesson_week; } public String getLesson_number() { return lesson_number; } public void setLesson_number(String lesson_number) { this.lesson_number = lesson_number; } public String getLesson_sumweek() { return lesson_sumweek; } public void setLesson_sumweek(String lesson_sumweek) { this.lesson_sumweek = lesson_sumweek; } }
5.最后一步就是调用游标,读取数据然后添加到scrollview组件中的listview中
public void test(String DB_PATH,String DB_NAME,String key) { // 下面测试 /data/data/com.test.db/databases/ 下的数据库是否能正常工作 SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(DB_PATH + DB_NAME, null); Cursor cursor=null; productList.clear(); if(key.isEmpty()) { cursor = database.rawQuery("select 课程,教师,教学楼,教室,性质,星期,节次,周次 from lesson_informatization ", null); } else { cursor = database.rawQuery("select 课程,教师,教学楼,教室,性质,星期,节次,周次 from lesson_informatization where 课程 like '%"+key+"%'", null); } ArrayList<LessonBean> list=new ArrayList<LessonBean>(); if(cursor.moveToFirst()) { do{ String name=cursor.getString(cursor.getColumnIndex("课程")); String teacher=cursor.getString(cursor.getColumnIndex("教师")); String lesson_building=cursor.getString(cursor.getColumnIndex("教学楼")); String lesson_classroom=cursor.getString(cursor.getColumnIndex("教室")); String lesson_nature=cursor.getString(cursor.getColumnIndex("性质")); String lesson_week=cursor.getString(cursor.getColumnIndex("星期")); String lesson_number=cursor.getString(cursor.getColumnIndex("节次")); String lesson_sumweek=cursor.getString(cursor.getColumnIndex("周次")); productList.add(new LessonBean(name,teacher,lesson_building,lesson_classroom,lesson_nature,lesson_week,lesson_number,lesson_sumweek)); }while (cursor.moveToNext()); } // if (cursor.getCount() > 0) { // cursor.moveToFirst(); // try { // // 解决中文乱码问题 // byte test[] = cursor.getBlob(0); // String strtest = new String(test, "utf-8").trim(); // // 看输出的信息是否正确 // Log.d("是否能够查找到本地数据库的信息",strtest); // System.out.println(strtest); // } catch (UnsupportedEncodingException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } // } cursor.close(); if(productList.size()==0) { productList.add(new LessonBean("没有找到相关课程哦(ಥ_ಥ)","","","","","","","")); } }