我已经被一个问题困扰了一天,但我仍然不知道该怎么做。我正在尝试在 Android 中制作一个 Google Maps 加油站查找器应用程序,我决定先制作,ListView
然后再通过它Latitude
和Longitude
要绘制的值。
ListView
我成功地用我的数据库中的数据填充了SQLite
,但我似乎无法正确使用OnItemClickListener
.
所以这是我的问题...
我似乎无法传递我单击的项目的Latitude
和Longitude
值。使用我当前的代码,每次单击某个项目时,应用程序都会关闭,LogCat 会给我一个java.lang.ClassCastException : java.lang.Integer
在线站stationList = (Station) list.getItemAtPosition(position);
(参见OnItemClickListener
代码)。
任何人都可以帮我通过我的纬度和经度ListView
吗?谢谢,麻烦您了。:)
NearGas活动
public class NearGasActivity extends Activity{
static final String KEY_NAME = "search"; // parent node
static final String KEY_ID = "id";
static final String KEY_STATNAME = "name";
static final String KEY_STATLOC = "location";
static final String KEY_LATITUDE = "latitude";
static final String KEY_LONGITUDE = "longitude";
LocationManager locationManager;
Location currentLocation = null;
GeoPoint currentPoint;
DatabaseHandler db = new DatabaseHandler(this);
double latPoint;
double lonPoint;
int currLatitude;
ListView list;
LazyAdapter adapter;
List<Station> listOfStations;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.nearme);
getLastLocation();
areaList(currentLocation);
}
public void areaList(Location location) {
listOfStations = db.getAllStations();
int currLatitude = (int) (location.getLatitude() * 1e6);
int currLongitude = (int) (location.getLongitude() * 1e6);
double diffLat;
double diffLon;
ArrayList<HashMap<String, String>> searchList = new ArrayList<HashMap<String, String>>();
for (Station b : listOfStations){
if ( b.getStationLocationLat() != null || b.getStationLocationLon() != null ) {
try {
latPoint = Double.parseDouble(b.getStationLocationLat());
lonPoint = Double.parseDouble(b.getStationLocationLon());
} catch (NumberFormatException e) {
}
}
diffLat = Math.abs(currLatitude - (latPoint * 1e6));
diffLon = Math.abs(currLongitude - (lonPoint * 1e6));
/* 2000 measurement units. Made-up. */
if (((diffLat) < 2000) && ((diffLon) < 2000)){
HashMap<String, String> map = new HashMap<String, String>();
map.put(KEY_ID, b.getStationCode());
map.put(KEY_STATNAME, db.getGasByCode(b.getStationGasCode()).getName());
map.put(KEY_STATLOC, b.getStationLocationDesc());
map.put(KEY_LATITUDE, b.getStationLocationLat());
map.put(KEY_LONGITUDE, b.getStationLocationLon());
songsList.add(map);
}
}
list = (ListView)findViewById(R.id.list);
adapter = new LazyAdapter(this, sList);
list.setAdapter(adapter);
// Click event for single list row
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Station stationList = (Station) list.getItemAtPosition(position);
double latitude = Double.parseDouble(stationList.getStationLocationLat());
double longitude = Double.parseDouble(stationList.getStationLocationLon());
Toast toast = Toast.makeText(NearGasActivity.this, "Test Message: " + lat, Toast.LENGTH_SHORT);
toast.show();
}
});
}
懒惰适配器
public class LazyAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.list_row, null);
TextView name = (TextView)vi.findViewById(R.id.name); // station name
TextView location = (TextView)vi.findViewById(R.id.location); // station location address
TextView latitude = (TextView)vi.findViewById(R.id.latitude);
TextView longitude = (TextView)vi.findViewById(R.id.longitude);
HashMap<String, String> search = new HashMap<String, String>();
search = data.get(position);
// Setting all values in listview
name.setText(search.get(NearGasActivity.KEY_STATNAME));
location.setText(search.get(NearGasActivity.KEY_STATLOC));
latitude.setText(search.get(NearGasActivity.KEY_LATITUDE));
longitude.setText(search.get(NearGasActivity.KEY_LONGITUDE));
return vi;
}
}
提前致谢。:)