从 sqlite 数据库中搜索并在 EditText 中显示记录导致 crash/restart,哪里出了问题?
Searching from sqlite database and displaying record in EditText causes crash/restart, what went wrong?
我在 android 工作室中有一个 crud 应用程序项目。我已经完成了插入、更新、删除和查看所有操作。我唯一出错的是搜索功能。搜索代码对我来说看起来不错,所以如果你能帮我找到我的错误就太好了。谢谢!
table 是这样创建的:
public void onCreate(SQLiteDatabase DB) {
DB.execSQL("create Table ProdDetails(id INTEGER primary key, name TEXT, description TEXT, price NUMERIC, quantity INTEGER)");
}
搜索方法,接受来自已转换为 int 的 EditText 的输入:
public Cursor searchData (int id)
{
SQLiteDatabase DB = this.getWritableDatabase();
Cursor cursor = DB.rawQuery("Select * from ProdDetails where id = ?", new String[]{String.valueOf(id)});
return cursor;
}
主要class:
public class RUDActivity extends AppCompatActivity {
EditText prodId, name, description, price, quantity;
Button update, delete, view, search;
DBHelper DB;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_retrieve);
prodId = (EditText) findViewById(R.id.prodId);
name = (EditText) findViewById(R.id.name);
description = (EditText) findViewById(R.id.description);
price = (EditText) findViewById(R.id.price);
quantity = (EditText) findViewById(R.id.quantity);
search = findViewById(R.id.btnSearch);
DB = new DBHelper(this);
search.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int prodTXT = Integer.parseInt(prodId.getText().toString());
Cursor res = DB.searchData(prodTXT);
if(res.getCount()==0){
Toast.makeText(RUDActivity.this, "Product ID Not Found", Toast.LENGTH_SHORT).show();
return;
}
// app crash/restart after this line, am I doing this wrong?
prodId.setText("Product ID: "+res.getString(0));
name.setText("Name: "+res.getString(1));
description.setText("Description: "+res.getString(2));
price.setText("Price: "+res.getString(3));
quantity.setText("Quantity: "+res.getString(4));
}
});
}
}
当我搜索不存在的 prodId 时,它会显示 toast 消息“未找到产品 ID”并且该应用不会 crash/restart。但是,如果我搜索一个确实存在的 prodId,应用程序就会崩溃。
在检索列的值之前,您缺少对方法 moveToFirst()
的调用:
res.moveToFirst();
prodId.setText("Product ID: "+res.getString(0));
................................................
因为游标的索引最初位于结果的第一行之前。
或者,不使用 getCount()
检查游标是否返回任何行,而是使用 moveToFirst()
:
if (!res.moveToFirst()) {
Toast.makeText(RUDActivity.this, "Product ID Not Found", Toast.LENGTH_SHORT).show();
} else {
prodId.setText("Product ID: "+res.getString(0));
................................................
}
我在 android 工作室中有一个 crud 应用程序项目。我已经完成了插入、更新、删除和查看所有操作。我唯一出错的是搜索功能。搜索代码对我来说看起来不错,所以如果你能帮我找到我的错误就太好了。谢谢!
table 是这样创建的:
public void onCreate(SQLiteDatabase DB) {
DB.execSQL("create Table ProdDetails(id INTEGER primary key, name TEXT, description TEXT, price NUMERIC, quantity INTEGER)");
}
搜索方法,接受来自已转换为 int 的 EditText 的输入:
public Cursor searchData (int id)
{
SQLiteDatabase DB = this.getWritableDatabase();
Cursor cursor = DB.rawQuery("Select * from ProdDetails where id = ?", new String[]{String.valueOf(id)});
return cursor;
}
主要class:
public class RUDActivity extends AppCompatActivity {
EditText prodId, name, description, price, quantity;
Button update, delete, view, search;
DBHelper DB;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_retrieve);
prodId = (EditText) findViewById(R.id.prodId);
name = (EditText) findViewById(R.id.name);
description = (EditText) findViewById(R.id.description);
price = (EditText) findViewById(R.id.price);
quantity = (EditText) findViewById(R.id.quantity);
search = findViewById(R.id.btnSearch);
DB = new DBHelper(this);
search.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int prodTXT = Integer.parseInt(prodId.getText().toString());
Cursor res = DB.searchData(prodTXT);
if(res.getCount()==0){
Toast.makeText(RUDActivity.this, "Product ID Not Found", Toast.LENGTH_SHORT).show();
return;
}
// app crash/restart after this line, am I doing this wrong?
prodId.setText("Product ID: "+res.getString(0));
name.setText("Name: "+res.getString(1));
description.setText("Description: "+res.getString(2));
price.setText("Price: "+res.getString(3));
quantity.setText("Quantity: "+res.getString(4));
}
});
}
}
当我搜索不存在的 prodId 时,它会显示 toast 消息“未找到产品 ID”并且该应用不会 crash/restart。但是,如果我搜索一个确实存在的 prodId,应用程序就会崩溃。
在检索列的值之前,您缺少对方法 moveToFirst()
的调用:
res.moveToFirst();
prodId.setText("Product ID: "+res.getString(0));
................................................
因为游标的索引最初位于结果的第一行之前。
或者,不使用 getCount()
检查游标是否返回任何行,而是使用 moveToFirst()
:
if (!res.moveToFirst()) {
Toast.makeText(RUDActivity.this, "Product ID Not Found", Toast.LENGTH_SHORT).show();
} else {
prodId.setText("Product ID: "+res.getString(0));
................................................
}