简体   繁体   中英

List isn't populating

I'm making an app that will list addresses nearby in a listview, but for some reason the addresses aren't going into the list. Am I missing something with the address collection or?...

It happens within the for loop. I want it to read the list of addresses I get and trim the information I want to only necessary bits, like street numbers and zip codes instead of the big mess of numbers.

Every time I run the app however, the list remains blank.

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 ); 
    } 
}
for (int a = 0; a > 6; a++) {

You are never going to enter this loop. You start with 0 which is always smaller than 6.

This condition for (int a = 0; a > 6; a++) is not correct.

As you have initialized a with 0 and then checking a > 6 it will always be false and your program will never get in the loop.

It must be for (int a = 0; a < 6; a++) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM