我想用输入的id号查看输入的文本区域。但我的应用程序正在停止。
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "user";
public static final String TABLE_NAME = "userTable";
public static final String COL1 = "ID";
public static final String COL2 = "NAME";
public static final String COL3 = "EMAIL";
public static final String COL4 = "TV";
public DatabaseHelper(@Nullable Context context) {
super(context, DATABASE_NAME, null , 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
String createTable = "CREATE TABLE "+ TABLE_NAME+ "(ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
" NAME VARCHAR , EMAIL VARCHAR , TV VARCHAR)";
db.execSQL(createTable);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String dropTable = "DROP TABLE IF EXİSTS "+ TABLE_NAME;
db.execSQL(dropTable);
onCreate(db);
}
public boolean addData(String name,String email,String tvShow){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL2,name);
contentValues.put(COL3,email);
contentValues.put(COL4,tvShow);
long result = db.insert(TABLE_NAME,null,contentValues);
if(result == -1 ){
return false;
}else{
return true;
}
}
public Cursor showData(){
SQLiteDatabase db = this.getWritableDatabase();
Cursor data = db.rawQuery("SELECT *FROM "+TABLE_NAME,null);
return data;
}
public Cursor SecileniGetir(String gelen){
SQLiteDatabase db = this.getWritableDatabase();
Cursor data = db.rawQuery("SELECT *FROM " + TABLE_NAME + " WHERE ID = "+ gelen,null );
return data;
}
public boolean Update(String id,String name , String email,String tvShow){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL1,id);
contentValues.put(COL2,name);
contentValues.put(COL3,email);
contentValues.put(COL4,tvShow);
if(COL1.toString()==""){
return false;
}else {
db.update(TABLE_NAME, contentValues, "ID = ?", new String[]{id});
return true;
}
}
}
我创造了一个 DatabaseHelper
班级。我创造了 dataAdd
功能, dataUpgrade
函数,我想在用户输入id时创建一个函数,在自己的edittext区域中查看其他信息。
public class MainActivity extends AppCompatActivity {
DatabaseHelper dbHelper;
private EditText etName,etMail,etFavorite,etId;
private Button addDataButton,viewData,update;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbHelper = new DatabaseHelper(this);
etName = findViewById(R.id.etName);
etId = findViewById(R.id.etId);
update = findViewById(R.id.update);
etMail = findViewById(R.id.etEmail);
etFavorite = (EditText) findViewById(R.id.etFavorite);
addDataButton = (Button) findViewById(R.id.addData);
viewData = (Button) findViewById(R.id.viewData);
AddData();
SecileniGoster();
Update();
}
public void AddData(){
addDataButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String email = etMail.getText().toString();
String name = etName.getText().toString();
String favorite = etFavorite.getText().toString();
if(email.equals("")||name.equals("")||favorite.equals("")){
Toast.makeText(MainActivity.this, "Lütfen Tüm alanları Doldurunuz!! Kayıt Başarısız", Toast.LENGTH_SHORT).show();
}else {
boolean insert = dbHelper.addData(name, email, favorite);
if (insert == true) {
Toast.makeText(MainActivity.this, "Başarılı", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Başarısız", Toast.LENGTH_SHORT).show();
}
}
}
});
}
public void ViewData(){
viewData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Cursor data = dbHelper.showData();
if(data.getCount() == 0){
//message
showMessage("Error","Data Not Found!!!");
}
StringBuffer buffer = new StringBuffer();
while(data.moveToNext()){
buffer.append("ID" + data.getString(0) + "\n");
buffer.append("Name : " + data.getString(1)+"\n");
buffer.append("Email : " + data.getString(2)+"\n");
buffer.append("Favorite Ch : "+ data.getString(3)+"\n");
buffer.append("\n");
showMessage("All Stored Data ", buffer.toString());
}
}
});
}
public void SecileniGoster(){
viewData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String id = etId.getText().toString();
Cursor data = dbHelper.SecileniGetir(id);
etName.setText(data.getString(1));
etMail.setText(data.getString(2));
etFavorite.setText(data.getString(3));
}
});
}
public void showMessage(String title, String message){
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle(title);
alert.setCancelable(true);
alert.setMessage(message);
alert.show();
}
public void Update(){
update.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String email = etMail.getText().toString();
String name = etName.getText().toString();
String favorite = etFavorite.getText().toString();
String id = etId.getText().toString();
int getID = etId.getText().toString().length();
if(getID>0){
boolean update = dbHelper.Update(id,name,email,favorite);
if(update == true){
Toast.makeText(MainActivity.this, "Succesfuly updated", Toast.LENGTH_SHORT).show();
etName.setText("");
etMail.setText("");
etFavorite.setText("");
}else{
Toast.makeText(MainActivity.this, "Unfortunatelly", Toast.LENGTH_SHORT).show();
}
}else{
Toast.makeText(MainActivity.this, "Girilen Id bulunamadi", Toast.LENGTH_SHORT).show();
}
}
});
}
}
结果
结果输出
2020-07-15 14:35:09.398 24815-24815/com.tcoding.sqlitesaveuserdata E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.tcoding.sqlitesaveuserdata, PID: 24815
android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
at android.database.AbstractCursor.checkPosition(AbstractCursor.java:515)
at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:138)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:52)
at com.tcoding.sqlitesaveuserdata.MainActivity$3.onClick(MainActivity.java:102)
at android.view.View.performClick(View.java:7125)
at android.view.View.performClickInternal(View.java:7102)
at android.view.View.access$3500(View.java:801)
at android.view.View$PerformClick.run(View.java:27336)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
暂无答案!
目前还没有任何答案,快来回答吧!