今日完成地铁查询系统android端线路查询功能的开发,站点查询可参考此查询方式。但是此查询方式仍有不足,比如会遇到查询信息过长界面不能完全显示导致的软件闪退,期望再后面的开发中能通过添加滚动条的方式解决。
查询结果图:
查询代码:
1 package com.example.test1; 2 import android.content.Intent; 3 import android.database.Cursor; 4 import android.database.sqlite.SQLiteDatabase; 5 import android.os.Bundle; 6 import android.view.View; 7 import android.widget.Button; 8 import android.widget.EditText; 9 import android.widget.TextView; 10 import android.widget.Toast; 11 12 import androidx.appcompat.app.AppCompatActivity; 13 14 public class ScreachFirstActivity extends AppCompatActivity { 15 final Mysql dbHelper = new Mysql(this,"subway",null,2); 16 private Button searchButton; 17 private EditText search; 18 private TextView tv_show, tv_showx; 19 20 @Override 21 protected void onCreate(Bundle savedInstanceState) { 22 super.onCreate(savedInstanceState); 23 setContentView(R.layout.activity_screach_first); 24 searchButton = findViewById(R.id.btn_choice1); 25 search = findViewById(R.id.search); 26 tv_show = findViewById(R.id.tv_show); 27 tv_showx = findViewById(R.id.tv_showx); 28 searchButton.setOnClickListener(new View.OnClickListener() { 29 @Override 30 public void onClick(View v) { 31 SQLiteDatabase db = dbHelper.getWritableDatabase(); 32 String n_search = search.getText().toString(); 33 if (n_search.equals("")) { 34 myShow(); 35 db.close(); 36 } else { 37 tv_show.setText("线路号"); 38 tv_showx.setText("站名"); 39 Cursor cursor = db.rawQuery("select * from bj_subway where line_name = ? ", new String[]{n_search}); 40 41 while (cursor.moveToNext()) { 42 String l = cursor.getString(1); 43 String x = cursor.getString(2); 44 tv_show.setText(tv_show.getText() + "\n" + l); 45 tv_showx.setText(tv_showx.getText() + "\n" + x); 46 } 47 48 cursor.close(); 49 db.close(); 50 } 51 } 52 }); 53 } 54 public void myShow() { 55 SQLiteDatabase db = dbHelper.getReadableDatabase(); 56 tv_show.setText("线路号"); 57 tv_showx.setText("站名"); 58 Cursor cursor = db.rawQuery("select * from bj_subway", null); 59 60 while (cursor.moveToNext()) { 61 String l = cursor.getString(1); 62 String x = cursor.getString(2); 63 tv_show.setText(tv_show.getText() + "\n" + l); 64 tv_showx.setText(tv_showx.getText() + "\n" + x); 65 } 66 } 67 }
此代码在查询时遇到能够查询到数据但是下方数据库显示数据库未打开问题,此情况需要重启软件即可解决。
标签:总结,cursor,show,tv,星期三,第六周,showx,import,android From: https://www.cnblogs.com/sgle0722/p/17245772.html