I'm trying to display a bus number in a textview in another activity when a user enters their location and destination. I can't find where I am making mistakes:
DatabaseOpenHelper.java
public class DatabaseOpenHelper extends SQLiteAssetHelper {
private static final String DATABASE_NAME = "CityBusManagementDatabase.db";
private static final int DATABASE_VERSION = 1;
public DatabaseOpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
}
DatabaseAccess.java
public class DatabaseAccess {
private SQLiteOpenHelper openHelper;
private SQLiteDatabase db;
private static DatabaseAccess instance;
Cursor c = null;
//private constructor so that object creation from outside the class is avoided
DatabaseAccess(Context context, Class<SearchResult> searchResultClass) {
this.openHelper = new DatabaseOpenHelper(context);
}
//to return the single instance of database
public static DatabaseAccess getInstance(Context context, Class<Dashboard> dashboardClass) {
if (instance == null) {
instance = new DatabaseAccess(context, SearchResult.class);
}
return instance;
}
//to open the database
public void open() {
this.db = openHelper.getWritableDatabase();
}
//closing the database connection
public void close() {
if (db != null) {
this.db.close();
}
}
public String getbus_number(String location, String destination) {
c = db.rawQuery("Select bus_number from abuja where " + location + "= ? AND " + destination + " = ?", new String[]{location, destination});
StringBuffer buffer = new StringBuffer();
while (c.moveToNext()) {
String bus_number = c.getString(0);
buffer.append(""+bus_number);
}
return buffer.toString();
}
}
}
Dashboard.java
public class Dashboard extends AppCompatActivity {
public EditText Location;
public EditText GoToInput;
public Button Search;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);
Location = findViewById(R.id.location);
GoToInput = findViewById(R.id.goToInput);
Search = findViewById(R.id.search);
Search.setOnClickListener(v -> {
DatabaseAccess databaseAccess = new DatabaseAccess(getApplicationContext(), SearchResult.class);
databaseAccess.open();
String l = Location.getText().toString();
String g = GoToInput.getText().toString();
});
}
}
SearchResult.java
public class SearchResult extends AppCompatActivity {
public TextView BusNumberOutput;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_result);
BusNumberOutput = findViewById(R.id.busNumberOutput);
DatabaseAccess databaseAccess = DatabaseAccess.getInstance(getApplicationContext(), Dashboard.class);
databaseAccess.open();
String bus_number = databaseAccess.getbus_number(0, 1);
// display the string into textView
BusNumberOutput.setText(bus_number);
}
}
activity_dashboard.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#E5E5E5"
tools:context=".Dashboard">
<ImageView
android:id="@+id/destination"
android:layout_width="180dp"
android:layout_height="230dp"
android:layout_marginRight="15dp"
android:layout_marginTop="105dp"
android:layout_marginLeft="15dp"
android:layout_marginBottom="15dp"
android:scaleType="fitXY"
app:srcCompat="@drawable/destinationpicture"/>
<TextView
android:id="@+id/welcome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/destination"
android:text="Welcome!"
android:textColor="@color/black"
android:textSize="27sp"
android:textStyle="bold"
android:layout_marginTop="165dp"
android:layout_marginBottom="15dp"
android:layout_marginLeft="25dp"/>
<TextView
android:id="@+id/essay2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="215dp"
android:layout_marginBottom="15dp"
android:layout_toRightOf="@+id/destination"
android:text="Let us help you find those bus numbers."
android:textAlignment="center"
android:textColor="@color/black"
android:textSize="16sp"
android:textStyle="normal" />
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/location"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginTop="450dp"
android:layout_marginEnd="32dp"
android:hint="Enter your location">
<!--this is the actual edit text which takes the input-->
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/location_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/goToInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginTop="550dp"
android:layout_marginEnd="32dp"
android:hint="Enter your destination">
<!--this is the actual edit text which takes the input-->
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/goToInput_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"/>
</com.google.android.material.textfield.TextInputLayout>
<Button
android:id="@+id/search"
android:layout_width="310dp"
android:layout_height="90dp"
android:layout_marginLeft="40dp"
android:layout_marginTop="700dp"
android:backgroundTint="#50C2C9"
android:text="Search"
android:textSize="20sp"/>
</RelativeLayout>
activity_search_result.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#E5E5E5"
tools:context=".SearchResult">
<ImageView
android:id="@+id/busStop"
android:layout_width="355dp"
android:layout_height="230dp"
android:layout_marginRight="15dp"
android:layout_marginTop="105dp"
android:layout_marginLeft="25dp"
android:layout_marginBottom="15dp"
android:scaleType="fitXY"
app:srcCompat="@drawable/busstoppicture" />
<TextView
android:id="@+id/busNumberIs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your Bus Number is:"
android:textColor="@color/black"
android:textSize="27sp"
android:textStyle="bold"
android:layout_marginTop="75dp"
android:layout_marginBottom="15dp"
android:layout_marginLeft="70dp"
android:textAlignment="center"/>
<TextView
android:id="@+id/busNumberOutput"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textColor="@color/black"
android:textSize="27sp"
android:textStyle="bold"
android:layout_marginTop="75dp"
android:layout_marginBottom="15dp"
android:layout_marginLeft="200dp"
android:textAlignment="center"/>
<TextView
android:id="@+id/thanks"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:layout_marginLeft="65dp"
android:text="Thank you for using CBMA!"
android:textAlignment="center"
android:textColor="@color/black"
android:textSize="21sp"
android:textStyle="normal" />
<Button
android:id="@+id/searchAgain"
android:layout_width="310dp"
android:layout_height="90dp"
android:layout_marginLeft="40dp"
android:layout_marginTop="20dp"
android:backgroundTint="#50C2C9"
android:text="Search Again"
android:textSize="20sp"/>
</LinearLayout>