2017-02-23 37 views
-1

我想把網址的值放到畢加索的[.load(url)]中,我不能通過它不知道爲什麼。但我可以在Textview中加載url的值。請幫助我知道它非常簡單。我需要幫助(新手)畢加索動態圖片網址傳遞日期

package com.maasin.map.anew.model; 

import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.support.design.widget.FloatingActionButton; 
import android.support.design.widget.Snackbar; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.text.Html; 
import android.util.Log; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.ImageView; 
import android.widget.TextView; 
import android.widget.Toast; 

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.Marker; 
import com.google.android.gms.maps.model.MarkerOptions; 
import com.google.firebase.database.ChildEventListener; 
import com.google.firebase.database.DataSnapshot; 
import com.google.firebase.database.DatabaseError; 
import com.google.firebase.database.DatabaseReference; 
import com.google.firebase.database.FirebaseDatabase; 
import com.google.firebase.database.ValueEventListener; 
import com.google.firebase.storage.FirebaseStorage; 
import com.google.firebase.storage.StorageReference; 
import com.maasin.map.anew.R; 
import com.maasin.map.anew.model.model.Local; 
import com.squareup.picasso.Picasso; 

import java.util.LinkedList; 
import java.util.List; 

import static android.R.attr.defaultValue; 
import static android.R.attr.drawable; 
import static android.R.attr.name; 
import static android.R.attr.value; 
import static android.R.attr.valueFrom; 

/** 
* Created by Carl Christian on 11/02/2017. 
*/ 

public class establishment extends AppCompatActivity implements OnMapReadyCallback { 

    TextView Name; 
    // TextView Name2; 
    TextView add; 
    TextView Categ; 
    TextView oStart; 
    TextView oEnd; 
    Toolbar tool; 
    String s; 
    private FirebaseDatabase database; 

    double latt = 124.838767; 
    double longg = 10.134265; 

    double longitude = 124.831786 ; 
    double latitude = 10.130952; 
    //String url; 
// List<String> urls = new LinkedList<String>(); 


    Context ctx = this; 


    private GoogleMap mMap; 
    private GoogleMap map; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_scrolling); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 
     database = FirebaseDatabase.getInstance(); 


     final DatabaseReference establishments = database.getReference("establishments"); 


     FirebaseStorage storage = FirebaseStorage.getInstance(); 
     s = getIntent().getStringExtra("PRIMEKEY"); 
     String s2 = getIntent().getStringExtra("PRIMEKEY2"); 
     latitude= getIntent().getDoubleExtra("location", defaultValue); 
     longitude= getIntent().getDoubleExtra("location1", defaultValue); 
    // Toast.makeText(this,Double.toString(l), Toast.LENGTH_SHORT).show(); 
     SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() 
       .findFragmentById(R.id.map); 
     mapFragment.getMapAsync(this); 
     // Toast.makeText(establishment.this, s2, Toast.LENGTH_SHORT).show(); 

     //SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() 
      //  .findFragmentById(R.id.map); 
     //mamapFragment.getMapAsync(this); 
     //map.addMarker(new MarkerOptions() 
      //  .position(new LatLng(124.838767, 10.134265)) 
      // .title("Hello world")); 

     // StorageReference storageRef = storage.getReferenceFromUrl("gs://newtest-e7dff.appspot.com/"); 

     //StorageReference imagesRef = storageRef.child(s2); 







