gpt4 book ai didi

android - arraylist> 不工作

转载 作者:行者123 更新时间:2023-11-30 03:12:09 25 4
gpt4 key购买 nike

我想知道,这两天我做错了什么。我启动以下 AsyncTask GetLocationsAsync.java 以将 HttpPost 发送到服务器上的 PHP 文件,

public class GetLocationsAsyncTask extends AsyncTask<Map<String, String>, Object, ArrayList<HashMap<String, String>>>
{
public static final String USER_CURRENT_LAT = "user_current_lat";
public static final String USER_CURRENT_LONG = "user_current_long";
public static final String USER_CURRENT_ADDRESS = "user_current_address";
private Exception exception;
private ProgressDialog progressDialog;
private Context MainContext;
private GoogleMap mMap;
private MapActivity activity;

public GetLocationsAsyncTask(MapActivity activity, Context con)
{
super();
this.activity = activity;
this.MainContext = con;

}

public void onPreExecute()
{
progressDialog = new ProgressDialog(MainContext);
progressDialog.setMessage("Loading....");
progressDialog.show();
}

@Override
public void onPostExecute(ArrayList<HashMap<String, String>> result)
{
progressDialog.dismiss();
if (exception == null)
{
activity.loadLocations(result);
}
else
{
processException();
}
}

@Override
protected ArrayList<HashMap<String, String>> doInBackground(Map<String, String>... params)
{
Map<String, String> paramMap = params[0];
try
{
double PLat = Double.valueOf(paramMap.get(USER_CURRENT_LAT));
double PLong = Double.valueOf(paramMap.get(USER_CURRENT_LONG));
String Addr = new String(paramMap.get(USER_CURRENT_ADDRESS));
Locq5ionsParser Lp = new LocationsParser();
Document doc = Pp.getDocument(Addr, PLat, PLong, "Bar");
ArrayList<HashMap<String, String>>LocationPoints = Lp.getDirection(doc);
return LocationPoints;
}
catch (Exception e)
{
exception = e;
Log.d("GetLocations", e.getMessage());
return null;
}
}

private void processException()
{
Toast.makeText(MainContext, "Fehler beim Auslesen der XML", Toast.LENGTH_LONG).show();
Log.d("GetLocationAsync", exception.getLocalizedMessage());
}



}


public class LocationParser {
public LocationParser() {
}

public Document getDocument(String addr, double latitude, double longitude, String type) {
String url = "http://**********/xmlphpshow.php";

try {
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost(url);
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>(4);
postParameters.add(new BasicNameValuePair("Addresse", addr
.toString()));
postParameters.add(new BasicNameValuePair("latitude", String.valueOf(latitude)
.toString()));
postParameters.add(new BasicNameValuePair("longitude", String.valueOf(longitude)
.toString()));
postParameters.add(new BasicNameValuePair("type", "Bar"
.toString()));
httpPost.setEntity(new UrlEncodedFormEntity(postParameters));
HttpResponse response = httpClient.execute(httpPost, localContext);
InputStream in = response.getEntity().getContent();
DocumentBuilder builder = DocumentBuilderFactory.newInstance()
.newDocumentBuilder();
Document doc = builder.parse(in);
return doc;
} catch (Exception e) {
Log.d("LocationParser", e.getMessage());
e.printStackTrace();
return null;
}

}


public ArrayList<HashMap<String, String>> getDirection(Document DirektDoc) {

ArrayList<HashMap<String, String>> LocationItems = new ArrayList<HashMap<String, String>>();
NodeList nl, nl2, nl3;
// getting XML
if (DirektDoc.equals(null)){
Log.d("Dokument", "doc ist leer");
return null;
}
try{
nl = DirektDoc.getElementsByTagName("marker");
}catch(Exception e){
Log.d("TagName Markers", e.getMessage());
return null;
}


// looping through all item nodes <item>
HashMap<String, String> map = new HashMap<String, String>();
for (int i = 0; i < nl.getLength(); i++) {
// creating new HashMap
try{
Node node1 = nl.item(i);
nl3 = node1.getChildNodes();
Node AddrNode = nl3.item(getNodeIndex(nl3, "address"));
String PAddr = AddrNode.getTextContent();

//the Log shows that the addresses of each marker item was read correctly
Log.d("LocationParser","Adresse :" + PAddr);



map.put("Addresse", PAddr);
Node latNode = nl3.item(getNodeIndex(nl3, "lat"));
String lat = latNode.getTextContent();
map.put("latitude", lat);
Node lngNode = nl3.item(getNodeIndex(nl3, "lng"));
String lng = lngNode.getTextContent();
map.put("longitude", lng);
Node typeNode = nl3.item(getNodeIndex(nl3, "type"));
String LocType = typeNode.getTextContent();
map.put("Type", LocType);
Node timeNode = nl3.item(getNodeIndex(nl3, "tstamp"));
String timeString = timeNode.getTextContent();
map.put("TimeStamp", timeString);


LocationItems.add(map);
}catch(Exception e){
Log.d("LocationParser", e.getMessage());
}
}

return LocationItems;
}


private int getNodeIndex(NodeList NodeInl, String nodename) {
for (int i = 0; i < NodeInl.getLength(); i++) {
if (NodeInl.item(i).getNodeName().equals(nodename)){
return i;
}

}
return -1;
}
}

