当我点击一个listView项目时,如何在另一个活动中显示firestore的详细信息?

问题描述 投票:0回答:1

我正在做一个应用程序,我使用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);



                    }
                }
            }
        });
    }

}
java android firebase android-studio google-cloud-firestore
1个回答
1
投票

有2个数组列表 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;
    .......
    .......

现在在 filmleriAl() 方式 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);
....
....

现在在你的 onCreate()FilmiGoster 获得通过的细节。

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

......
......
© www.soinside.com 2019 - 2024. All rights reserved.