【发布时间】:2018-03-22 04:26:09
【问题描述】:
在我的应用程序中,我有源坐标(用户位置)和目标坐标的文本字段。我想知道我的数据库(巴士站)中是否有任何靠近这些坐标的记录。本质上,我想找到最接近用户位置和目的地位置的巴士站,因为我正在使用以下代码。
来源
public ArrayList<String> countDatabase(Double LAT, Double LNG) {
String sourcename=null;
double min_distance=99999999;
ArrayList<String> sList = new ArrayList<>();
SQLiteDatabase db = this.getWritableDatabase();
Cursor mCount= db.rawQuery("SELECT snme FROM halt_details WHERE slati BETWEEN '" +LAT+"- 0.1' AND '"+LAT+" + 0.1' AND slng BETWEEN '" +LNG+"- 0.1' AND '"+LNG+" + 0.1'", null);
mCount.moveToFirst();
while (mCount.moveToNext()) {
double Latitude = mCount.getDouble(2);
double Longitude = mCount.getDouble(3);
double dis=Math.sqrt( (Latitude-LAT)*(Latitude-LAT) + (Longitude-LNG)*(Longitude-LNG) );
if (dis<min_distance){
sourcename=mCount.getString(1);
sList.add(sourcename);
}
}
mCount.close();
return sList;
}
最后我得到一个数组列表(slist),其中包含使用欧拉距离公式计算的用户位置附近的所有站点的名称。将相同的过程应用于目的地以获取列表(dlist)。 现在我想知道该列表中的任何电台是否位于我的数据库中的同一行,为此
for(int i=0;i<ssize;i++){
String source = ssList.get(i);
for(int j=0;j<dsize;j++) {
String destination = ddList.get(j);
Boolean b = myDb.findBus(source, destination);
if(b){
TextView tvv = (TextView) findViewById(R.id.textView7);
tvv.setText(source+" and "+destination);
}
}
}
findBus
public Boolean findBus(String sourcename, String desname){
SQLiteDatabase db = this.getWritableDatabase();
Cursor mCount= db.rawQuery("select count(*) from search_table where name='" + sourcename + "' AND dname='"+desname+"'", null);
mCount.moveToFirst();
int count= mCount.getInt(0);
mCount.close();
if (count>0) return true;
else return false;
}
但我似乎无法运行此代码,遇到错误
Cursor mCount= db.rawQuery("SELECT snme FROM halt_details WHERE slati BETWEEN '" +LAT+"- 0.1' AND '"+LAT+" + 0.1' AND slng BETWEEN '" +LNG+"- 0.1' AND '"+LNG+" + 0.1'", null);
错误提示
java.lang.IllegalStateException: 无法为 android:onClick 执行方法
完整代码
数据库助手
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "Nearby.db";
public static final String TABLE_NAME = "halt_details";
public static final String COL_2 = "snme";
public static final String COL_3 = "slati";
public static final String COL_4 = "slong";
public static final String COL_5 = "dnme";
public static final String COL_6 = "dlati";
public static final String COL_7 = "dlong";
public static final String COL_8 = "buses";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_NAME + "(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, snme VARCHAR, slati DOUBLE, slong DOUBLE, dnme VARCHAR, dlati DOUBLE, dlong DOUBLE, buses VARCHAR)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public void insertData(String sname, Double slat, Double slng, String dname, Double dlat, Double dlng, String bus) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("snme",sname);
contentValues.put("slati",slat);
contentValues.put("slong",slng);
contentValues.put("dnme",dname);
contentValues.put("dlati",dlat);
contentValues.put("dlong",dlng);
contentValues.put("buses",bus);
db.insert(TABLE_NAME,null ,contentValues);
}
public void clearDatabase() {
SQLiteDatabase db = this.getWritableDatabase();
String clearDBQuery = "DELETE FROM "+TABLE_NAME;
db.execSQL(clearDBQuery);
}
public ArrayList<String> countDatabase(Double LAT, Double LNG) {
String sourcename=null;
double min_distance=99999999;
ArrayList<String> sList = new ArrayList<>();
SQLiteDatabase db = this.getWritableDatabase();
Cursor mCount= db.rawQuery("SELECT snme FROM halt_details WHERE slati BETWEEN '" +LAT+"- 0.1' AND '"+LAT+" + 0.1' AND slng BETWEEN '" +LNG+"- 0.1' AND '"+LNG+" + 0.1'", null);
mCount.moveToFirst();
while (mCount.moveToNext()) {
double Latitude = mCount.getDouble(2);
double Longitude = mCount.getDouble(3);
double dis=Math.sqrt( (Latitude-LAT)*(Latitude-LAT) + (Longitude-LNG)*(Longitude-LNG) );
if (dis<min_distance){
sourcename=mCount.getString(1);
sList.add(sourcename);
}
}
mCount.close();
return sList;
}
public ArrayList<String> countDatabase1(Double LAT, Double LNG) {
String destinationname=null;
ArrayList<String> dList = new ArrayList<>();
double min_distance=99999999;
SQLiteDatabase db = this.getWritableDatabase();
Cursor mCount= db.rawQuery("SELECT snme FROM halt_details WHERE slati BETWEEN '" +LAT+"- 0.1' AND '"+LAT+" + 0.1' AND slng BETWEEN '" +LNG+"- 0.1' AND '"+LNG+" + 0.1'", null);
mCount.moveToFirst();
while (mCount.moveToNext()) {
double Latitude = mCount.getDouble(2);
double Longitude = mCount.getDouble(3);
double dis=Math.sqrt( (Latitude-LAT)*(Latitude-LAT) + (Longitude-LNG)*(Longitude-LNG) );
if (dis<min_distance){
destinationname = mCount.getString(4);
dList.add(destinationname);
}
}
mCount.close();
return dList;
}
public Boolean findBus(String sourcename, String desname){
SQLiteDatabase db = this.getWritableDatabase();
Cursor mCount= db.rawQuery("select count(*) from search_table where name='" + sourcename + "' AND dname='"+desname+"'", null);
mCount.moveToFirst();
int count= mCount.getInt(0);
mCount.close();
if (count>0) return true;
else return false;
}
public Cursor getAllData() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("select * from " + TABLE_NAME, null);
return res;
}
}
Result.java
public class result extends Activity {
DatabaseHelper myDb;
@Override
protected void onCreate(Bundle savedInstanceState) {
myDb = new DatabaseHelper(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_result);
String url = getIntent().getExtras().getString("username");
String url1 = getIntent().getExtras().getString("username1");
TextView tv= (TextView)findViewById(R.id.textView2);
TextView tv1= (TextView)findViewById(R.id.textView5);
tv.setText(url);
tv1.setText(url1);
String[] separated = url.split("_");
String srclat = separated[0];
Double dsrclat= Double.parseDouble(srclat);
String srclng = separated[1];
Double dsrclng= Double.parseDouble(srclng);
String[] separated1 = url.split("_");
String deslat = separated1[0];
Double ddeslat= Double.parseDouble(deslat);
String deslng = separated1[1];
Double ddeslng= Double.parseDouble(deslng);
}
public void Database(View view){
Intent intent = new Intent(result.this, feeder.class);
startActivity(intent);
}
public void NearestValue(View view){
TextView tv= (TextView)findViewById(R.id.textView2);
String s1 = tv.getText().toString();
String[] separated = s1.split("_");
String deslat = separated[0];
Double Lat= Double.parseDouble(deslat);
String deslng = separated[1];
Double Lng= Double.parseDouble(deslng);
ArrayList<String> ssList = new ArrayList<>();
ssList = myDb.countDatabase(Lat,Lng);
int ssize = ssList.size();
TextView tvvv = (TextView) findViewById(R.id.textView8);
for(int i=0;i<ssize;i++) {
tvvv.setText(ssList.get(i));
}
TextView tv1= (TextView)findViewById(R.id.textView5);
String s11 = tv1.getText().toString();
String[] separated1 = s11.split("_");
String deslat1 = separated1[0];
Double Lat1= Double.parseDouble(deslat1);
String deslng1 = separated1[1];
Double Lng1= Double.parseDouble(deslng1);
ArrayList<String> ddList = new ArrayList<>();
ddList = myDb.countDatabase1(Lat1,Lng1);
int dsize = ddList.size();
TextView tvvvv = (TextView) findViewById(R.id.textView9);
for(int i=0;i<dsize;i++) {
tvvv.setText(ddList.get(i));
}
for(int i=0;i<ssize;i++){
String source = ssList.get(i);
for(int j=0;j<dsize;j++) {
String destination = ddList.get(j);
Boolean b = myDb.findBus(source, destination);
if(b){
TextView tvv = (TextView) findViewById(R.id.textView7);
tvv.setText(source+" and "+destination);
}
}
}
}
}
结果,xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="136dp"
android:layout_marginTop="84dp"
android:text="TextView"
android:textColor="@color/colorAccent" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/textView2"
android:layout_below="@+id/textView2"
android:layout_marginTop="97dp"
android:text="TextView"
android:textColor="@color/colorAccent" />
<TextView
android:id="@+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/nearest"
android:layout_marginEnd="14dp"
android:layout_marginTop="88dp"
android:layout_toStartOf="@+id/nearest"
android:textColor="@color/colorAccent"
android:textSize="30sp"
tools:text="tv" />
<Button
android:id="@+id/nearest"
android:layout_width="130sp"
android:layout_height="50dp"
android:onClick="NearestValue"
android:layout_alignStart="@+id/gotodb"
android:layout_alignTop="@+id/gotodb"
android:layout_marginTop="76dp"
android:background="@drawable/buttonshape"
android:shadowColor="#6C74A8"
android:shadowDx="6"
android:shadowDy="0"
android:shadowRadius="9"
android:text="nearest bus stop"
android:textColor="#43456B"
android:textColorLink="@android:color/holo_blue_bright"
android:textSize="15sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.95" />
<TextView
android:id="@+id/gotodb"
android:layout_width="130sp"
android:layout_height="50dp"
android:layout_alignParentEnd="true"
android:layout_below="@+id/textView5"
android:onClick="Database"
android:text="database"
android:textColor="@android:color/background_light"
android:textColorLink="@android:color/holo_blue_bright"
android:textSize="15sp" />
<TextView
android:id="@+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/nearest"
android:layout_marginEnd="38dp"
android:layout_marginTop="20dp"
android:layout_toStartOf="@+id/textView5"
android:text="TextView" />
<TextView
android:id="@+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/textView8"
android:layout_alignTop="@+id/textView7"
android:layout_marginTop="31dp"
android:text="TextView" />
【问题讨论】:
-
显示完整的堆栈跟踪。
-
实际上我解决了该错误,但现在我面临编辑问题中提到的另一个困难。顺便说一下,“仅显示选定的应用程序”的堆栈跟踪很清楚。
标签: java android sql sqlite relational-database