2016-04-19 141 views
0

我想從hashmap(從ListViewRestaurants)獲取值因此,我可以將經緯度傳遞給MapsActivity,但因爲我的hashmap總是返回null ...所以任何人都可以幫助我。爲什麼我的hashmap是空的Android?

public class ListViewRestaurants extends Activity { 

    static ListView listView; 
    static ArrayList<HashMap<String, String>> arrList; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_list); 

     listView = (ListView) findViewById(R.id.listiew); 
     arrList = new ArrayList<HashMap<String, String>>(); 

     String json_str = getJsonData(); 

     try { 
      JSONArray jArray = new JSONArray(json_str); 

      for (int i = 0; i < jArray.length(); i++) { 
       JSONObject json = null; 
       json = jArray.getJSONObject(i); 

       HashMap<String, String> map1 = new HashMap<String, String>(); 

       // adding each child node to HashMap key => value 
       map1.put("id", json.getString("id")); 
       map1.put("BusinessName", json.getString("BusinessName")); 
       map1.put("AddressLine1", json.getString("AddressLine1")); 
       map1.put("AddressLine2", json.getString("AddressLine2")); 
       map1.put("AddressLine3", json.getString("AddressLine3")); 
       map1.put("PostCode", json.getString("PostCode")); 
       map1.put("RatingValue", json.getString("RatingValue")); 
       map1.put("RatingDate", json.getString("RatingDate")); 
       map1.put("DistanceKM", json.getString("DistanceKM")); 
       map1.put("Latitude", json.getString("Latitude")); 
       map1.put("Longitude", json.getString("Longitude")); 

       int name = json.getInt("RatingValue"); 
       if(name == 5){ 
        map1.put("Image", String.valueOf(R.drawable.ic_launcher_web)); 
       } 
       else if(name==4){ 
        //map1.put("Image", String.valueOf(R.drawable.ic_launcher_web)); 
       } 
       else if(name==3){ 
        // map1.put("Image", String.valueOf(R.drawable.ic_launcher_web)); 
       } 
       else if(name==2){ 
        // map1.put("Image", String.valueOf(R.drawable.ic_launcher_web)); 
       } 
       else if(name==1){ 
        // map1.put("Image", String.valueOf(R.drawable.ic_launcher_web)); 
       } 
       else if(name==0){ 
        // map1.put("Image", String.valueOf(R.drawable.ic_launcher_web)); 
       } 
       else if(name==-1){ 
        map1.put("RatingValue", "Exempt"); 
       } 
       // adding HashList to ArrayList 
       arrList.add(map1); //    Intent intent = new Intent(this,MapsActivity.class); //    intent.putExtra("map", map1); 
      } 


     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 

     if (!arrList.isEmpty()) { 
      ListAdapter adapter = new SimpleAdapter(this, arrList, 
        R.layout.list_item, new String[]{"id", "BusinessName", 
        "AddressLine1", "AddressLine2", "AddressLine3", "PostCode", 
        "RatingValue", "RatingDate", "DistanceKM", "Image"}, 
        new int[]{R.id.restaurantId, R.id.BusinessName, 
          R.id.adr1, R.id.adr2, R.id.adr3, R.id.postCode, 
          R.id.rating, R.id.ratingDate, R.id.distance, R.id.image}); 


      listView.setAdapter(adapter); 
     } 
    } 

    private String getJsonData() { 
     StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() 
       .detectDiskReads() 
       .detectDiskWrites() 
       .detectNetwork() // or .detectAll() for all detectable problems 
       .penaltyLog() 
       .build()); 

     String str = ""; 
     Intent intent = getIntent(); 
     String latitude = intent.getStringExtra("Lat"); 
     String longtitude = intent.getStringExtra("Lon"); 
     HttpResponse response; 
     HttpClient myClient = new DefaultHttpClient(); 
     HttpPost myConnection = new HttpPost("http://sandbox.kriswelsh.com/hygieneapi/hygiene.php?op=s_loc&lat="+latitude+"&long="+longtitude); 

     try { 
      response = myClient.execute(myConnection); 
      str = EntityUtils.toString(response.getEntity(), "UTF-8"); 

     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     return str; 
    } 

} 

這裏我試圖找回從ListViewRestuarants的hashamp(經度和緯度)的細節,但我總是得到空值。

import android.content.Intent; import android.support.v4.app.FragmentActivity; import android.os.Bundle; import android.util.Log; 

import com.google.android.gms.maps.CameraUpdateFactory; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.OnMapReadyCallback; import com.google.android.gms.maps.SupportMapFragment; import com.google.android.gms.maps.model.LatLng; import com.google.android.gms.maps.model.MarkerOptions; 

import java.util.HashMap; 

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback { 

    private GoogleMap mMap; 
    static String x; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_maps); 

     // Obtain the SupportMapFragment and get notified when the map is ready to be used. 
     SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() 
       .findFragmentById(R.id.map); 
     mapFragment.getMapAsync(this); 
     //get the value from the array class 
     Intent intent = getIntent(); 
     HashMap<String, String> hashMap = (HashMap<String, String>)intent.getSerializableExtra("map"); System.out.println(hashmap) 


     // Log.v("HashMapTest", hashMap.get("Longtitude")); 
    } 

    /** 
    * Manipulates the map once available. 
    * This callback is triggered when the map is ready to be used. 
    * This is where we can add markers or lines, add listeners or move the camera. In this case, 
    * we just add a marker near Sydney, Australia. 
    * If Google Play services is not installed on the device, the user will be prompted to install 
    * it inside the SupportMapFragment. This method will only be triggered once the user has 
    * installed Google Play services and returned to the app. 
    */ 
    @Override 
    public void onMapReady(GoogleMap googleMap) { 
     mMap = googleMap; 

     // Add a marker in Sydney and move the camera 
     LatLng sydney = new LatLng(-34, 151); 
     mMap.addMarker(new MarkerOptions().position(sydney).title(x)); 
     mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)); 
    } } 

這是一個主類,它顯示選項卡視圖(詳細信息)

import android.app.TabActivity; import android.content.Intent; import android.os.Bundle; import android.widget.TabHost; 

public class MainActivity extends TabActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     // create the TabHost that will contain the Tabs 
     TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost); 

     TabHost.TabSpec tab1 = tabHost.newTabSpec("First Tab"); 
     TabHost.TabSpec tab2 = tabHost.newTabSpec("Second Tab"); 

     // Set the Tab name and Activity 
     // that will be opened when particular Tab will be selected 
     tab1.setIndicator("Restaurant"); 
     tab1.setContent(new Intent(this, ListViewRestaurants.class)); 

     tab2.setIndicator("Map"); 
     tab2.setContent(new Intent(this, RestaurantMap.class)); 

     /** Add the tabs to the TabHost to display. */ 
     tabHost.addTab(tab1); 
     tabHost.addTab(tab2); 
    } } 
+0

對於初學者來說,取消註釋您正在設置附加功能的代碼會有所幫助。 – NoChinDeluxe

+0

你是否得到一個NullPointerException?地圖在什麼時候是空的? – cyroxis

回答

0

的問題是你設置的地圖不同的密鑰,但試圖獲取對應不同的價值鍵 -

map1.put("Longitude", json.getString("Longitude")); //Setting "Longitude" 

Log.v("HashMapTest", hashMap.get("Longtitude")); //Getting "Longtitude" 

顯然,在您的地圖中沒有鍵「Longtitude」。

最佳做法是使用鍵的常量並將其用於插入和檢索,這樣可以避免這些問題,也不會浪費您的時間進行調試。