今日共同报告-10-安卓端实现最短路径查询
今日共同报告-10
今天我完成了们完成了安卓端的最短路径查询,按照分工,这部分主要由烁玚来完成。
查询功能依然是使用SQL语句的递归来实现的,数据则是靠外部导入数据库,毕竟地铁的信息那么多,不可能靠自己输入。
今天的合作照片
效果展示:
部分代码:
SearchActivity
package com.example.dataapplication; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; public class SearchActivity extends AppCompatActivity implements View.OnClickListener{ private Button mBtn_search; private EditText eText_start,eText_end; private String start,end; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_search); mBtn_search = findViewById(R.id.btn_search); mBtn_search.setOnClickListener(this); eText_start = findViewById(R.id.et_start); eText_end = findViewById(R.id.et_end); } @Override public void onClick(View view) { start=eText_start.getText().toString(); end=eText_end.getText().toString(); if (view == findViewById(R.id.btn_search)){ Intent intent = new Intent(this,ShowActivity.class); intent.putExtra("start_text",start); intent.putExtra("end_text",end); startActivity(intent); } } }
ShowActivity
package com.example.dataapplication; import androidx.appcompat.app.AppCompatActivity; import android.annotation.SuppressLint; import android.content.Intent; import android.os.Bundle; import android.widget.TextView; import com.example.dataapplication.DataBase.CRUD; import com.example.dataapplication.Infomation.SubwayBean; public class ShowActivity extends AppCompatActivity { private TextView tv1,tv2,tv3,tv4; @SuppressLint("MissingInflatedId") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_show); tv1=findViewById(R.id.tv_1); tv2=findViewById(R.id.tv_2); tv3=findViewById(R.id.tv_3); tv4=findViewById(R.id.tv_4); Intent intent = getIntent(); String start = intent.getStringExtra("start_text"); String end = intent.getStringExtra("end_text"); CRUD crud = new CRUD(this); SubwayBean subwayBean = crud.BestTransferStation(start,end); tv1.setText(subwayBean._StartStation); tv2.setText(subwayBean._EndStation); tv3.setText((CharSequence) subwayBean._Number); tv4.setText(subwayBean._Path); } }
CRUD
package com.example.dataapplication.DataBase; import android.annotation.SuppressLint; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import com.example.dataapplication.Infomation.SubwayBean; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; public class CRUD { private DBHelper dbHelper; public CRUD (Context context){dbHelper = new DBHelper(context);} @SuppressLint("Range") public SubwayBean BestTransferStation(String station1, String station2){ SQLiteDatabase db = dbHelper.getReadableDatabase(); String sql = "WITH RECURSIVE transfer(start_station, stop_station, stops, paths) AS (\n" + "SELECT station_name, next_station, 1 stops, \n" + " line_name||station_name||'->'||line_name||next_station AS paths\n" + "FROM bj_subway\n" + "WHERE station_name = ?\n" + "UNION ALL \n" + "SELECT t.start_station, s.next_station, stops+1, paths||'->'||s.line_name||s.next_station\n" + "FROM transfer t\n" + "JOIN bj_subway s \n" + "ON (t.stop_station = s.station_name AND instr(paths, s.next_station)=0)\n" + ")\n" + "SELECT *\n" + "FROM transfer\n" + "WHERE stop_station = ?;"; Cursor cursor = db.rawQuery(sql,new String[]{station1,station2}); SubwayBean subwayBean = new SubwayBean(); if (cursor.moveToFirst()){ subwayBean._StartStation = cursor.getString(cursor.getColumnIndex("start_station")); subwayBean._EndStation = cursor.getString(cursor.getColumnIndex("stop_station")); subwayBean._Number = cursor.getString(cursor.getColumnIndex("stops")); subwayBean._Path = cursor.getString(cursor.getColumnIndex("paths")); } cursor.close(); db.close(); return subwayBean; } }
DBHelper
package com.example.dataapplication.DataBase; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class DBHelper extends SQLiteOpenHelper { private static final int DATABASE_VERSION=2; private static final String DATABASE_NAME="crud.db"; public DBHelper(Context context) { super(context,DATABASE_NAME,null,DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }