简体   繁体   中英

How to display detail informations from firestore in another activity when I click a listView item?

I'm making app and I use firestore as database. I made a listView consisting of movies(just names). And I want to display details in another activity when I click one of them. I did something for this but I just get the last movie I added. When I click the another one, the result is the last movie I added. All movie details are same. I couldn't fix this. How can I fix it?

this is how I made the listView part

public class Izlediklerim extends AppCompatActivity {
    private FirebaseAuth firebaseAuth;
    private FirebaseFirestore firebaseFirestore;
    ArrayList<String> adArray;
    ListView listView;
    ArrayAdapter arrayAdapter;


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

        firebaseFirestore=FirebaseFirestore.getInstance();
        adArray=new ArrayList<>();
        listView=findViewById(R.id.listView);
        arrayAdapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1,adArray);
        listView.setAdapter(arrayAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Intent intent=new Intent(Izlediklerim.this,FilmiGoster.class);
                startActivity(intent);
            }
        });
        filmleriAl();
    }
    public void filmleriAl(){
        CollectionReference collectionReference = firebaseFirestore.collection("Filmler");
        collectionReference.orderBy("filmtarih").addSnapshotListener(new EventListener<QuerySnapshot>() {
            @Override
            public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {
                if (e != null) {
                    Toast.makeText(Izlediklerim.this, e.getLocalizedMessage().toString(), Toast.LENGTH_LONG).show();
                }

                if (queryDocumentSnapshots != null) {

                    for (DocumentSnapshot snapshot : queryDocumentSnapshots.getDocuments()) {

                        Map<String, Object> data = snapshot.getData();
                        String name = (String) data.get("filmadi");

                        adArray.add(name);
                        arrayAdapter.notifyDataSetChanged();
                    }
                }

            }
        });
    }
}

and this is the detail part

public class FilmiGoster extends AppCompatActivity {
    ImageView fr;
    TextView fa,ft,fh;
    private FirebaseAuth firebaseAuth;
    private FirebaseFirestore firebaseFirestore;


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

        fr=findViewById(R.id.fr);
        fa=findViewById(R.id.fa);
        ft=findViewById(R.id.ft);
        fh=findViewById(R.id.fh);



        firebaseFirestore=FirebaseFirestore.getInstance();
        firebaseAuth=FirebaseAuth.getInstance();

        CollectionReference collectionReference = firebaseFirestore.collection("Filmler");
        collectionReference.addSnapshotListener(new EventListener<QuerySnapshot>() {
            @Override
            public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {
                if (e != null) {
                    Toast.makeText(FilmiGoster.this,e.getLocalizedMessage().toString(),Toast.LENGTH_LONG).show();
                }

                if (queryDocumentSnapshots != null) {
                    for (DocumentSnapshot snapshot : queryDocumentSnapshots.getDocuments()) {

                        Map<String, Object> data = snapshot.getData();


                        String hakkinda = (String) data.get("filmdusunce");
                        String ad = (String) data.get("filmadi");
                        String downloadUrl = (String) data.get("downloadurl");
                        String zaman = (String) data.get("filmtarih");




                        fa.setText(ad);
                        Picasso.get().load(downloadUrl).into(fr);
                        fh.setText(hakkinda);
                        ft.setText(zaman);



                    }
                }
            }
        });
    }

}

Have 2 more array lists in Izlediklerim

public class Izlediklerim extends AppCompatActivity {

    private FirebaseAuth firebaseAuth;
    private FirebaseFirestore firebaseFirestore;
    ArrayList<String> adArray;

    //here..
    ArrayList<String> hakkindaArray = new ArrayList<>();
    ArrayList<String> zamanArray = new ArrayList<>();

    ListView listView;
    ArrayAdapter arrayAdapter;
    .......
    .......

Now in filmleriAl() method of Izlediklerim :

for (DocumentSnapshot snapshot : queryDocumentSnapshots.getDocuments()) {

 Map<String, Object> data = snapshot.getData();

String name = (String) data.get("filmadi");

//add these too
String hakkinda = (String) data.get("filmdusunce");
String zaman = (String) data.get("filmtarih");

adArray.add(name);

//add these too
hakkindaArray.add(hakkinda);
zamanArray.add(zaman);
....
....

Pass the movie details as an intent extra:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

Intent intent=new Intent(Izlediklerim.this,FilmiGoster.class); 
//here..
intent.putExtra("movie_name" , adArray.get(position));
intent.putExtra("movie_hakkinda" , hakkindaArray.get(position));
intent.putExtra("movie_zaman" , zamanArray.get(position));
startActivity(intent);
....
....

Now in your onCreate() of FilmiGoster get the details passed:

public class FilmiGoster extends AppCompatActivity {
ImageView fr;
TextView fa,ft,fh;
private FirebaseAuth firebaseAuth;
private FirebaseFirestore firebaseFirestore;

//add these

private String movieName;
private String movieHakkinda;
private String movieZaman;


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

//here get the passed movie details and use directly in textViews
movieName = getIntent().getStringExtra("movie_name");
movieHakkinda = getIntent().getStringExtra("movie_hakkinda");
movieZaman = getIntent().getStringExtra("movie_zaman");

......
......

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM