I can not get return value of the onSuccess method (Uri uri) [closed]

0

For my college CBT I'm developing an android app. I stopped at a point where I'm downloading the URL of some Firebase Storage images, and within the onSuccess(Uri uri) method, I have access to the uri variable where the URL of the image returns to me so I can switch to Picasso or Glider and download. But the thing is that I can not get this uri value to handle outside of the onSucess() method.

Below is my code:

ListView Locallista;
List<DataProvider> tracks;
DataProvider track;
String uid;
String iid;
Uri bitmap;
View header;
String key;
AdaptadorCuston adaptador;
private FirebaseAuth auth;
Query databaseTracks;
FirebaseDatabase databaseTrack;
FirebaseStorage storage;
StorageReference storageReference;
@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);

    setContentView(R.layout.tela_principal);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    toolbar.setTitle("HunterFood Fornecedor");
    setSupportActionBar(toolbar);

    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();

    if (user != null) {
        uid = user.getUid();
    }

    auth = FirebaseAuth.getInstance();
    databaseTrack = FirebaseDatabase.getInstance();
    storage = FirebaseStorage.getInstance();

    databaseTracks = 
    databaseTrack.getReference("produtos").orderByChild("id").equalTo(uid);
    storageReference = storage.getReferenceFromUrl("gs://hunterfood-
    a7f41.appspot.com/pictures_prod/");

    tracks = new ArrayList<>();
    Locallista = (ListView) findViewById(R.id.lista);
    header = (View) getLayoutInflater().inflate(R.layout.celula_lista,null);
    Locallista.addHeaderView(header);

    carregarDados();

    Locallista.setOnItemClickListener(new AdapterView.OnItemClickListener(){

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int 
    position, long id) {

            Object o = Locallista.getItemAtPosition(position);

            Intent t = new Intent(getApplicationContext(),
            tela_editar.class);
            startActivity(t);
        }
    });
   }

   protected void onStart() {
        super.onStart();
    }

    protected void onResume(){
        super.onResume();
    }

    private void carregarDados() {

        databaseTracks.addValueEventListener(new ValueEventListener(){

        @Override
        public void onDataChange(final DataSnapshot dataSnapshot) {

            tracks.clear();
            for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                track = postSnapshot.getValue(DataProvider.class);
                iid = postSnapshot.getKey();
                track.setKey(iid);

                storageReference.child(iid + 
   ".jpg").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>
  (){
                    @Override
                    public void onSuccess(Uri uri) {

                        //enter code here
                        bitmap = uri;
                        Context context = getApplicationContext();
                        Toast toast = Toast.makeText(context, "Download Completou", Toast.LENGTH_SHORT);
                        toast.show();
                    }

                }).addOnFailureListener(new OnFailureListener() {

                    @Override
                    public void onFailure(@NonNull Exception exception) {

                        Context context = getApplicationContext();
                        Toast toast = Toast.makeText(context, "Download 
    Falhou", Toast.LENGTH_SHORT);
                        toast.show();
                    }
                });

                track.setIcone(bitmap);
                tracks.add(track);
            }

            adaptador = new 
    AdaptadorCuston(tela_principal.this,R.layout.celula_lista, tracks);

            Locallista.setAdapter(adaptador);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            String mensagem = databaseError.getMessage();
            Toast.makeText(getApplicationContext(), mensagem, 
    Toast.LENGTH_SHORT).show();
        }
    });

Below is the data from the AdapterCuston class:

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull 
ViewGroup parent) {
    View row;
    row = convertView;
    DataHandler handler;
    if(convertView == null){
       LayoutInflater inflater =


  (LayoutInflater)this.getContext().getSystemService(
   Context.LAYOUT_INFLATER_SERVICE);
        row = inflater.inflate(R.layout.celula_lista,parent, false);
        handler = new DataHandler();
        handler.imagemIcone = (ImageView)row.findViewById(R.id.img_lista);
        handler.titulo = (TextView) row.findViewById(R.id.txt_titulo);
        handler.descricao = (TextView) row.findViewById(R.id.txt_descricao);
        row.setTag(handler);
    }else{
        handler = (DataHandler)row.getTag();
    }
    DataProvider dataProvider;
    dataProvider = (DataProvider) this.getItem(position);
    Glide
            .with(getContext())
            .load(dataProvider.getIcone())
            .into(handler.imagemIcone); 
    handler.titulo.setText(dataProvider.getTitulo());
    handler.descricao.setText(dataProvider.getDescricao());
    return row;
}
    private class DataHandler {
        ImageView imagemIcone;
        TextView titulo;
        TextView descricao;
    }
}
    
asked by anonymous 06.09.2017 / 03:00

1 answer

0

You will need to declare an instance of your DataProvider within the for and pass this instance to the onSuccess(...) method, where you can assign the uri to the correct instance of each DataProvider .

tracks.clear();
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
    final DataProvider track = postSnapshot.getValue(DataProvider.class);
    iid = postSnapshot.getKey();
    track.setKey(iid);

    storageReference.child(iid + ".jpg").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>(){
        @Override
        public void onSuccess(Uri uri) {

            track.setIcone(uri);

            ...
        }

    });
    ...
}

UPDATE

AdaptadorCuston constructor:

public AdaptadorCuston(/*Seus parâmetros*/..., StorageReference storageReference) {
    this.storageReference = storageReference;
}

Method getView of AdaptadorCuston :

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    ...
    handler.imagemIcone = (ImageView)row.findViewById(R.id.img_lista);
    ...

    DataProvider dataProvider = (DataProvider) this.getItem(position);

    storageReference.child(dataProvider.getKey() + ".jpg").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>(){
        @Override
        public void onSuccess(Uri uri) {

            Glide.with(getContext())
                .load(uri)
                .into(handler.imagemIcone);

            dataProvider.setIcone(uri);

            ...
        }
    });
}
    
06.09.2017 / 04:45