当我现在尝试读取 ArrayList 中的 Hashmap 值并在我的 GoogleMap 上创建一个标记时,只有一个标记可见。

public class MapActivity {
public GoogleMap googleMap;
public MapFragment mapfrag;
public Context MapContext;
public Document dc;
public MarkerOptions startMarker, ParkMarker;
public MapActivity(MapFragment gmapfrag, Context con){
this.mapfrag = gmapfrag;
this.googleMap = mapfrag.getMap();
this.MapContext = con;
}
public void initilizeMap() {
try {
googleMap.clear();
googleMap.getUiSettings().setRotateGesturesEnabled(true);
googleMap.getUiSettings().setCompassEnabled(true);


if (googleMap == null) {
Toast.makeText(MapContext, "Karte konnte nicht erstellt werden. Versuchen
sie es zu einem Späteren Zeitpunkt erneut.", Toast.LENGTH_SHORT).show();
}
}catch(Exception e){
Toast.makeText(MapContext, "Karte konnte nicht erstellt werden. Versuchen
sie es zu einem Späteren Zeitpunkt erneut.", Toast.LENGTH_SHORT).show();
}
return;
}
public void loadMarkers(double Mylati, double Mylongi, String aDdR){
googleMap.clear();
startMarker = new MarkerOptions().position(new
LatLng(Mylati,Mylongi)).title("Aktuelle Position");
startMarker.icon(BitmapDescriptorFactory.fromResource(R.drawable.start_icon));
googleMap.addMarker(startMarker);
Map<String, String> map = new HashMap<String, String>();
map.put(GetLocationsAsyncTask.USER_CURRENT_LAT, String.valueOf(Mylati));
map.put(GetLocationsAsyncTask.USER_CURRENT_LONG, String.valueOf(Mylongi));
map.put(GetLocationsAsyncTask.USER_CURRENT_ADDRESS, String.valueOf(aDdR));
GetLocationsAsyncTask asyncTask = new GetLocationsAsyncTask(this, MapContext);
asyncTask.execute(map);
//setCamView(Mylati, Mylongi);
return;
}
public void loadLocations(ArrayList<HashMap<String, String>> directionPoints){
for(int i = 0 ; i < directionPoints.size() ; i++){

String LocAddress = directionPoints.get(i).get("Addresse");
double LocLat = Double.parseDouble(directionPoints.get(i).get("latitude"));
double LocLng =
Double.parseDouble(directionPoints.get(i).get("longitude"));
String LocType = directionPoints.get(i).get("Type");
Log.d("MapActivit", "directionPoints.size beträgt: "+
directionPoints.size() + LocAddress + String.valueOf(i));
String LocTime = directionPoints.get(i).get("TimeStamp");


googleMap.addMarker( new MarkerOptions().position(new LatLng(LocLat,LocLng)).icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_icon)));
}
return;

}
public void setCamView(double camLat, double camLong){
CameraPosition cameraPosition = new CameraPosition.Builder().target(
new LatLng(camLat, camLong)).zoom(12).build();
LatLng Startposition = new LatLng(camLat, camLong);
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
return;
}
}

生成的 XML 文件如下所示:


<markers>
<marker>
<address>1908 Pike pl, Seattle, WA</address>
<lat>47.610126</lat>
<lng>-122.342834</lng>
<type>Bar</type>
<tstamp>2013-12-25 00:43:50</tstamp>
</marker>
<marker>
<address>1416 E Olive Way, Seattle, WA</address>
<lat>47.617214</lat>
<lng>-122.326584</lng>
<type>Bar</type>
<tstamp>2013-12-25 00:43:50</tstamp>
</marker>
<marker>
<address>2234 2nd Ave, Seattle, WA</address>
<lat>47.613976</lat>
<lng>-122.345467</lng>
<type>Restaurant</type>
<tstamp>2013-12-25 00:43:50</tstamp>
</marker>
<marker>
<address>1301 Alaskan Way, Seattle, WA</address>
<lat>47.605961</lat>
<lng>-122.340363</lng>
<type>Bar</type>
<tstamp>2013-12-25 00:43:50</tstamp>
</marker>
<marker>
<address>1225 1st Ave, Seattle, WA</address>
<lat>47.606365</lat>
<lng>-122.337654</lng>
<type>Bar</type>
<tstamp>2013-12-25 00:43:50</tstamp>
</marker>
</markers>

有人能帮帮我吗?我的代码有什么问题?

编辑:

问题在 gaurav5430 和 Ivan 的帮助下得以解决。请在下面查看他们的回答。

最佳答案

方法中

public ArrayList<HashMap<String, String>> getDirection(Document DirektDoc) {
....
HashMap<String, String> map = new HashMap<String, String>();
for (int i = 0; i < nl.getLength(); i++) {
// creating new HashMap
try{

....

LocationItems.add(map);
}catch(Exception e){
Log.d("LocationParser", e.getMessage());
}
}

您始终操作一个 hashmap 实例并将该实例添加到 List 中。所以你需要像这样将 HashMap 的创建移动到循环中:

   public ArrayList<HashMap<String, String>> getDirection(Document DirektDoc) {
....

for (int i = 0; i < nl.getLength(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
// creating new HashMap
try{

....

LocationItems.add(map);
}catch(Exception e){
Log.d("LocationParser", e.getMessage());
}

关于android - arraylist<hashmap<string,string>> 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20768302/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com