So I'm creating an application that will use current gps location (lat/long). The user will have to select a specified radius (.5-100 miles) from a Spinner. Then, when clicked, there will be a Button (onClickListener()) that will populate the locations from a SQLite database, in that set radius, to a ListView. From there, select an item from the ListView that will call an Activity (Intent).
I have the ListView populating data from the database, but I'm having issues with it populating from an onClick event (Button). Furthermore, how can I get the current location to compare the locations listed in the database based on the set radius (from the Spinner), and show the ones that match in the ListView. I would REALLY appreciate your suggestions/answers. Thanks!
SN: I have it displaying the current location. Here's my code.
Java File
import android.content.Context;
import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.net.Uri;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
public class MetersActivity extends ActionBarActivity {
private Toolbar toolbar;
private DrawerLayout drawerLayout;
private static Button search;
private TextView myAddress;
GPSTracker gps;
MeterInfoDatabase myDb;
private Geocoder geocoder = new Geocoder(this, Locale.ENGLISH);
final MeterInfoDatabase db = new MeterInfoDatabase(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_meters);
final ListView listView = (ListView) findViewById(R.id.MeterList);
Log.d("LOG", "Before testing the database");
List<MeterInfoSQL> allMeters = db.getAllMeters();
ArrayList<String> ar = new ArrayList<String>();
for(MeterInfoSQL m : allMeters)
{
String str = "Meter #: " + m.getMeterNumber();
ar.add(str);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, ar);
listView.setAdapter(adapter);
toolbar= (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
myAddress = (TextView) findViewById(R.id.myaddress);
onClickButtonListener();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_meters, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void onClickButtonListener() {
final Context context = this;
search = (Button) findViewById(R.id.MeterSearch_button);
search.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
gps = new GPSTracker(MetersActivity.this);
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();
if (gps.canGetLocation()) {
try {
List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);
if(addresses != null) {
Address returnedAddress = addresses.get(0);
StringBuilder strReturnedAddress = new StringBuilder("Your Current Address:\n");
for(int i=0; i<returnedAddress.getMaxAddressLineIndex(); i++) {
strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n");
}
myAddress.setText(strReturnedAddress.toString());
} else{
myAddress.setText("No Address returned!");
}
} catch (IOException e) {
e.printStackTrace();
myAddress.setText("Canont get Address!");
}
}
else {
gps.showSettingsAlert();
}
}
});
}
}
XML File
<android.support.v4.widget.DrawerLayout 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"
android:removed="@color/background"
tools:context="com.example.ataccofficial.MetersActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:removed="#ff8a8a90"
android:clickable="true"
tools:context="com.example.themetest.MetersActivity">
<include
android:id="@+id/app_bar"
layout="@layout/app_bar" />
<TextView
android:id="@+id/meterSelectRadiusText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="@+id/app_bar"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:inputType="none"
android:text="Select Radius:"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffffff"
android:textSize="25dp" />
<Spinner
android:id="@+id/meter_spinner"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/textView"
android:layout_below="@+id/meterSelectRadiusText"
android:layout_marginRight="10dp"
android:entries="@array/meter_array"
android:gravity="center"
android:spinnerMode="dialog"
android:textColor="#ffffffff" />
<TextView
android:id="@+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/meter_spinner"
android:layout_centerHorizontal="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="27dp"
android:removed="#ff8a8a90"
android:text="Location:"
android:textColor="#ffffffff"
android:textSize="15sp" />
<TextView
android:id="@+id/myaddress"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:autoText="false"
android:editable="false"
android:enabled="false"
android:focusable="true"
android:textColor="@color/primaryColor"
android:textSize="15sp" />
<Button
android:id="@+id/MeterSearch_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Customer"
android:id="@+id/lablecrm"
android:layout_alignBottom="@+id/dailyReportDivider"
android:layout_alignParentStart="true"
android:textColor="#ffffffff" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Issue"
android:id="@+id/lableIssue"
android:layout_alignBottom="@+id/dailyReportDivider"
android:layout_centerHorizontal="true"
android:textColor="#ffffffff" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Distance"
android:id="@+id/lableDistance"
android:layout_alignBottom="@+id/dailyReportDivider"
android:layout_alignParentEnd="true"
android:textColor="#ffffffff" />
<View
android:id="@+id/dailyReportDivider"
android:layout_width="fill_parent"
android:layout_height="2dp"
android:removed="@android:color/darker_gray"
android:layout_below="@+id/myaddress"
android:layout_alignParentStart="true"
android:layout_marginTop="84dp" />
<ListView
android:id="@+id/MeterList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/dailyReportDivider"
android:layout_centerHorizontal="true"
android:choiceMode="singleChoice"
android:textColor="#ffffffff"/>
</RelativeLayout>
<fragment
android:id="@+id/fragment_navigation_drawer"
android:name="com.example.ataccofficial.NavigationDrawerFragment"
android:layout_width="250dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:layout="@layout/fragment_navigation_drawer"
tools:layout="@layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>
String For Spinner
<string-array name="meter_array">
<item>.5 Miles</item>
<item>1 Miles</item>
<item>5 Miles</item>
<item>10 Miles</item>
<item>15 Miles</item>
<item>20 Miles</item>
<item>25 Miles</item>
<item>50 Miles</item>
<item>100 Miles</item>
</string-array>