目的
此项目用以辅助背诵方歌
项目gitee地址
项目结构
运行效果
android简易背诵方歌项目
CollectActivity
package com.example.recitefangge4.collect;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import com.example.recitefangge4.R;
import com.example.recitefangge4.database.FangGeDBHelper;
import com.example.recitefangge4.entity.FangGe;
public class CollectActivity extends AppCompatActivity implements View.OnClickListener {
private FangGeDBHelper mHelper;
private EditText et_name;
private EditText et_efficacy;
private EditText et_content;
private EditText et_category;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_collect);
et_name = findViewById(R.id.et_name);
et_efficacy = findViewById(R.id.et_efficacy);
et_content = findViewById(R.id.et_content);
et_category = findViewById(R.id.et_category);
findViewById(R.id.btn_collet).setOnClickListener(this);
findViewById(R.id.btn_reset).setOnClickListener(this);
}
@Override
protected void onStart() {
super.onStart();
//获取数据库帮助器实例
mHelper = FangGeDBHelper.getInstance(this);
//打开数据库帮助器读写连接
mHelper.openWriteLink();
mHelper.openReadLink();
}
@Override
protected void onStop() {
super.onStop();
//关闭数据库连接
mHelper.closeLink();
}
@Override
public void onClick(View v) {
if(v.getId() == R.id.btn_collet){
String name = et_name.getText().toString();
String efficacy = et_efficacy.getText().toString();
String content = et_content.getText().toString();
String category = et_category.getText().toString();
FangGe fangGe = new FangGe();
fangGe.setName(name);
fangGe.setEfficacy(efficacy);
fangGe.setContent(content);
fangGe.setCategory(category);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("确认录入");
builder.setMessage("是否录入?");
builder.setPositiveButton("是", ((dialog, which) -> {
long res = mHelper.insert(fangGe);
if(res > 0 ) {
Toast.makeText(this, "录入成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "录入失败", Toast.LENGTH_SHORT).show();
}
}));
builder.setNegativeButton("否", ((dialog, which) -> {
Toast.makeText(this, "不录入", Toast.LENGTH_SHORT).show();
}));
AlertDialog dialog = builder.create();
dialog.show();
}
if(v.getId() == R.id.btn_reset){
et_name.setText("");
et_efficacy.setText("");
et_content.setText("");
et_category.setText("");
}
}
}
FangGeDBHelper
package com.example.recitefangge4.database;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteOpenHelper;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import com.example.recitefangge4.entity.FangGe;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class FangGeDBHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "recite_fang_ge.db";
private static final String TABLE_NAME = "fang_ge_info";
private static final int DB_VERSION = 1;
private static FangGeDBHelper mHelper = null;
private SQLiteDatabase mRDB = null;
private SQLiteDatabase mWDB = null;
private FangGeDBHelper(Context context){
super(context, DB_NAME, null, DB_VERSION);
}
//利用单例模式获取数据库唯一实例
public static FangGeDBHelper getInstance(Context context) {
if(mHelper == null){
mHelper = new FangGeDBHelper(context);
}
return mHelper;
}
@Override
public void onCreate(SQLiteDatabase db) {
//创建数据库 执行建表语句
String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (" +
"_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," +
"name VARCHAR NOT NULL," +
"efficacy VARCHAR NOT NULL," +
"content VARCHAR NOT NULL," +
"category VARCHAR NOT NULL," +
"update_time VARCHAR NOT NULL);";
db.execSQL(sql);
}
// 打开数据库的读连接
public SQLiteDatabase openReadLink(){
if(mRDB == null || !mRDB.isOpen()) {
mRDB = mHelper.getReadableDatabase();
}
return mRDB;
}
// 打开数据库的写连接
public SQLiteDatabase openWriteLink(){
if(mWDB == null || !mWDB.isOpen()){
mWDB = mHelper.getWritableDatabase();
}
return mWDB;
}
// 关闭数据库连接
public void closeLink(){
if(mRDB != null && mRDB.isOpen()){
mRDB.close();
mRDB = null;
}
if(mWDB != null && mWDB.isOpen()) {
mWDB.close();
mWDB = null;
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public long insert(FangGe fangGe){
ContentValues values = new ContentValues();
values.put("name", fangGe.getName());
values.put("efficacy", fangGe.getEfficacy());
values.put("content", fangGe.getContent());
values.put("category", fangGe.getCategory());
values.put("update_time", new Date().toString());
return mWDB.insert(TABLE_NAME, null, values);
}
// 获取数据库所有内容
public List<FangGe> queryAll() {
List<FangGe> fangGeList = new ArrayList<>();
Cursor cursor = mRDB.query(TABLE_NAME, null, null, null, null, null, null);
while ( cursor.moveToNext() ) {
FangGe fangGe = new FangGe();
fangGe.setName(cursor.getString(1));
fangGe.setEfficacy(cursor.getString(2));
fangGe.setContent(cursor.getString(3));
fangGe.setCategory(cursor.getString(4));
fangGeList.add(fangGe);
}
return fangGeList;
}
//通过id获取方歌
public FangGe queryById(int id) {
Cursor cursor = mRDB.query(TABLE_NAME, null, "_id=?", new String[]{String.valueOf(id)}, null, null, null);
FangGe fangGe = new FangGe();
if (cursor.moveToNext()){
fangGe.setName(cursor.getString(1));
fangGe.setEfficacy(cursor.getString(2));
fangGe.setContent(cursor.getString(3));
fangGe.setCategory(cursor.getString(4));
}
return fangGe;
}
//通过name获取数据
public List<FangGe> queryByName(String name) {
List<FangGe> fangGeList = new ArrayList<>();
Cursor cursor = mRDB.query(TABLE_NAME, null, "name=?", new String[]{name}, null, null, null);
while ( cursor.moveToNext() ) {
FangGe fangGe = new FangGe();
fangGe.setName(cursor.getString(1));
fangGe.setEfficacy(cursor.getString(2));
fangGe.setContent(cursor.getString(3));
fangGe.setCategory(cursor.getString(4));
fangGeList.add(fangGe);
}
return fangGeList;
}
//获取数据库内容大小
public long queryCount(){
String sql = "select count(*) from " + TABLE_NAME;
Cursor cursor = mRDB.rawQuery(sql, null);
cursor.moveToFirst();
long count = cursor.getLong(0);
cursor.close();
return count;
}
// 修改数据
public long update(FangGe fangGe) {
ContentValues values = new ContentValues();
values.put("name", fangGe.getName());
values.put("efficacy", fangGe.getEfficacy());
values.put("content", fangGe.getContent());
values.put("category", fangGe.getCategory());
values.put("update_time", new Date().toString());
return mWDB.update(TABLE_NAME, values, "name=?", new String[]{fangGe.getName()});
}
}
FangGe
package com.example.recitefangge4.entity;
public class FangGe {
private String name;
private String efficacy;
private String content;
private String category;
public FangGe() {
}
public FangGe(String name, String efficacy, String content, String category) {
this.name = name;
this.efficacy = efficacy;
this.content = content;
this.category = category;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEfficacy() {
return efficacy;
}
public void setEfficacy(String efficacy) {
this.efficacy = efficacy;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
@Override
public String toString() {
return "FangGe{" +
"name='" + name + '\'' +
", efficacy='" + efficacy + '\'' +
", content='" + content + '\'' +
", category='" + category + '\'' +
'}';
}
}
Zi
package com.example.recitefangge4.entity;
public class Zi{
private Integer id;
private String value;
private Integer state;
public Integer getState() {
return state;
}
public void setState(Integer state) {
this.state = state;
}
public Zi(Integer id, String value, Integer state) {
this.id = id;
this.value = value;
this.state = state;
}
public Zi() {
}
public Zi(Integer id, String value) {
this.id = id;
this.value = value;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
@Override
public String toString() {
return "Zi{" +
"id=" + id +
", value='" + value + '\'' +
", state=" + state +
'}';
}
}
ZiSate
package com.example.recitefangge4.entity;
public class ZiState {
public static final Integer disableSwap = 0;
public static final Integer enableSwap = 1;
public static final Integer correct = 2;
}
ModifyActivity
package com.example.recitefangge4.modify;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import com.example.recitefangge4.R;
import com.example.recitefangge4.database.FangGeDBHelper;
import com.example.recitefangge4.entity.FangGe;
import java.util.List;
public class ModifyActivity extends AppCompatActivity implements View.OnClickListener, View.OnFocusChangeListener {
private FangGeDBHelper mHelper;
private EditText et_name;
private EditText et_efficacy;
private EditText et_content;
private EditText et_category;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_modify);
et_name = findViewById(R.id.et_name);
et_efficacy = findViewById(R.id.et_efficacy);
et_content = findViewById(R.id.et_content);
et_category = findViewById(R.id.et_category);
findViewById(R.id.btn_collet).setOnClickListener(this);
findViewById(R.id.btn_reset).setOnClickListener(this);
et_name.setOnFocusChangeListener(this);
}
@Override
protected void onStart() {
super.onStart();
//获取数据库帮助器实例
mHelper = FangGeDBHelper.getInstance(this);
//打开数据库帮助器读写连接
mHelper.openWriteLink();
mHelper.openReadLink();
}
@Override
protected void onStop() {
super.onStop();
//关闭数据库连接
mHelper.closeLink();
}
@Override
public void onClick(View v) {
if(v.getId() == R.id.btn_collet){
String name = et_name.getText().toString();
String efficacy = et_efficacy.getText().toString();
String content = et_content.getText().toString();
String category = et_category.getText().toString();
FangGe fangGe = new FangGe();
fangGe.setName(name);
fangGe.setEfficacy(efficacy);
fangGe.setContent(content);
fangGe.setCategory(category);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("确认修改");
builder.setMessage("是否修改?");
builder.setPositiveButton("是", ((dialog, which) -> {
long res = mHelper.update(fangGe);
if(res > 0 ) {
Toast.makeText(this, "修改成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "修改失败", Toast.LENGTH_SHORT).show();
}
}));
builder.setNegativeButton("否", ((dialog, which) -> {
Toast.makeText(this, "不修改", Toast.LENGTH_SHORT).show();
}));
AlertDialog dialog = builder.create();
dialog.show();
}
if(v.getId() == R.id.btn_reset){
et_name.setText("");
et_efficacy.setText("");
et_content.setText("");
et_category.setText("");
}
}
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(v.getId() == R.id.et_name) {
String name = et_name.getText().toString();
if(name.length() > 0 && !hasFocus) {
//查询
List<FangGe> fangGeList = mHelper.queryByName(name);
if(fangGeList.size() > 0){
for (int i = 0; i < fangGeList.size(); i++) {
FangGe fangGe = fangGeList.get(i);
et_efficacy.setText(fangGe.getEfficacy());
et_category.setText(fangGe.getCategory());
et_content.setText(fangGe.getContent());
}
} else {
Toast.makeText(this, "没有此方,请重新输入", Toast.LENGTH_SHORT).show();
}
}
}
}
}
ReciteActivity
package com.example.recitefangge4.recite;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.widget.EditText;
import android.widget.GridLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.example.recitefangge4.R;
import com.example.recitefangge4.Util.MyStringArrayUtil;
import com.example.recitefangge4.database.FangGeDBHelper;
import com.example.recitefangge4.entity.FangGe;
import com.example.recitefangge4.entity.Zi;
import com.example.recitefangge4.entity.ZiState;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class ReciteActivity extends AppCompatActivity implements View.OnClickListener {
private TextView tv_name;
private TextView tv_efficacy;
private GridLayout gl_content;
//用于记录交换状态的数组
private List<Zi> swapList = new ArrayList<>();
private FangGe mFangGe;
//显示内容
private List<Zi> displayContent = new ArrayList<>();
//目标内容
private String targetContent = "无内容";
private List<TextView> textViewList = new ArrayList<>();
private int displayWidth;
private int displayHeight;
//可交换状态下的背景
private GradientDrawable enableDrawable;
//不可交换状态背景
private GradientDrawable disableDrawable;
//正确状态背景
private GradientDrawable correctDrawable;
private EditText et_designeted_name;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recite);
// 获取屏幕宽高
DisplayMetrics dm = getResources().getDisplayMetrics();
displayWidth = dm.widthPixels;
displayHeight = dm.heightPixels;
Log.d("nin", "displayWidth: " + displayWidth);
Log.d("nin", "displayHeight: " + displayHeight);
tv_name = findViewById(R.id.tv_name);
tv_efficacy = findViewById(R.id.tv_eficacy);
gl_content = findViewById(R.id.gl_content);
et_designeted_name = findViewById(R.id.et_designated_name);
//下一首方歌
findViewById(R.id.btn_next).setOnClickListener(this);
//背诵指定方歌
findViewById(R.id.btn_designated).setOnClickListener(this);
//设置最大列数
gl_content.setColumnCount(7);
// 初始化
init();
}
/**
* 添加textView
*/
private TextView addTextView(Integer id, String value){
TextView tv = new TextView(this);
tv.setId(id);
tv.setText(value);
tv.setBackground(enableDrawable);
tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 35);
gl_content.addView(tv);
tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myOnClickListener(tv.getId(), tv.getText().toString());
}
});
return tv;
}
/**
* TextView 监听事件
*/
private void myOnClickListener(Integer id, String value) {
/**
* 先调用swap
* 交不交换由swap处理
* 该函数只负责传输字的状态及值
* 然后再次调用swap,
* 避免需要第三次点击才交换值
*/
//判断全文是否正确
Boolean correct = isCorrect(targetContent);
Log.d("ning", "myOnClickListener: correct" + correct);
if(correct){
Toast.makeText(this, "正确", Toast.LENGTH_SHORT).show();
return;
}
swap();
Zi zi = displayContent.get(id);
if(zi.getState() == ZiState.enableSwap){
zi.setState(ZiState.disableSwap);
displayContent.get(id).setState(ZiState.disableSwap);
swapList.add(zi);
}
swap();
display(displayContent);
}
/**
* 初始化显示内容
* */
public void initDisplayContent() {
String tmpContent = targetContent;
Log.d("ning", "initDisplayContent: " + tmpContent);
if(tmpContent != null) {
//正确数组
String[] tmp = tmpContent.split("");
// 乱序数组
String[] any = MyStringArrayUtil.getOutOfOrderStringArray(targetContent);
//生成显示内容displayContent
for (int i = 0; i < any.length; i++) {
//刷新字状态
if(tmp[i].equals(any[i])){
Zi zi = new Zi();
zi.setState(ZiState.correct);
zi.setValue(any[i]);
zi.setId(i);
displayContent.add(zi);
} else {
Zi zi = new Zi();
zi.setState(ZiState.enableSwap);
zi.setValue(any[i]);
zi.setId(i);
displayContent.add(zi);
}
}
}
}
public void initDisplayContentByContent(String content) {
targetContent = content;
String tmpContent = targetContent;
Log.d("ning", "initDisplayContent: " + tmpContent);
if(tmpContent != null) {
//正确数组
String[] tmp = tmpContent.split("");
// 乱序数组
String[] any = MyStringArrayUtil.getOutOfOrderStringArray(targetContent);
//生成显示内容displayContent
for (int i = 0; i < any.length; i++) {
//刷新字状态
if(tmp[i].equals(any[i])){
Zi zi = new Zi();
zi.setState(ZiState.correct);
zi.setValue(any[i]);
zi.setId(i);
displayContent.add(zi);
} else {
Zi zi = new Zi();
zi.setState(ZiState.enableSwap);
zi.setValue(any[i]);
zi.setId(i);
displayContent.add(zi);
}
}
}
}
/**
* 交换值
*/
private void swap(){
if (swapList.size() == 2) {
//交换值
Zi zi1;
Zi zi2;
zi1 = swapList.get(0);
zi2 = swapList.get(1);
String tmpZi = zi1.getValue();
zi1.setValue(zi2.getValue());
zi2.setValue(tmpZi);
zi1.setState(ZiState.enableSwap);
zi2.setState(ZiState.enableSwap);
Log.d("swap", "swap: " + zi2.toString() + "|" + zi1.toString());
displayContent.get(zi1.getId()).setValue(zi1.getValue());
displayContent.get(zi1.getId()).setState(zi1.getState());
displayContent.get(zi2.getId()).setValue(zi2.getValue());
displayContent.get(zi2.getId()).setState(zi2.getState());
//清空list
swapList.clear();
swapList = new ArrayList<>();
Log.d("ning", "swap: length: " + swapList.size());
}
for (int i = 0; i < displayContent.size(); i++) {
Zi zi = displayContent.get(i);
Log.d("displayContent", "swap: discontent zi: " + zi.toString());
}
//重新显示内容
display(displayContent);
}
//判断全文是否正确
public Boolean isCorrect(String target){
String[] tmp = target.split("");
String tmpStr = "无内容";
tmpStr = "";
for (int i = 0; i < displayContent.size(); i++) {
tmpStr += displayContent.get(i).getValue();
}
boolean equals = tmpStr.equals(target);
return equals;
}
//初始化显示
public void initDisplay(List<Zi> list){
//清空textView
textViewList.clear();
textViewList = new ArrayList<>();
Log.d("recite2", "initDisplay: intitDisplay; : textViewList.size(): " + textViewList.size());
for (int i = 0; i < list.size(); i++) {
// Button button = addButton(i, list.get(i).getValue());
// buttonList.add(button);
TextView tv = addTextView(i, list.get(i).getValue());
textViewList.add(tv);
}
display(list);
}
//显示
public void display(List<Zi> list) {
for (int i = 0; i < list.size(); i++) {
Zi zi = list.get(i);
TextView tv = textViewList.get(zi.getId());
if(zi.getState() == ZiState.correct){
tv.setBackground(correctDrawable);
}
if(zi.getState() == ZiState.disableSwap){
tv.setBackground(disableDrawable);
}
if(zi.getState() == ZiState.enableSwap){
tv.setBackground(enableDrawable);
}
tv.setText(zi.getValue());
}
}
//单字的背景初始化
public void initDrawableZi(){
//可交换状态下的背景
enableDrawable = new GradientDrawable();
enableDrawable.setColor(Color.parseColor("#E3E3E3"));
enableDrawable.setCornerRadius(10f);
//不可交换状态背景
disableDrawable = new GradientDrawable();
disableDrawable.setColor(Color.parseColor("#EC7259"));
disableDrawable.setCornerRadius(10);
//正确状态背景
correctDrawable = new GradientDrawable();
correctDrawable.setColor(Color.parseColor("#6200ED"));
correctDrawable.setCornerRadius(10);
}
//初始话目标内容
private void initTargetContent(){
mFangGe = getAnyOneFangGe();
targetContent = mFangGe.getContent();
}
//交换过程中刷新显示内容
private void flushDisplayInSwap(){
display(swapList);
}
//获取任意一首方歌
public FangGe getAnyOneFangGe(){
FangGe fangGe = new FangGe();
//获取数据库帮助器
FangGeDBHelper helper = FangGeDBHelper.getInstance(this);
//开启读连接
helper.openReadLink();
long count = helper.queryCount();
Random random = new Random();
int id = random.nextInt((int) count) + 1;
fangGe = helper.queryById(id);
//关闭连接
helper.closeLink();
return fangGe;
}
@Override
protected void onDestroy() {
super.onDestroy();
//
}
@Override
public void onClick(View v) {
if(v.getId() == R.id.btn_next){
clear();
init();
}
if(v.getId() == R.id.btn_designated){
String name = et_designeted_name.getText().toString();
FangGe fangGe = getOneFangGeByName(name);
clear();
flush(fangGe);
}
}
//清空界面
public void clear(){
//清除显示内容
removeDisplay(textViewList);
tv_name.setText("");
tv_efficacy.setText("");
targetContent = "";
mFangGe = new FangGe();
swapList.clear();
displayContent.clear();
textViewList.clear();
swapList = new ArrayList<>();
displayContent = new ArrayList<>();
textViewList = new ArrayList<>();
display(displayContent);
Log.d("recite", "clear: swapList.size()" + swapList.size());
Log.d("recite", "clear: displaycontent.size():: " + displayContent.size());
Log.d("recite", "clear: textViewList.size()" + textViewList.size());
}
//初始化
public void init() {
mFangGe = getAnyOneFangGe();
tv_name.setText(mFangGe.getName());
tv_efficacy.setText(mFangGe.getEfficacy());
targetContent = mFangGe.getContent();
initDrawableZi();
initDisplayContent();
initDisplay(displayContent);
display(displayContent);
}
public void flush(FangGe fangGe){
mFangGe = fangGe;
tv_name.setText(mFangGe.getName());
tv_efficacy.setText(mFangGe.getEfficacy());
targetContent = mFangGe.getContent();
initDrawableZi();
initDisplayContentByContent(mFangGe.getContent());
initDisplay(displayContent);
display(displayContent);
}
//清除显示内容
public void removeDisplay(List<TextView> textViewList){
for (int i = 0; i < textViewList.size(); i++) {
TextView textView = textViewList.get(i);
gl_content.removeView(textView);
}
}
// 通过方名获取一首方歌
public FangGe getOneFangGeByName(String name) {
FangGe fangGe = new FangGe();
//获取数据库帮助器
FangGeDBHelper helper = FangGeDBHelper.getInstance(this);
//开启读连接
helper.openReadLink();
List<FangGe> fangGeList = helper.queryByName(name);
for (int i = 0; i < fangGeList.size(); i++) {
fangGe = fangGeList.get(i);
}
//关闭连接
helper.closeLink();
return fangGe;
}
}
studyActivity
package com.example.recitefangge4.study;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.example.recitefangge4.R;
import com.example.recitefangge4.database.FangGeDBHelper;
import com.example.recitefangge4.entity.FangGe;
import org.w3c.dom.Text;
import java.util.ArrayList;
import java.util.List;
public class StudyActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private List<FangGe> mFangGeList = new ArrayList<>();
private MyAdapter mMyAdapter;
private FangGeDBHelper mHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_study);
mRecyclerView = findViewById(R.id.recycleview);
// TextView tv_test = findViewById(R.id.tv_test);
//
// String txt = "1234567891011";
// String s = formatText(txt, 5);
// tv_test.setText(s);
}
@Override
protected void onStart() {
super.onStart();
//获取数据库帮助器实例
mHelper = FangGeDBHelper.getInstance(this);
// 打开读写连接
mHelper.openReadLink();
mHelper.openWriteLink();
// 构造数据
List<FangGe> fangGeList = mHelper.queryAll();
Log.d("study", "onCreate: queryAll: fangList.size(): " + fangGeList.size());
for (int i = 0; i < fangGeList.size(); i++) {
FangGe fangGe = fangGeList.get(i);
Log.d("study3", "onStart: fangGe: " + fangGe.toString());
mFangGeList.add(fangGe);
}
// for (int i = 0; i < 50; i++) {
//
// FangGe fangGe = new FangGe();
// fangGe.setName("方名");
// fangGe.setEfficacy("功效");
// fangGe.setContent("内容");
// fangGe.setCategory("分类");
// mFangGeList.add(fangGe);
//
// }
Log.d("study2", "onCreate: mFangGeList.size" + mFangGeList.size());
mMyAdapter = new MyAdapter();
mRecyclerView.setAdapter(mMyAdapter);
// LinearLayoutManager layoutManager = new LinearLayoutManager(StudyActivity.this);
//装饰item
DividerItemDecoration mDivider = new DividerItemDecoration(this, DividerItemDecoration.HORIZONTAL);
GridLayoutManager layoutManager = new GridLayoutManager(StudyActivity.this, 1);
mRecyclerView.addItemDecoration(mDivider);
mRecyclerView.setLayoutManager(layoutManager);
}
@Override
protected void onStop() {
super.onStop();
// 关闭数据库连接
mHelper.closeLink();
}
private class MyAdapter extends RecyclerView.Adapter<MyViewHoder> {
@NonNull
@Override
public MyViewHoder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(StudyActivity.this).inflate(R.layout.itm_list, parent, false);
MyViewHoder myViewHoder = new MyViewHoder(view);
return myViewHoder;
}
@Override
public void onBindViewHolder(@NonNull MyViewHoder holder, int position) {
FangGe fangGe = mFangGeList.get(position);
Log.d("study", "onBindViewHolder: fangGe.toString(): " + fangGe.toString());
holder.tv_name.setText(fangGe.getName());
holder.tv_efficacy.setText(fangGe.getEfficacy());
//格式化显示内容
String content = fangGe.getContent();
content = formatText(content, 7);
holder.tv_content.setText(content);
}
@Override
public int getItemCount() {
return mFangGeList.size();
}
}
private class MyViewHoder extends RecyclerView.ViewHolder {
TextView tv_name;
TextView tv_efficacy;
TextView tv_content;
public MyViewHoder(@NonNull View itemView) {
super(itemView);
tv_name = itemView.findViewById(R.id.tv_name);
tv_efficacy = itemView.findViewById(R.id.study_tv_efficacy);
tv_content = itemView.findViewById(R.id.tv_content);
}
}
//格式化文本,限定每行字数
private String formatText(String text, int maxCharsPerLine) {
StringBuilder formattedText = new StringBuilder();
int index = 0;
while (index < text.length()) {
if (index + maxCharsPerLine < text.length()) {
formattedText.append(text.substring(index, index + maxCharsPerLine)).append("\n");
index += maxCharsPerLine;
} else {
formattedText.append(text.substring(index));
break;
}
}
return formattedText.toString();
}
}
FangGeUtil
package com.example.recitefangge4.study;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.example.recitefangge4.R;
import com.example.recitefangge4.database.FangGeDBHelper;
import com.example.recitefangge4.entity.FangGe;
import org.w3c.dom.Text;
import java.util.ArrayList;
import java.util.List;
public class StudyActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private List<FangGe> mFangGeList = new ArrayList<>();
private MyAdapter mMyAdapter;
private FangGeDBHelper mHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_study);
mRecyclerView = findViewById(R.id.recycleview);
// TextView tv_test = findViewById(R.id.tv_test);
//
// String txt = "1234567891011";
// String s = formatText(txt, 5);
// tv_test.setText(s);
}
@Override
protected void onStart() {
super.onStart();
//获取数据库帮助器实例
mHelper = FangGeDBHelper.getInstance(this);
// 打开读写连接
mHelper.openReadLink();
mHelper.openWriteLink();
// 构造数据
List<FangGe> fangGeList = mHelper.queryAll();
Log.d("study", "onCreate: queryAll: fangList.size(): " + fangGeList.size());
for (int i = 0; i < fangGeList.size(); i++) {
FangGe fangGe = fangGeList.get(i);
Log.d("study3", "onStart: fangGe: " + fangGe.toString());
mFangGeList.add(fangGe);
}
// for (int i = 0; i < 50; i++) {
//
// FangGe fangGe = new FangGe();
// fangGe.setName("方名");
// fangGe.setEfficacy("功效");
// fangGe.setContent("内容");
// fangGe.setCategory("分类");
// mFangGeList.add(fangGe);
//
// }
Log.d("study2", "onCreate: mFangGeList.size" + mFangGeList.size());
mMyAdapter = new MyAdapter();
mRecyclerView.setAdapter(mMyAdapter);
// LinearLayoutManager layoutManager = new LinearLayoutManager(StudyActivity.this);
//装饰item
DividerItemDecoration mDivider = new DividerItemDecoration(this, DividerItemDecoration.HORIZONTAL);
GridLayoutManager layoutManager = new GridLayoutManager(StudyActivity.this, 1);
mRecyclerView.addItemDecoration(mDivider);
mRecyclerView.setLayoutManager(layoutManager);
}
@Override
protected void onStop() {
super.onStop();
// 关闭数据库连接
mHelper.closeLink();
}
private class MyAdapter extends RecyclerView.Adapter<MyViewHoder> {
@NonNull
@Override
public MyViewHoder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(StudyActivity.this).inflate(R.layout.itm_list, parent, false);
MyViewHoder myViewHoder = new MyViewHoder(view);
return myViewHoder;
}
@Override
public void onBindViewHolder(@NonNull MyViewHoder holder, int position) {
FangGe fangGe = mFangGeList.get(position);
Log.d("study", "onBindViewHolder: fangGe.toString(): " + fangGe.toString());
holder.tv_name.setText(fangGe.getName());
holder.tv_efficacy.setText(fangGe.getEfficacy());
//格式化显示内容
String content = fangGe.getContent();
content = formatText(content, 7);
holder.tv_content.setText(content);
}
@Override
public int getItemCount() {
return mFangGeList.size();
}
}
private class MyViewHoder extends RecyclerView.ViewHolder {
TextView tv_name;
TextView tv_efficacy;
TextView tv_content;
public MyViewHoder(@NonNull View itemView) {
super(itemView);
tv_name = itemView.findViewById(R.id.tv_name);
tv_efficacy = itemView.findViewById(R.id.study_tv_efficacy);
tv_content = itemView.findViewById(R.id.tv_content);
}
}
//格式化文本,限定每行字数
private String formatText(String text, int maxCharsPerLine) {
StringBuilder formattedText = new StringBuilder();
int index = 0;
while (index < text.length()) {
if (index + maxCharsPerLine < text.length()) {
formattedText.append(text.substring(index, index + maxCharsPerLine)).append("\n");
index += maxCharsPerLine;
} else {
formattedText.append(text.substring(index));
break;
}
}
return formattedText.toString();
}
}
MyStringArrayUtil
package com.example.recitefangge4.Util;
import java.util.Random;
public class MyStringArrayUtil {
//生成乱序数组
public static String[] getOutOfOrderStringArray(String str){
String[] tmp = str.split("");
//乱序数组
String[] messString = new String[str.length()];
//获取乱序索引
Integer[] messIndex = getMessIndex(tmp.length);
for (int i = 0; i < str.length(); i++) {
int idx = messIndex[i];
messString[i] = tmp[idx];
}
return messString;
}
//生成指定个数乱序索引
public static Integer[] getMessIndex(Integer len) {
Integer[] messIndex = new Integer[len];
int start = 0;
while (start < len){
Random random = new Random();
int i = random.nextInt(len);
//判断该索引是否存在
Boolean exit = isExit(i, messIndex);
if(!exit){
//不存在, 存入乱序索引数组
messIndex[start++] = i;
}
}
return messIndex;
}
//该值是否存在于数组
public static Boolean isExit(int value, Integer[] arr){
for (int i = 0; i < arr.length; i++) {
if (arr[i] == Integer.valueOf(value)){
return true;
}
}
return false;
}
}
MainActivity
package com.example.recitefangge4;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import com.example.recitefangge4.collect.CollectActivity;
import com.example.recitefangge4.database.FangGeDBHelper;
import com.example.recitefangge4.modify.ModifyActivity;
import com.example.recitefangge4.recite.ReciteActivity;
import com.example.recitefangge4.study.StudyActivity;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private FangGeDBHelper mHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btn_recite).setOnClickListener(this);
findViewById(R.id.btn_collet).setOnClickListener(this);
findViewById(R.id.btn_study).setOnClickListener(this);
findViewById(R.id.btn_modify).setOnClickListener(this);
}
// @Override
// protected void onStart() {
// super.onStart();
// //获得数据库帮助器实例
// mHelper = FangGeDBHelper.getInstance(this);
//
// // 打开数据库帮助器的读写连接
// SQLiteDatabase mRDB = mHelper.openReadLink();
// SQLiteDatabase mWDB = mHelper.openWriteLink();
// }
//
// @Override
// protected void onStop() {
// super.onStop();
// // 关闭数据库连接
// mHelper.closeLink();
// }
@Override
public void onClick(View v) {
if(v.getId() == R.id.btn_recite){
Intent intent = new Intent(MainActivity.this, ReciteActivity.class);
startActivity(intent);
}
if(v.getId() == R.id.btn_collet) {
Intent intent = new Intent(MainActivity.this, CollectActivity.class);
startActivity(intent);
}
if(v.getId() == R.id.btn_study) {
Intent intent = new Intent(MainActivity.this, StudyActivity.class);
startActivity(intent);
}
if (v.getId() == R.id.btn_modify) {
Intent intent = new Intent(MainActivity.this, ModifyActivity.class);
startActivity(intent);
}
}
}
drawable_edit_text_bg
activity_collect.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".collect.CollectActivity"
android:orientation="vertical"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="5dp"
>
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="方名"/>
<EditText
android:id="@+id/et_name"
android:padding="10dp"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:hint="请输入方名"
android:background="@drawable/drawable_edit_text_bg"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="5dp"
>
<TextView
android:id="@+id/tv_efficacy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="功效"/>
<EditText
android:id="@+id/et_efficacy"
android:padding="10dp"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:hint="请输入功效"
android:background="@drawable/drawable_edit_text_bg"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="5dp"
>
<TextView
android:id="@+id/tv_category"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="分类"/>
<EditText
android:id="@+id/et_category"
android:padding="10dp"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:hint="请输入分类"
android:background="@drawable/drawable_edit_text_bg"
/>
</LinearLayout>
<TextView
android:id="@+id/tv_content"
android:layout_marginTop="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="内容"/>
<EditText
android:id="@+id/et_content"
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入内容"
android:background="@drawable/drawable_edit_text_bg"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<Button
android:id="@+id/btn_collet"
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="录入"/>
<Button
android:id="@+id/btn_reset"
android:layout_marginRight="10dp"
android:gravity="right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="重置"
/>
</RelativeLayout>
</LinearLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<Button
android:id="@+id/btn_recite"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="背诵方歌"/>
<Button
android:id="@+id/btn_collet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="录入方歌"/>
<Button
android:id="@+id/btn_study"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="学习方歌"/>
<Button
android:id="@+id/btn_modify"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="修改方歌"/>
</LinearLayout>
activity_modify.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".modify.ModifyActivity"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginBottom="10dp"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="5dp"
>
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="方名"/>
<EditText
android:id="@+id/et_name"
android:padding="10dp"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:hint="请输入方名"
android:background="@drawable/drawable_edit_text_bg"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="5dp"
>
<TextView
android:id="@+id/tv_efficacy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="功效"/>
<EditText
android:id="@+id/et_efficacy"
android:padding="10dp"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:hint="请输入功效"
android:background="@drawable/drawable_edit_text_bg"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="5dp"
>
<TextView
android:id="@+id/tv_category"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="分类"/>
<EditText
android:id="@+id/et_category"
android:padding="10dp"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:hint="请输入分类"
android:background="@drawable/drawable_edit_text_bg"
/>
</LinearLayout>
<TextView
android:id="@+id/tv_content"
android:layout_marginTop="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="内容"/>
<EditText
android:id="@+id/et_content"
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入内容"
android:background="@drawable/drawable_edit_text_bg"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<Button
android:id="@+id/btn_collet"
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="修改"/>
<Button
android:id="@+id/btn_reset"
android:layout_marginRight="10dp"
android:gravity="right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="重置"
/>
</RelativeLayout>
</LinearLayout>
activity_recite.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".recite.ReciteActivity"
android:orientation="vertical">
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:layout_marginBottom="20dp"
android:textSize="40dp"
android:text="方名"/>
<TextView
android:id="@+id/tv_eficacy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:layout_marginBottom="20dp"
android:textSize="30dp"
android:text="功效"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<GridLayout
android:id="@+id/gl_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:columnCount="7"
android:layout_centerInParent="true"
>
</GridLayout>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="30dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="20dp"
android:layout_marginLeft="20dp"
>
<EditText
android:id="@+id/et_designated_name"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:background="@drawable/drawable_edit_text_bg"
android:hint="请输入要背诵的方名"
android:layout_marginRight="10dp"
/>
<Button
android:id="@+id/btn_designated"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="背诵"
android:layout_marginRight="20dp"
/>
<Button
android:id="@+id/btn_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下一首"
android:layout_marginRight="20dp"
/>
</LinearLayout>
</LinearLayout>
activity_study.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".study.StudyActivity"
android:orientation="vertical"
android:gravity="center"
>
<!-- <TextView-->
<!-- android:layout_width="wrap_content"-->
<!-- android:layout_height="wrap_content"-->
<!-- android:id="@+id/tv_test"-->
<!-- />-->
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycleview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="5dp"
android:paddingBottom="5dp"
/>
</LinearLayout>
item_list.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="wrap_content"
android:orientation="vertical"
android:layout_marginTop="30dp">
<TextView
android:id="@+id/tv_name"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:textSize="30dp"
android:text="方名"
/>
<TextView
android:id="@+id/study_tv_efficacy"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:textSize="20dp"
android:text="功效"
/>
<TextView
android:id="@+id/tv_content"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:ellipsize="end"
android:maxLines="20"
android:textSize="25dp"
android:text="内容1234567890"
/>
</LinearLayout>
此项目尚不完善,待以后慢慢优化。。。。。