import androidx.appcompat.app.AppCompatActivity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class AddDanciActivity extends AppCompatActivity {
private EditText wordedit;
private EditText yisiedit;
private Button add_btn;
private Button quxiao_btn;
private Button shanchu_btn;
private ListView listview;
private DBOpenHelper dbOpenHelper; //声明
@Override
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_add_danci);
dbOpenHelper = new DBOpenHelper(AddDanciActivity. this , "db_dict" , null , 1 ); //实例化,创建数据库
wordedit = (EditText) findViewById(R.id.addword_edit);
yisiedit = (EditText) findViewById(R.id.fanyiword_edit);
listview = (ListView) findViewById(R.id.add_list);
add_btn = (Button) findViewById(R.id.add_btn);
quxiao_btn = (Button) findViewById(R.id.quxiao_btn);
shanchu_btn = (Button) findViewById(R.id.shanchu_btn);
quxiao_btn.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(AddDanciActivity. this , "返回单词本主界面" , Toast.LENGTH_SHORT).show();
finish();
}
});
shanchu_btn.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
String word = wordedit.getText().toString();
String ys = yisiedit.getText().toString();
if (word.equals( "" )) {
Toast.makeText(AddDanciActivity. this , "填写的单词为空" , Toast.LENGTH_SHORT).show();
} else {
deleteData(dbOpenHelper.getReadableDatabase(), word);
Toast.makeText(AddDanciActivity. this , "删除成功" , Toast.LENGTH_SHORT).show();
}
}
});
add_btn.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
String word = wordedit.getText().toString();
String ys = yisiedit.getText().toString();
if (word.equals( "" ) || ys.equals( "" )) {
Toast.makeText(AddDanciActivity. this , "填写的单词或解释为空" , Toast.LENGTH_SHORT).show();
} else {
insertData(dbOpenHelper.getReadableDatabase(), word, ys); //插入生词
Toast.makeText(AddDanciActivity. this , "添加生词成功" , Toast.LENGTH_SHORT).show();
renew();
}
}
});
}
//插入数据的方法
private void insertData(SQLiteDatabase sqLiteDatabase, String word, String ys) {
ContentValues values = new ContentValues();
values.put( "word" , word); //保存单词
values.put( "detail" , ys);
sqLiteDatabase.insert( "tb_dict" , null , values); //执行插入操作
renew();
}
private void deleteData(SQLiteDatabase sqLiteDatabase, String word) {
ContentValues values = new ContentValues();
String[] args = {String.valueOf(word)};
sqLiteDatabase.delete( "tb_dict" , "word=?" , args); //执行删除操作
renew();
}
@Override
protected void onDestroy() {
super .onDestroy();
if (dbOpenHelper != null ) {
dbOpenHelper.close(); //关闭
}
}
public void renew() {
Cursor cursor = dbOpenHelper.getReadableDatabase().query( "tb_dict" , null , null , null , null , null , null );
ArrayList<Map<String, String>> resultList = new ArrayList<Map<String, String>>();
while (cursor.moveToNext()) {
Map<String, String> map = new HashMap<String, String>();
map.put( "word" , cursor.getString( 1 ));
map.put( "interpret" , cursor.getString( 2 ));
resultList.add(map);
}
if (resultList == null || resultList.size() == 0 ) {
Toast.makeText(AddDanciActivity. this , "很遗憾,没有相关记录!" , Toast.LENGTH_SHORT).show();
} else {
SimpleAdapter simpleAdapter = new SimpleAdapter(AddDanciActivity. this , resultList, R.layout.item, new String[]{ "word" , "interpret"
}, new int []{R.id.textView, R.id.textView2});
listview.setAdapter(simpleAdapter);
}
}
@Override
protected void onStart() {
super .onStart();
renew();
}
}
|