//from main activity , title action bar 
     Name = (TextView) findViewById(R.id.Name); 
     //Name.setText(s2); 

     // Name2 = (TextView) findViewById(R.id.Name2); 

     add = (TextView) findViewById(R.id.add); 
     // Name2.setText(s2); 
     oStart = (TextView) findViewById(R.id.oStart); 

     oEnd = (TextView) findViewById(R.id.oEnd); 

     Categ = (TextView) findViewById(R.id.Categ); 

     getSupportActionBar().setTitle(s); 





     //FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance(); 
     // DatabaseReference mref = firebaseDatabase.getReference(); 
     establishments.child(s2).child("Estab_url").addChildEventListener(new ChildEventListener() { 
      @Override 
      public void onChildAdded(DataSnapshot dataSnapshot, String s) { 
      // String url = dataSnapshot.child("url").getValue(String.class); 
       // url = dataSnapshot.child("url").getValue(String.class); 
       // Name.setText(Html.fromHtml("&ldquo;" + url + "&rdquo;")); 
       // String gg = (Html.fromHtml("&ldquo;" + url + "&rdquo;")); 
       //urls.add(url); 
      // Name.setText(url); 
       ImageView imageView = (ImageView) findViewById(R.id.imageView); 
       Picasso.with(ctx) 
         .load(dataSnapshot.child("url").getValue(String.class)) 
         .resize(200,100) 
         .into(imageView); 

      } 

      public void onChildChanged(DataSnapshot dataSnapshot, String s) { } 
      public void onChildRemoved(DataSnapshot dataSnapshot) { } 
      public void onChildMoved(DataSnapshot dataSnapshot, String s) { } 

      @Override 
      public void onCancelled(DatabaseError databaseError) { 
       throw databaseError.toException(); // don't ignore onCancelled 
      } 
     }); 

     // Name.setText(urls); 



    // ImageView imageView = (ImageView) findViewById(R.id.imageView); 
    // Name.setText(url); 
    // Picasso.with(this) 
     //  .load(url) 
      // .resize(200,100) 
      //  .into(imageView); 







     // String x = establishments.child(s2).child("Estab_name").toString(); 
     // Name2.setText(x); 
     // final DatabaseReference establishments = database.getReference("establishments"); 



     establishments.child(s2).child("Estab_name").addListenerForSingleValueEvent(new ValueEventListener() 
     { 
      @Override 
      public void onDataChange(DataSnapshot dataSnapshot) { 
       String value = (String) dataSnapshot.getValue(); 
       //Name.setText(value); 

      } 

      @Override 
      public void onCancelled(DatabaseError databaseError) { 

      } 
     }); 




     // establishments.child(s2).child("latitude").addValueEventListener(new ValueEventListener() { 
     // @Override 
      // public void onDataChange(DataSnapshot dataSnapshot) { 
      // String value = (String) dataSnapshot.getValue(); 
       // double amount = Double.parseDouble(value); 
       //latitude= amount; 
       // Toast.makeText(this,value, Toast.LENGTH_SHORT).show(); 
      // } 

//   @Override 
    //   public void onCancelled(DatabaseError databaseError) { 

    //  } 
     // }); 



//  establishments.child(s2).child("longitude").addValueEventListener(new ValueEventListener() { 
    //   @Override 
    //  public void onDataChange(DataSnapshot dataSnapshot) { 
     //   String value = (String) dataSnapshot.getValue(); 
     //  double amount = Double.parseDouble(value); 
      //  longitude= amount; 

      //} 

//   @Override 
    //   public void onCancelled(DatabaseError databaseError) { 

//   } 
    //  }); 










     establishments.child(s2).child("Estab_add").addValueEventListener(new ValueEventListener() { 
      @Override 
      public void onDataChange(DataSnapshot dataSnapshot) { 
       String value = (String) dataSnapshot.getValue(); 
       add.setText(value); 

      } 

      @Override 
      public void onCancelled(DatabaseError databaseError) { 

      } 
     }); 





     establishments.child(s2).child("Estab_category").addValueEventListener(new ValueEventListener() { 
      @Override 
      public void onDataChange(DataSnapshot dataSnapshot) { 
       String value = (String) dataSnapshot.getValue(); 
       Categ.setText(value); 

      } 

      @Override 
      public void onCancelled(DatabaseError databaseError) { 

      } 
     }); 


     establishments.child(s2).child("Estab_Officestart").addValueEventListener(new ValueEventListener() { 
      @Override 
      public void onDataChange(DataSnapshot dataSnapshot) { 
       String value = (String) dataSnapshot.getValue(); 
       oStart.setText(value); 

      } 

      @Override 
      public void onCancelled(DatabaseError databaseError) { 

      } 
     }); 


     establishments.child(s2).child("Estab_Officeout").addValueEventListener(new ValueEventListener() { 
      @Override 
      public void onDataChange(DataSnapshot dataSnapshot) { 
       String value = (String) dataSnapshot.getValue(); 
       oEnd.setText(value); 

      } 

      @Override 
      public void onCancelled(DatabaseError databaseError) { 

      } 
     }); 
    } 

    @Override 
    public void onMapReady(GoogleMap googleMap) { 
     mMap = googleMap; 




     // Add a marker in Sydney and move the camera 
     LatLng sydney = new LatLng(latitude, longitude); 
     float zoomLevel = 16; 
     mMap.addMarker(new MarkerOptions().position(sydney).title(s)); 
     mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, zoomLevel)); 
    } 

} 
+0

