つい先日は、ID検索の結果表示の件でお世話になりました。
あれから少し進めて、別画面に結果表示をするまでは上手くいったのですが、例外処理で躓いてしまいました。
idがSQliteに存在する → 別画面で結果表示
idがSQliteに存在しない → setText("データが存在しません")
としたいのですが、デバックで確認したところ
if(type != id){ のidが初期値1になっており、idが1以外だとデータが存在しませんと表示されてから別画面に映ってしまいます。この手の処理方法を調べてみたのですが探しきれなくて質問した次第です。
よろしくお願いします。
public class MainActivity extends Activity { int type; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button = (Button) findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { EditText editText = (EditText) findViewById(R.id.editText); String str = editText.getText().toString(); type = Integer.parseInt(str); DataBaseHelper dbHelper = new DataBaseHelper(MainActivity.this); SQLiteDatabase db = dbHelper.getReadableDatabase(); Cursor c = db.rawQuery("SELECT * from test", null); while (c.moveToNext()) { int index_id = c.getColumnIndex("id"); int index_name = c.getColumnIndex("name"); int index_company = c.getColumnIndex("company"); int id = c.getInt(index_id); String name = c.getString(index_name); String company = c.getString(index_company); if (type != id) { String string = "データが存在しません"; TextView textView = (TextView) findViewById(R.id.textView); textView.setText(string); //idが1以外だと表示されてしまう } else if (type == id) { Intent intent = new Intent(MainActivity.this, ResultActivity.class); intent.putExtra("id", id); intent.putExtra("name",name); intent.putExtra("company",company); startActivity(intent); } } } }); } }
回答2件
あなたの回答
tips
プレビュー