You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
book/UserActivity.java

227 lines
8.5 KiB

package com.grassroots.booktracker.activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.grassroots.booktracker.R;
import com.grassroots.booktracker.adapter.BookInfo;
import com.grassroots.booktracker.adapter.BookInfoAdapter;
import com.grassroots.booktracker.utils.DatabaseHelper;
import java.util.ArrayList;
import java.util.List;
public class UserActivity extends AppCompatActivity {
private Button insertButton, updateButton, searchButton, deleteButton;
private EditText name, day;
private RecyclerView rvBookInfo;
private TextView tvCount ,tvDays;
private BookInfoAdapter bookInfoAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user);
TextView tvTitle = findViewById(R.id.tv_main_title);
tvTitle.setText("借阅信息");
TextView tvBack = findViewById(R.id.tv_back);
tvBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
TextView tvUsername = findViewById(R.id.tv_username);
String loginUserName = getSharedPreferences("loginInfo", MODE_PRIVATE).getString("loginUserName", "");
tvUsername.setText(loginUserName);
rvBookInfo = findViewById(R.id.rv_book_info);
insertButton = findViewById(R.id.btn_insert);
updateButton = findViewById(R.id.btn_update);
searchButton = findViewById(R.id.btn_search);
deleteButton = findViewById(R.id.btn_delete);
name = findViewById(R.id.name);
day = findViewById(R.id.day);
tvCount = findViewById(R.id.tv_count);
tvDays = findViewById(R.id.tv_days);
tvCount.setText("共借阅"+getCount()+"本");
tvDays.setText("共借阅"+getAllDays()+"天");
initRv();
SQLiteDatabase db = DatabaseHelper.getInstance().getReadableDatabase();
myShow();
insertButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (TextUtils.isEmpty(name.getText().toString())){
Toast.makeText(UserActivity.this, "请输入书名", Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(day.getText().toString())){
Toast.makeText(UserActivity.this, "请输入借阅天数", Toast.LENGTH_SHORT).show();
return;
}
SQLiteDatabase db = DatabaseHelper.getInstance().getWritableDatabase();
ContentValues values = new ContentValues();
values.put("name", name.getText().toString());
values.put("days", day.getText().toString());
long id = db.insert("information", null, values);
Log.d("myDeBug", "insert");
myShow();
db.close();
name.setText(null);
day.setText(null);
tvCount.setText("共借阅"+getCount()+"本");
tvDays.setText("共借阅"+getAllDays()+"天");
}
});
updateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (TextUtils.isEmpty(name.getText().toString())){
Toast.makeText(UserActivity.this, "请输入书名", Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(day.getText().toString())){
Toast.makeText(UserActivity.this, "请输入借阅天数", Toast.LENGTH_SHORT).show();
return;
}
SQLiteDatabase db = DatabaseHelper.getInstance().getWritableDatabase();
ContentValues values = new ContentValues();
values.put("days", day.getText().toString());
db.update("information", values, "name=?", new String[]{name.getText().toString()});
myShow();
db.close();
Log.d("myDebug", "update");
name.setText(null);
day.setText(null);
tvCount.setText("共借阅"+getCount()+"本");
tvDays.setText("共借阅"+getAllDays()+"天");
}
});
searchButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (TextUtils.isEmpty(name.getText().toString())){
Toast.makeText(UserActivity.this, "请输入书名", Toast.LENGTH_SHORT).show();
return;
}
SQLiteDatabase db = DatabaseHelper.getInstance().getWritableDatabase();
String name1 = name.getText().toString();
if (name1.equals("")) {
myShow();
db.close();
} else {
Cursor cursor = db.rawQuery("select * from information where name = ? ", new String[]{name1});
List<BookInfo> bookInfoList=new ArrayList<>();
while (cursor.moveToNext()) {
String newName = cursor.getString(1);
int newDays = cursor.getInt(2);
bookInfoList.add(new BookInfo(newName,newDays));
}
bookInfoAdapter.setBookInfoList(bookInfoList);
cursor.close();
db.close();
name.setText(null);
day.setText(null);
}
}
});
deleteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (TextUtils.isEmpty(name.getText().toString())){
Toast.makeText(UserActivity.this, "请输入书名", Toast.LENGTH_SHORT).show();
return;
}
SQLiteDatabase db = DatabaseHelper.getInstance().getWritableDatabase();
db.delete("information", "name=?", new String[]{name.getText().toString()});
myShow();
db.close();
Log.d("myDeBug", "DeleteSuccess");
name.setText(null);
day.setText(null);
tvCount.setText("共借阅"+getCount()+"本");
tvDays.setText("共借阅"+getAllDays()+"天");
}
});
}
private int getCount(){
SQLiteDatabase db = DatabaseHelper.getInstance().getReadableDatabase();
Cursor cursor = db.rawQuery("select count(*) from information", null);
cursor.moveToFirst();
int count = cursor.getInt(0);
cursor.close();
return count;
}
private int getAllDays(){
SQLiteDatabase db = DatabaseHelper.getInstance().getReadableDatabase();
Cursor cursor = db.rawQuery("select sum(days) from information", null);
cursor.moveToFirst();
int count = cursor.getInt(0);
cursor.close();
return count;
}
private void initRv(){
bookInfoAdapter = new BookInfoAdapter();
rvBookInfo.setAdapter(bookInfoAdapter);
rvBookInfo.setLayoutManager(new LinearLayoutManager(this));
}
public void myShow() {
SQLiteDatabase db = DatabaseHelper.getInstance().getReadableDatabase();
Cursor cursor = db.rawQuery("select * from information", null);
List<BookInfo> bookInfoList=new ArrayList<>();
while (cursor.moveToNext()) {
String newName = cursor.getString(1);
int newDays = cursor.getInt(2);
bookInfoList.add(new BookInfo(newName,newDays));
}
cursor.close();
bookInfoAdapter.setBookInfoList(bookInfoList);
}
}