'How can I 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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

