讲解完学生、职员、书籍这些基础层之后,我们可以来了解一些应用层的活动。
新书上架、借阅排行、黑名单、图书馆介绍、图书馆新闻。
新书上架是查询数据库里的Book表,将最近注册的五本书的基本信息(若图书馆所有书籍少于5,则所有)通过listview展示出来。
源代码贴出:
1 package com.example.administrator.library1; 2 3 import android.content.Intent; 4 import android.support.v7.app.AppCompatActivity; 5 import android.os.Bundle; 6 import android.widget.ArrayAdapter; 7 import android.widget.ListView; 8 9 import org.litepal.LitePal; 10 11 import java.util.ArrayList; 12 import java.util.List; 13 14 public class New_Books extends AppCompatActivity { 15 16 ListView listView; 17 List<Book> bookList=new ArrayList<>(); 18 List<String> books=new ArrayList<>(); 19 @Override 20 protected void onCreate(Bundle savedInstanceState) { 21 super.onCreate(savedInstanceState); 22 setContentView(R.layout.activity_new__books); 23 24 listView=findViewById(R.id.books_new_list_id); 25 bookList= LitePal.findAll(Book.class); 26 if(bookList.size()>8) 27 { 28 for(int i=0;i<8;++i) 29 { 30 books.add("书名:"+bookList.get(bookList.size()-i-1).getName()+" "+"作者:"+bookList.get(bookList.size()-i-1).getWriter()); 31 } 32 } 33 else 34 { 35 for(int i=bookList.size()-1;i>=0;--i) 36 { 37 books.add("书名:"+bookList.get(i).getName()+" "+"作者:"+bookList.get(i).getWriter()); 38 } 39 } 40 ArrayAdapter adapter=new ArrayAdapter<String>(New_Books.this,android.R.layout.simple_list_item_1,books); 41 listView.setAdapter(adapter); 42 } 43 44 @Override 45 protected void onPause() { 46 Intent intent=new Intent(New_Books.this,MainActivity.class); 47 startActivity(intent); 48 super.onPause(); 49 } 50 }
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:orientation="vertical" 6 android:layout_width="match_parent" 7 android:layout_height="match_parent" 8 tools:context=".New_Books"> 9 <TextView 10 android:layout_width="match_parent" 11 android:layout_height="wrap_content" 12 android:textSize="30sp" 13 android:textColor="#091" 14 android:gravity="center" 15 android:text="新书上架"/> 16 <ListView 17 android:id="@+id/books_new_list_id" 18 android:layout_width="match_parent" 19 android:layout_height="match_parent"> 20 </ListView> 21 22 </LinearLayout>
接着是借阅排行榜。
搜索出借阅数量最多的五个学生,被借阅量最多的五本书,通过listview展示出来。
代码如下:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:orientation="vertical" 6 android:layout_width="match_parent" 7 android:layout_height="match_parent" 8 tools:context=".Ranking"> 9 10 <ImageView 11 android:layout_width="wrap_content" 12 android:layout_height="150dp" 13 android:layout_gravity="center" 14 android:src="@drawable/ranking" /> 15 16 <TextView 17 android:layout_width="match_parent" 18 android:layout_height="wrap_content" 19 android:gravity="center" 20 android:textSize="30sp" 21 android:text="人物榜" /> 22 23 <ListView 24 android:id="@+id/rank_list1_id" 25 android:layout_width="match_parent" 26 android:layout_height="wrap_content"></ListView> 27 <TextView 28 android:layout_width="match_parent" 29 android:layout_height="wrap_content" 30 android:gravity="center" 31 android:textSize="30sp" 32 android:text="书榜"/> 33 <ListView 34 android:id="@+id/rank_list2_id" 35 android:layout_width="match_parent" 36 android:layout_height="wrap_content"></ListView> 37 38 </LinearLayout>
1 package com.example.administrator.library1; 2 3 import android.content.Intent; 4 import android.support.v7.app.AppCompatActivity; 5 import android.os.Bundle; 6 import android.widget.ArrayAdapter; 7 import android.widget.ListView; 8 9 import org.litepal.LitePal; 10 11 import java.util.ArrayList; 12 import java.util.List; 13 14 public class Ranking extends AppCompatActivity { 15 16 ListView listView1,listView2; 17 List<Student> students=new ArrayList<>(); 18 List<Book> books=new ArrayList<>(); 19 List<String> stu=new ArrayList<>(); 20 List<String> bo=new ArrayList<>(); 21 @Override 22 protected void onCreate(Bundle savedInstanceState) { 23 super.onCreate(savedInstanceState); 24 setContentView(R.layout.activity_ranking); 25 26 listView1=findViewById(R.id.rank_list1_id); 27 listView2=findViewById(R.id.rank_list2_id); 28 29 students= LitePal.findAll(Student.class); 30 books=LitePal.findAll(Book.class); 31 32 if(students.size()>5) 33 { 34 for(int i=0;i<5;++i) 35 { 36 for(int j=students.size()-1;j>0;--j) 37 { 38 if(students.get(j).getBookAmount()>students.get(j-1).getBookAmount()) 39 { 40 Student student_temp; 41 student_temp=students.get(j); 42 students.set(j,students.get(j-1)); 43 students.set(j-1,student_temp); 44 } 45 } 46 } 47 for(int i=0;i<5;++i) 48 { 49 stu.add("学生:"+students.get(i).getName()+" "+"借阅量:"+String.valueOf(students.get(i).getBookAmount())); 50 } 51 } 52 else 53 { 54 for(int i=0;i<students.size()-1;++i) 55 { 56 for(int j=i+1;j<students.size();++j) 57 { 58 if(students.get(i).getBookAmount()<students.get(j).getBookAmount()) 59 { 60 Student student_temp; 61 student_temp=students.get(i); 62 students.set(i,students.get(j)); 63 students.set(j,student_temp); 64 } 65 } 66 } 67 for(int i=0;i<students.size();++i) 68 { 69 stu.add("姓名:"+students.get(i).getName()+" "+"借阅量:"+String.valueOf(students.get(i).getBookAmount())); 70 } 71 } 72 ArrayAdapter<String> adapter1=new ArrayAdapter<String>(Ranking.this,android.R.layout.simple_list_item_1,stu); 73 listView1.setAdapter(adapter1); 74 75 if(books.size()>5) 76 { 77 for(int i=0;i<5;++i) 78 { 79 for(int j=books.size()-1;j>0;--j) 80 { 81 if(books.get(j).getAmount()-books.get(j).getIn_shelf()>books.get(j-1).getAmount()-books.get(j-1).getIn_shelf()) 82 { 83 Book book_temp; 84 book_temp=books.get(j); 85 books.set(j,books.get(j-1)); 86 books.set(j-1,book_temp); 87 } 88 } 89 } 90 for(int i=0;i<5;++i) 91 { 92 bo.add("书名:"+books.get(i).getName()+" "+"被借阅量:"+String.valueOf(books.get(i).getAmount()-books.get(i).getIn_shelf())); 93 } 94 } 95 else 96 { 97 for(int i=0;i<books.size()-1;++i) 98 { 99 for(int j=i+1;j<books.size();++j) 100 { 101 if(books.get(i).getAmount()-books.get(i).getIn_shelf()<books.get(j).getAmount()-books.get(i).getIn_shelf()) 102 { 103 Book book_temp; 104 book_temp=books.get(i); 105 books.set(i,books.get(j)); 106 books.set(j,book_temp); 107 } 108 } 109 } 110 for(int i=0;i<books.size();++i) 111 { 112 bo.add("书名:"+books.get(i).getName()+" "+"被借阅量:"+String.valueOf(books.get(i).getAmount()-books.get(i).getIn_shelf())); 113 } 114 } 115 ArrayAdapter<String> adapter2=new ArrayAdapter<String>(Ranking.this,android.R.layout.simple_list_item_1,bo); 116 listView2.setAdapter(adapter2); 117 } 118 119 @Override 120 protected void onPause() { 121 Intent intent=new Intent(Ranking.this,MainActivity.class); 122 startActivity(intent); 123 super.onPause(); 124 } 125 }
效果图:
关于图书馆介绍,图书馆新闻这两个的实现比较简单,可以在Github上查看。
至此 我的第一个系统性的Android应用程序讲解完毕,中间有一些不足之处,希望之后我能够将它们进行修正。