Reputation: 1
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>
Upvotes: -1
Views: 41
Reputation: 56938
I can't find where I am making mistakes:
You need to learn how to debug. However, here's some pointers.
DatabaseOpenHelper
OK as it is
DatabaeAccess (see comments):-
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
/*<<<<<<<<<< Database should be distinct from UI so no need for UI classes being passed to constructors >>>>>>>>>>*/
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) {
/*<<<<<<<<<< column names should be specifically location and destination not variables location and destination resolved >>>>>>>>>>*/
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);
}
c.close(); /* SHOULD ALWAYS CLOSE CURSOR WHEN DONE WITH IT */
return buffer.toString();
}
}
Dashboard (see comments)
public class Dashboard extends AppCompatActivity {
public EditText Location;
public EditText GoToInput;
public Button Search;
public static final String INTENT_EXTRA_DASHBOARD_LOCATION = "IEDASHBOARDLOCATION"; /*<<<<<<<<<< ADDED >>>>>>>>>>*/
public static final String INTENT_EXTRA_DASHBOARD_DESTINATION = "IEDASHBOARDDESTINATION"; /*<<<<<<<<<< ADDED >>>>>>>>>>*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);
//Location = findViewById(R.id.location); /*<<<<<<<<<< WRONG ERROR SO COMMENTED OUT >>>>>>>>>>*/
Location = findViewById(R.id.location_text); /*<<<<<<<<<< IMPORTANT CHANGED >>>>>>>>>>*/
//GoToInput = findViewById(R.id.goToInput); /*<<<<<<<<<< WRONG ERROR SO COMMENTED OUT >>>>>>>>>>*/
GoToInput = findViewById(R.id.goToInput_text); /*<<<<<<<<<< IMPORTANT CHANGED >>>>>>>>>>*/
Search = findViewById(R.id.search);
Search.setOnClickListener(v -> {
/*<<<<<<<<<< NO need to access the database here so commented out >>>>>>>>>>*/
//DatabaseAccess databaseAccess = new DatabaseAccess(getApplicationContext()/*, SearchResult.class*/);
//databaseAccess.open();
//String l = Location.getText().toString();
//String g = GoToInput.getText().toString();
/*<<<<<<<<<< WHAT YOU NEED TO DO IS >>>>>>>>>>*/
/* PREPARE TO INVOKE THE SEARCHRESULTS PASSINg THE VALUES TO SEARCH RESULTS */
Intent intent = new Intent(getApplicationContext(),SearchResult.class); /* prepare an intent to start the searchresult activity */
intent.putExtra(INTENT_EXTRA_DASHBOARD_LOCATION,Location.getText().toString()); /* store the location input in an intent extra */
intent.putExtra(INTENT_EXTRA_DASHBOARD_DESTINATION,GoToInput.getText().toString()); /* store the destination input in an intent extra */
startActivity(intent); /* Start the SEARCHRESULT Activity that can then get the extras from the intent*/
});
}
}
SearchResult (see comments)
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();
/*<<<<<<<<<< CHANGED TO USE location and destination from the dashboard activity */
String bus_number = databaseAccess.getbus_number(
this.getIntent().getStringExtra(Dashboard.INTENT_EXTRA_DASHBOARD_LOCATION),
this.getIntent().getStringExtra(Dashboard.INTENT_EXTRA_DASHBOARD_DESTINATION)
);
// display the string into textView
BusNumberOutput.setText(bus_number);
}
}
Demo
So using a database that has the abuja table as per :-
And copying the database into the assets/databases folder of the project (after creating the folders) as per:-
In addition to the provided code MainActivity (Button defined in the layout) was:-
public class MainActivity extends AppCompatActivity {
Button dashboard_button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dashboard_button = this.findViewById(R.id.dashboard_button);
dashboard_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(),Dashboard.class);
startActivity(intent);
}
});
}
}
So when run then :-
Clicking the DASHBOARD button :-
Entering location 1 and destination 2 and clicking SEARCH :-
i.e. BUS012 as expected.
The display will be lacking the images as these have been removed.
Additional
In response to
but what if the location and destination are in text form not in integer form
As per the comment no issue at all.
As proof I added the following to MainActiviy:-
DatabaseAccess.getInstance(this).insert("BUSLONDON-NEWYORK","London","New York");
Along with the following additional method in DatabaseAccess :-
public long insert(String bus_number, String location, String destination) {
ContentValues cv = new ContentValues();
cv.put("bus_number",bus_number);
cv.put("location",location);
cv.put("destination",destination);
return instance.openHelper.getWritableDatabase().insert("abuja",null,cv);
}
Without any other changes the the App was run and London input as the location and New York as the destination resulting in:-
Upvotes: 0