0

我已经被一个问题困扰了一天,但我仍然不知道该怎么做。我正在尝试在 Android 中制作一个 Google Maps 加油站查找器应用程序,我决定先制作,ListView然后再通过它LatitudeLongitude要绘制的值。

ListView我成功地用我的数据库中的数据填充了SQLite,但我似乎无法正确使用OnItemClickListener.

所以这是我的问题...

我似乎无法传递我单击的项目的LatitudeLongitude值。使用我当前的代码,每次单击某个项目时,应用程序都会关闭,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;
   }
  }

提前致谢。:)

4

2 回答 2

0

更改

Station stationList = (Station) list.getItemAtPosition(position);

LazyAdapter la = (LazyAdapter) list.getAdapter();
Station stationlist = (Station) la.getItemAtPosition(position); // *Convert HashMap to Station

*并且您必须从 HashMap 转换为 Station。如果您分享您的 Station 课程,我可以提供帮助。

最后,在您的 LazyAdapter 类中,您必须像这样添加 getItemAtPosition(int position) 方法:

public HashMap getItemAtPostion(int position){
     return data.get(position);
}
于 2012-08-14T08:56:36.877 回答
0

在您的适配器中,我发现以下内容:

public Object getItem(int position) {
    return position;
}

此方法应在提供的位置返回实际Station对象,而不是它的位置。

强制转换异常是因为您的方法返回一个 int ,运行时无法将其强制转换为Station对象。

所以,这里是你需要做的:

1-将实施更改getItem(int position)为:

public Station getItem(int position) {
    HashMap station_data = data.get(position);
    Station s = new Station(station_data);
    return s;
}

2- 在Station接受HashMap<String,String>.

于 2012-08-14T13:41:26.773 回答