2
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.example.geotask.maptask;
import com.google.android.maps.GeoPoint;

public class list extends Activity{ 
    private ListView listView; 
    List<String> data = new ArrayList<String>(); 
    ArrayAdapter<String> Adapter;
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        String provider = locationManager.GPS_PROVIDER;
        Location location = locationManager.getLastKnownLocation(provider);
       String latLongString = "Lat:" + location.getLatitude() + "\nLong:" + location.getLongitude();

        setContentView(R.layout.list); 
        locationManager.requestLocationUpdates(provider, 2000, 10, locationListener);
        listView = (ListView)findViewById(R.id.listView1); 
        Adapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1,data);
        listView.setAdapter(Adapter);
        data.add(latLongString);
    } 




     private  LocationListener locationListener = new LocationListener() {
            public void onLocationChanged(Location location) {

                final String latLongString = "Lat:" + location.getAltitude() + "\nLong:" + location.getLongitude();
                 Runnable add= new Runnable(){
                        public void run()
                        {   data.add(latLongString);
                        Adapter.notifyDataSetChanged();
                        }
                    };
             // updateWithNewLocation(location);


        //      addlayout(location);
            }

            public void onProviderDisabled(String provider){
              //updateWithNewLocation(null);
          //    addlayout(null);

            }

            public void onProviderEnabled(String provider) {
                //updateWithNewLocation(null);
            //  addlayout(null);

            }

            public void onStatusChanged(String provider, int status, Bundle extras) {}
          };


          //Update the map with a new location 


} 

就像上面的代码一样,当我启动应用程序时,它就可以工作了。但是在我通过模拟器控制发送另一个位置点后,它失败了。我更新了问题。现在logcat没有显示任何错误信息(没有红线),但是如果我更改位置,新项目仍然无法添加到数据列表中。

4

3 回答 3

0

你没有设置你的适配器。你应该把它放在onCreate

Adapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1,data);
listView.setAdapter(Adapter);

代替 :

listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1,data));
于 2012-11-24T23:46:09.223 回答
0

您无法从不同的班级获取位置,您只能在当前班级内获取onLocationChanged()

      String latLongString = location.getAttitude()" + "location.getLongtitude(); 
      Runnable add= new Runnable(){
            public void run()
            {   

                data.add(latLongString);
                Adapter.notifyDataSetChanged();
            }
      };
于 2012-11-24T23:46:40.023 回答
0

我通过添加一个新方法解决了这个问题

于 2012-11-25T21:23:45.367 回答