0

我的光标中有一个字符。如何将其转换为字符串并在警报框中显示?

这是我的代码...

   public class MainActivity extends Activity {
    SQLiteDatabase db;
    String a,c;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        db=openOrCreateDatabase("dat", MODE_PRIVATE, null);
        db.execSQL("CREATE TABLE IF NOT EXISTS rk(num integer,alph char);");
        db.execSQL("INSERT INTO rk VALUES('1','a');");
        db.execSQL("INSERT INTO rk VALUES('2','b');");
        db.execSQL("INSERT INTO rk VALUES('3','c');");
        db.execSQL("INSERT INTO rk VALUES('4','d');");
        db.execSQL("INSERT INTO rk VALUES('5','e');");
    }

    public void disp(View view){
      EditText ed;
      ed=(EditText)findViewById(R.id.editText1);
      a=ed.getText().toString();
      Cursor b=db.rawQuery("SELECT alph FROM rk WHERE num='a';",null);
      c=b.getString(b.getColumnIndex("alph"));
      Builder builder=new AlertDialog.Builder(MainActivity.this);
      builder.setMessage(c);
      AlertDialog alert=builder.create();
      alert.show();

}
4

2 回答 2

0

游标对象已仅包含“alph”列中的值,您正尝试再次在此处查询索引 c=b.getString(b.getColumnIndex("alph"));。我认为这是造成问题的原因。

因此,您可以直接尝试打印光标的值,如下所示,

  c=b.getString(0);
于 2013-08-30T11:03:24.153 回答
0

在您的查询中,您使用的是 as 字符串而不是保存编辑文本值的 String 对象。所以更新您的查询如下,

a=ed.getText().toString();

Cursor b=db.rawQuery("SELECT alph FROM rk WHERE num='" + a +"'",null);

if (b.getCount() > 0 ) {
      c=b.getString(b.getColumnIndex("alph"));
      showMessage(c);
} else {
      showMessage("No record match");
}


private void showMessage(String msg) {
     Builder builder=new AlertDialog.Builder(MainActivity.this);
      builder.setMessage(msg);
      AlertDialog alert=builder.create();
      alert.show();
}
于 2013-08-30T11:01:59.617 回答