日常問題... onChildXXX()中的代碼是執行異步的。這意味着,它下面的代碼將被執行之前,你有url –

回答

0

數據是從Firebase異步加載的。這意味着在您使用畢加索設置圖像時,url尚未設置。

你可以很容易地看到這種通過放置一些日誌語句在代碼:

System.out.println("Before attaching listener"); 
establishments.child(s2).child("Estab_url").addChildEventListener(new ChildEventListener() { 
    @Override 
    public void onChildAdded(DataSnapshot dataSnapshot, String s) { 
     System.out.println("In onChildAdded"); 
    } 

    public void onChildChanged(DataSnapshot dataSnapshot, String s) { } 
    public void onChildRemoved(DataSnapshot dataSnapshot) { } 
    public void onChildMoved(DataSnapshot dataSnapshot, String s) { } 

    @Override 
    public void onCancelled(DatabaseError databaseError) { 
     throw databaseError.toException(); // don't ignore onCancelled 
    } 
}); 
System.out.println("After attaching listener"); 

當你運行該代碼時,它會打印:

之前安裝監聽器

附加聽衆後

在onChild新增

這可能不是您預期的順序。但它完全解釋了爲什麼你的代碼無法加載圖像:URL尚不可用。

解決方案是重構問題。我不會考慮「首先閱讀網址,然後將其設置爲圖片」,而是將其重新設置爲「我將開始閱讀網址,當它可用時,我會將其設置爲圖片」。

後一種思維方式意味着您需要將URL移動到onChildAdded(在這種情況下)。所以,你最終用:

ImageView imageView = (ImageView) findViewById(R.id.imageView); 
Context ctx = this; 

establishments.child(s2).child("Estab_url").addChildEventListener(new ChildEventListener() { 
    @Override 
    public void onChildAdded(DataSnapshot dataSnapshot, String s) { 
     String url = dataSnapshot.child("url").getValue(String.class); 
     Picasso.with(ctx) 
      .load(url) 
      .resize(200,100) 
      .into(imageView); 
    } 

    public void onChildChanged(DataSnapshot dataSnapshot, String s) { } 
    public void onChildRemoved(DataSnapshot dataSnapshot) { } 
    public void onChildMoved(DataSnapshot dataSnapshot, String s) { } 

    @Override 
    public void onCancelled(DatabaseError databaseError) { 
     throw databaseError.toException(); // don't ignore onCancelled 
    } 
}); 
+0

我不知道什麼是錯的,但當我試着上面的應用程序崩潰的代碼。建設時沒有錯誤。我嘗試使用.load(url)和這個.load(「“」+ url +「”」)在url中添加「」 –

+0

沒有必要在子名稱中包含引號字符。這只是數據庫中的「url」。如果您遇到崩潰,您需要編輯您的問題,以純文本形式包含您的數據結構,新代碼,堆棧跟蹤,並指出哪一行代碼會引發異常。 –

+0

謝謝你的幫助弗蘭克我們現在可以檢索一個圖像。我們現在的問題是檢索多個圖像。 –

0

你必須在onChildAdded回調執行畢加索負載。由於onChildAdded是異步的,因此必須在回調後完成加載。

@Override 
public void onChildAdded(DataSnapshot dataSnapshot, String s) { 
    String url = dataSnapshot.child("url").getValue(String.class); 
    Log.v("Url content", "value:" + url); // Added this log 
    ImageView imageView = (ImageView) findViewById(R.id.imageView); 
    Picasso.with(this) 
      .load(url) 
      .resize(200,100) 
      .into(imageView); 
} 
+0

現在的應用程序不再崩潰,但它不會顯示URL中的圖像我不知道該怎麼辦 –

+0

請與代碼庫或其他內容分享您的代碼 –

+0

Nicola,這裏是我的建立.class。在我的問題下面。希望你能幫助我。 –