2012-07-05 47 views
0

我正在製作一個應用程序,該應用程序將在列表視圖中列出附近的地址,但由於某些原因,地址不會進入列表。我是否缺少地址集合或?...List is not populating

它發生在for循環中。我希望它讀取我得到的地址列表,並修剪我只需要的位的信息,如街道號碼和郵政編碼,而不是大量的數字。

但是,每次運行應用程序時,列表都保持空白。

package com.atonea.ps; 


import java.util.ArrayList; 
import java.util.Arrays; 
import java.util.List; 
import java.util.Locale; 

import android.app.Activity; 
import android.content.Context; 
import android.location.Address; 
import android.location.Criteria; 
import android.location.Geocoder; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.ListView; 
import android.widget.TextView; 
import android.widget.Toast; 

public class PSActivity extends Activity { 
protected static final Location Location = null; 
/** Called when the activity is first created. */ 
String location_text=""; 
String here=""; 
final ArrayList<String> addressbook = new ArrayList<String>(); 


@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    Button goButton = (Button) findViewById(R.id.beginButton); 
    LocationManager locationManager; 
    String context = Context.LOCATION_SERVICE; 
    locationManager = (LocationManager)getSystemService(context); 

    Criteria crta = new Criteria(); 
    crta.setAccuracy(Criteria.ACCURACY_FINE); 
    crta.setAltitudeRequired(false); 
    crta.setBearingRequired(false); 
    crta.setCostAllowed(true); 
    crta.setPowerRequirement(Criteria.POWER_LOW); 
    String provider = locationManager.getBestProvider(crta, true); 
    Location location = locationManager.getLastKnownLocation(provider); 
    updateWithNewLocation(location); 

    goButton.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
     } 

    });   



} 
private void updateWithNewLocation(Location location) { 
    String latLong; 
    TextView uhoh = (TextView) findViewById(R.id.texty); 

    ListView listview = (ListView) findViewById(R.id.listview1); 
    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, addressbook); 
    listview.setAdapter(arrayAdapter); 

    String addressString = "no address found"; 
    Address fulladdress; 
    String street; 
    String zip; 
    String addresstogether; 

    if(location!=null) { 
    double lat = location.getLatitude(); 
    double lon = location.getLongitude(); 
    latLong = "Lat:" + lat + "\nLong:" + lon; 

    double lattitude = location.getLatitude(); 
    double longitude = location.getLongitude();  
    Geocoder gc = new Geocoder(this,Locale.getDefault()); 

    try { 
    List addresses= gc.getFromLocation(lattitude, longitude, 1); 

    if(addresses.size()>0) { 
     for (int a = 0; a > 6; a++) { 
      fulladdress = ((Address) addresses.get(a)); 
      street = fulladdress.getAddressLine(a); 
      zip = fulladdress.getPostalCode(); 
      addresstogether = street+" "+zip; 
      addressbook.add(a, addresstogether); 
      arrayAdapter.notifyDataSetChanged(); 
      Toast.makeText(getApplicationContext(),addresstogether, Toast.LENGTH_LONG).show(); 
      } 
     } 
    } catch (Exception e) { 
    } 
    } else { 
    latLong = " NO Location Found "; 
    } 

    uhoh.setText("your Current Position is :\n" +latLong + "\n " + addressString); 
    } 
} 
+6

'a> 6'應該是'a st0le

回答

8
for (int a = 0; a > 6; a++) { 

你永遠不會進入這個循環。您從0開始始終小於6.

+0

哇......我很笨。像真的愚蠢 – AtlasOnEarth

+0

請確保你看st0le評論。當API返回任何答案時,您可能會遇到不同的問題。 –

+2

請將此問題標記爲答案,方法是單擊樂譜旁邊的勾號。 :) – Brad

1

此條件for (int a = 0; a > 6; a++)不正確。

由於您初始化了a with 0,然後檢查了a > 6,它將始終爲假,並且您的程序永遠不會進入循環。

它必須爲(int a = 0; a < 6; a++)