我需要从EditText传递一个参数,当我点击按钮时,它会启动另一个活动并获取该参数并将其传递给查询。它如下:
final Button ara = (Button) findViewById(R.id.maddebutton);
ara.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String maddeno = madde.getText().toString(); //madde is my EditText
Intent intent = new Intent(Anayasa.this, MaddeBul.class);
intent.putExtra("maddeler", maddeno);
startActivity(intent);
}
});
我的第二堂课如下:
Intent intent = getIntent();
int maddebul = intent.getIntExtra("maddeler", 0); //I don't want to set a default value but it pushes me
try {
Cursor cursor = FetchRecord(db.query("anayasa", SELECT,
"no = maddebul", null, null, null, null));
ShowRecord(cursor);
} finally {
database.close();
}
我的FetchRecord(Cursor c)
和ShowRecord(Cursor cursor)
功能正常,因为我在其他课程中使用它们。有"不"在我的" anayasa"保存整数值的数据库。
在LogCat上,它说"没有列像maddebul"。确实,没有。它假设是:
SELECT * FROM anayasa WHERE no = maddebul; //as sql command
任何帮助?
答案 0 :(得分:2)
您在此处添加额外的字符串:
intent.putExtra("maddeler", maddeno);
但是当您尝试检索额外内容时,您将其检索为int:
int maddebul = intent.getIntExtra("maddeler", 0);
尝试使用此insteald
String maddebul = intent.getStringExtra("maddeler", "");
此处参考的是getStringExtra() method的文档。
答案 1 :(得分:1)
On LogCat, it says "no column such maddebul". It is true, there isn't.
由于其中的条款“no = maddebul”,maddebul不是变量,它是字符串部分所以更改whereClause以获取它的值
Cursor cursor = FetchRecord(db.query("anayasa", SELECT,
"no = maddebul", null, null, null, null));
应该是
Cursor cursor = FetchRecord(db.query("anayasa", SELECT,
"no = "+maddebul, null, null, null, null));
答案 2 :(得分:0)
你正在双引号中写下maddebul。
替换
Cursor cursor = FetchRecord(db.query("anayasa", SELECT,
"no = maddebul", null, null, null, null));
通过
Cursor cursor = FetchRecord(db.query("anayasa", SELECT,
"no = "+maddebul, null, null, null, null));
更好地使用
Cursor cursor = FetchRecord(db.query("anayasa", SELECT,
"no = ?",new String[]{String.valueOf(maddebul)}, null, null, null));
答案 3 :(得分:0)
你把Extra作为字符串并将其作为整数 - 也许这就是问题?
答案 4 :(得分:0)
存在多个问题:
你把maddebul
作为字符串放在这里,所以需要像这样:
String maddebul = intent.getStringExtra("maddeler", "");
此外,您正在构建错误的SQL查询,maddebul
作为字符串传递给数据库,而您实际上想要传递该变量的值。所以它可能是这样的:
Cursor cursor = FetchRecord(db.query("anayasa", SELECT,
"no = ?",new String[]{maddebul}, null, null, null));