我想将url的值放入毕加索的[.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("“" + url + "”" ));
// String gg = (Html.fromHtml("“" + url + "”" ));
//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 :(得分:0)
数据是从Firebase异步加载的。这意味着,当您使用Picasso设置图像时,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");
运行此代码时,将打印:
在附加监听器之前
附加监听器后
在onChildAdded
这可能不是你预期的顺序。但它完全解释了为什么您的代码无法加载图片:URL尚不可用。
解决方案是重新构建问题。而不是想“首先我会读取URL,然后我将其设置在图像中”,将其重新定义为“我将开始阅读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
}
});
答案 1 :(得分:0)
您必须在onChildAdded回调中执行Picasso加载。由于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);
}