我正在開發一個使用FirebaseListAdapter的列表應用程序。我遇到了getKey()值的問題。我想訪問已被點擊的列表項目下的數據,並將其顯示在另一個活動的文本視圖中。但無法弄清楚該怎麼辦?Firebase使用鍵值獲取數據
這裏是我的代碼看起來像現在
myList.setAdapter(myAdapter);
myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Story st = (Story)parent.getItemAtPosition(position);
String stry = st.Story;
Toast.makeText(MainActivity.this, ""+stry,Toast.LENGTH_LONG).show();
Intent intent = new Intent(context, MyStoryActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Bundle bundle = new Bundle();
bundle.putString("Story", stry);
intent.putExtras(bundle);
context.startActivity(intent);
}
});
}
我得到最後添加的故事劇情值當我點擊任何列表項的。
下面是我想要顯示Story的值的另一個活動的代碼。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.story_view);
database = FirebaseDatabase.getInstance();
ref = database.getReference("Stories");
ttl = (TextView) findViewById(R.id.title);
stry = (TextView) findViewById(R.id.story);
Bundle bundle = getIntent().getExtras();
stry1 = bundle.getString("Story");
ref.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Story ad = dataSnapshot.getValue(Story.class);
ttl.setText(ad.Title);
stry.setText(stry1);
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
這裏是Story.class
public class Story {
public String Title;
public String Story;
// Default constructor required for calls to
// DataSnapshot.getValue(User.class)
public Story() {
}
public Story(String ttl, String stry) {
this.Title = ttl;
this.Story = stry;
}}
顯示你的'故事'類 –
@PhátPhát添加stroy.class – ASAP