2012-11-24 18 views
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沒有顯示任何錯誤的信息(沒有紅線),但是如果我改變位置,新項目仍然不能被添加到數據列表中。如何添加當前位置列表視圖

+0

請務必發佈例外的日誌。 – yDelouis

回答

0

我已經解決通過添加新方法的問題

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)); 
+0

它不起作用。 – HeikiCyan

0

您不能從不同類的getLocation,你只能進去當前onLocationChanged()

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

       data.add(latLongString); 
       Adapter.notifyDataSetChanged(); 
      } 
     }; 
+0

我已經做了你的建議,但它仍然無法正常工作。我更新我的案例的logcat信息。 – HeikiCyan

+0

將此「適配器」更改爲適配器,無論它出現在哪裏 – RajeshVijayakumar

+0

我不明白你的意思?該適配器只出現在這個java文件中,我改變了代碼,以便該類不再擴展maptask。所有出現在我的java文件中的適配器都使用這個「Adapter」。 – HeikiCyan

相關問題