如何从数据库中检索数据并在textview或toast中显示或在警报中显示任何内容?

时间:2017-05-24 05:02:26

标签: java android database sqlite

public class SearchActivity extends AppCompatActivity {
EditText recupdate;
Button update;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_search);

    recupdate = (EditText)findViewById(R.id.txtusername);
    update = (Button)findViewById(R.id.btnupdate);
    update.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view){

            SQLiteDatabase fuuast = openOrCreateDatabase("MyDb", MODE_PRIVATE, null);
            fuuast.execSQL("Create table if not exists std_rec(username varchar primary key, password varchar, age Integer, phone Integer)  ");
            String query = "Select * from std_rec Where username = '"+update.getText()+"'";
            fuuast.execSQL(query);


            Intent i = new Intent(getApplicationContext(), MainActivity.class);
            Toast.makeText(getApplicationContext(), "", Toast.LENGTH_SHORT).show();
            startActivity(i);
        }

    });
}

如何显示数据或在哪里显示请帮助我想在点击搜索按钮后显示数据抱歉我的英语我是初学者

1 个答案:

答案 0 :(得分:-1)

使用具有数据的查询制作光标

SQLiteDatabase db = openOrCreateDatabase("MyDb", MODE_PRIVATE, null);
String selectQuery = "SELECT * FROM std_rec";

Cursor cursor = db.rawQuery(selectQuery, null);
if(cursor != null) {
    cursor.moveToFirst();   
    startManagingCursor(cursor);
    for (int i = 0; i < cursor.getCount(); i++) {
        String message = cursor.getString(0);
        // Here display message as you like, in toast, alert or TextView
        Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
    }
}

希望这有帮助