Soru & Cevap

Viewpager Firebase İdsi

06.04.2020 - 03:06

Merhaba, Firebase'ten PagerAdapter Listeleme yapıyorum, ve ViewPager'da Görüntülüyorum ama istediğim şu, Viewpager'a açık olan View'in idsini almak istiyorum. 'ama firebase idsi olmasını istiyorum', bir türlü yapamadım, yardımcı olabilirmisiniz?

 

Yapmak istediğim şu, ViewPager'da 1 adet Butonum var ve ona Tıkladığı Zaman, O anki Görüntünün Firebase'deki İdsini Yakalamak İstiyorum. 

Şimdiden Teşekkür Ederim..

 

Movie.java

package com.example.duvarlar.Models;

public class Movie {
    private String gonderiId;
    String image;

    public Movie() {
    }

    public Movie(String gonderiId,String image) {
        this.gonderiId = gonderiId;
        this.image = image;
    }
    public String getGonderiId() {
        return gonderiId;
    }

    public void setGonderiId(String gonderiId) {
        this.gonderiId = gonderiId;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }
}

MyAdapter.java

public class MyAdapter extends PagerAdapter {
    Context context;
    List<Movie> movieList;
    LayoutInflater inflater;
    OutputStream outputStream;
    public MyAdapter(Context context, List<Movie> movieList) {
        this.context = context;
        this.movieList = movieList;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return movieList.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        ((ViewPager)container).removeView((View)object);
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull final ViewGroup container, final int position) {
        final View view = inflater.inflate(R.layout.items,container,false);
        final ImageView movie_image = (ImageView)view.findViewById(R.id.movie_image);
        Picasso.get()
                .load(movieList.get(position).getImage())
                .fit()
                .centerCrop()
                .into(movie_image);

        container.addView(view);

        return view;
    }
}


SimpleFragment.java

public class SimpleFragment extends Fragment implements IFirebaseLoadDone, ValueEventListener {
    private ViewPager viewPager;
    private MyAdapter adapter;
    private DatabaseReference movies;
    private IFirebaseLoadDone iFirebaseLoadDone;
    private ImageButton btn_fav;
    private ContentLoadingProgressBar progressBar;
    private OutputStream outputStream;


    public SimpleFragment() {
        // Required empty public constructor
    }


    @SuppressLint("ClickableViewAccessibility")
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_simple, container, false);

        movies = FirebaseDatabase.getInstance().getReference("Sports");

        iFirebaseLoadDone = this;
        loadMovie();

        viewPager = (ViewPager) view.findViewById(R.id.vivi_pager);


        btn_fav = view.findViewById(R.id.btn_fav);

     
    
        btn_fav.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                final FirebaseUser mevcutKullanici = FirebaseAuth.getInstance().getCurrentUser();
                FirebaseDatabase.getInstance().getReference().child("Favorites").child("burasıduzelecek")
                        .child(mevcutKullanici.getUid()).setValue(true);

                Toast.makeText(getContext(), "Favoriye Eklendi", Toast.LENGTH_SHORT).show();




            }
        });


        return view;
    }

    private void loadMovie() {

        movies.addValueEventListener(this);


    }

    @Override
    public void onFirebaseLoadSuccess(List<Movie> movieList) {
        adapter = new MyAdapter(getContext(), movieList);
        viewPager.setAdapter(adapter);

    }

    @Override
    public void onFirebasLoadFailed(String message) {
        Toast.makeText(getContext(), "" + message, Toast.LENGTH_SHORT).show();
    }


    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        List<Movie> movieList = new ArrayList<>();
        for (DataSnapshot moviesSnapShot : dataSnapshot.getChildren())
            movieList.add(moviesSnapShot.getValue(Movie.class));

        iFirebaseLoadDone.onFirebaseLoadSuccess(movieList);


        adapter.notifyDataSetChanged();
        progressBar.setVisibility(View.GONE);


    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        iFirebaseLoadDone.onFirebasLoadFailed(databaseError.getMessage());
    }

    @Override
    public void onDestroy() {
        movies.removeEventListener(this);
        super.onDestroy();
    }

    @Override
    public void onResume() {
        super.onResume();
  
    }

    @Override
    public void onStop() {
        movies.removeEventListener(this);
        super.onStop();
    }
}

 

6 Görüntülenme

0 Cevap

Sitedeki sorulara cevap verebilmek için giriş yapın ya da üye olun.