简体   繁体   中英

How to retrieve data from firebase in new activity when an item in Listview is being clicked?

I am developing an app where I have used Firebase as my database and I have displayed some words in the ListView.

When I click on a particular item from the listview, the data for that word should be fetched from firebase and it should be displayed in a new activity.

I have managed to display the data from firebase in listview but couldn't show the other details as the user clicks on an item.

I have attached the code.

This is the activity which displays data from firebase in listview.

Dashboard.java

    public class Dashboard extends AppCompatActivity {

    ListView listView;
    FirebaseDatabase firebaseDatabase;
    DatabaseReference dreff, ref;
    ArrayList<String> words;
    ArrayList<Word> words2;
    ArrayAdapter<String> adapter;
    Word word;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dashboard);
        listView = (ListView) findViewById(R.id.listView);
        firebaseDatabase = FirebaseDatabase.getInstance();
        dreff = firebaseDatabase.getReference("words");
        ref = firebaseDatabase.getReference("words");
        words = new ArrayList<>();
        words2 = new ArrayList<Word>();
        adapter = new ArrayAdapter<>(Dashboard.this, R.layout.word_info, R.id.word_info_list, words);

        dreff.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for (DataSnapshot ds: dataSnapshot.getChildren()){
                    word = ds.getValue(Word.class);
                    words.add("" + word.getWord().toString());
                }
                listView.setAdapter(adapter);
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });

        ref.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
                String word = dataSnapshot.child("word").getValue(String.class);
                String description = dataSnapshot.child("description").getValue(String.class);
                String tamil = dataSnapshot.child("tamilword").getValue(String.class);


                words2.add(new Word(word, description, tamil));
            }

            @Override
            public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

            }

            @Override
            public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {

            }

            @Override
            public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(Dashboard.this, Meaning.class);
                intent.putExtra("description", listView.getItemAtPosition(position).toString());
                startActivity(intent);
            }
        });
    }
}

This is the Word.java class

Word.java

    public class Word {

    public String word;
    public String tamilword;
    public String description;


    public Word() {
    }

    public Word(String word, String description, String tamil) {

    }

    public String getWord() {
        return word;
    }

    public void setWord(String word) {
        this.word = word;
    }

    public String getTamilword() {
        return tamilword;
    }

    public void setTamilword(String tamilword) {
        this.tamilword = tamilword;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}

Now I want to display the other details when the user clicks on an item in the listview.

I have attached the firebase database below.

Okay so when you click the item do this:

Not the best way but just so you get the idea, I would suggest pass the position to another activity and do the fetching in the opened activity

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


   //fetch the details of the item

   dreff.child(String.valueOf(position+1)).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
         //get details of the item
          String description = dataSnapshot.child("description").getValue(String.class);
          String image = dataSnapshot.child("image").getValue(String.class);
          String tamilword = dataSnapshot.child("tamilword").getValue(String.class);
          String word = dataSnapshot.child("word").getValue(String.class);

        //open another acivity and pass these


        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });


        }
    });

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