当我单击listView项时,如何在其他活动中显示来自Firestore的详细信息?

时间:2020-05-09 18:56:49

标签: java android firebase android-studio google-cloud-firestore

我正在制作应用程序,并且将Firestore用作数据库。我制作了一个由电影(仅是名称)组成的listView。我想在单击其中一个活动时在另一个活动中显示详细信息。我为此做了一些工作,但我只得到了我添加的最后一部电影。当我单击另一部电影时,结果是我添加的最后一部电影。所有电影细节均相同。我无法解决此问题。我该如何解决?

这就是我制作listView部分的方式

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();
                    }
                }

            }
        });
    }
}

这是详细信息部分

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);



                    }
                }
            }
        });
    }

}

1 个答案:

答案 0 :(得分:1)

Izlediklerim中还有2个数组列表

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;
    .......
    .......

现在使用filmleriAl()的{​​{1}}方法:

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); .... .... 传递:

intent

现在在您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); .... .... 的{​​{1}}中传递详细信息:

onCreate()