【发布时间】:2017-07-20 21:46:34
【问题描述】:
我正在使用 FirebaseListAdapter 开发一个列表应用程序。我的 getKey() 值有问题。我想访问被单击的列表项下的数据并将其显示在另一个活动的文本视图中。但我不知道该怎么办?
这是我的代码现在的样子
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);
}
});
}
当我单击任何列表项时,我将获得最后添加的故事的故事值。
这是我要显示故事值的另一个活动的代码。
@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;
}}
【问题讨论】:
-
显示您的
Story课程 -
@PhátPhát 添加了 stroy.class
标签: android firebase firebase-realtime-database