Firebase使用键值获取数据

时间:2017-07-20 04:37:15

标签: android firebase firebase-realtime-database

我正在使用FirebaseListAdapter开发一个列表应用程序。我遇到了getKey()值的问题。我想访问已单击的listitem下的数据,并将其显示在另一个活动的textview中。但是不知道该怎么办?

以下是我的代码现在的样子

   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) {

        }
    });

}

这就是我的数据库的样子 enter image description here

这是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;
}}

2 个答案:

答案 0 :(得分:0)

使用<%= f.select :url_id, @urls, {}, class: 'form-control' %> 将对象发送到另一个Parcelable

关注此tutorial

答案 1 :(得分:0)

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

            Story story = myAdapter.getItem(position);
            String st = story.getStory();
            String tt = story.getTitle();


            Intent intent = new Intent(context, MyStoryActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            Bundle bundle = new Bundle();
            bundle.putString("Story", st);
            bundle.putString("Title",tt);
            intent.putExtras(bundle);
            context.startActivity(intent);
        }
    });
}

我通过将Story.class的变量定义到项目位置来获得解决方案,该项目位置已被点击并通过变量获取数据。