【问题标题】:Retrieve data from Android Firebase , display it to a listview and apply onItemClickListener to the ListView从 Android Firebase 检索数据,将其显示到 listview 并将 onItemClickListener 应用于 ListView
【发布时间】:2018-11-21 22:43:31
【问题描述】:

我是 Android 开发新手。

我想从 Firebase 中检索所有标题(Title 1, Title 2, Title 3, Title 4)并显示在我能够做到的listview 中。但是我想将onItemClickListener 应用于listview,并且我希望当我单击listview 中的特定项目时,下一个活动应该向我显示特定内容。 例如,如果我单击Title 1,则下一个活动应显示Content 1。如果Title 2 然后Content 2

请说明我该怎么做。`

advaitavedanta-f1443
Chapter 1
description: "Content 1"
title: "Title 1"
Chapter 2
description: "Content 2"
title: "Title 2"
Chapter 3
description: "Content 3" 
title: "Title 3"
Chapter 4
description: "Content 4"
title: "Title 4"
//this is my data from firebase//



private static final String TAG = "MainActivity";
private FirebaseDatabase mDatabase;
private DatabaseReference mDbRef;

String Title;
String Description;
ListView listView;
ArrayList<String> mybook = new ArrayList<>();
ArrayAdapter<String> arrayAdapter;
Firebase url;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mDatabase = FirebaseDatabase.getInstance();
    mDbRef = mDatabase.getReference("Chapter 1");
    User user = new User("Title 1", "Content 1");
    mDbRef.setValue(user);

    mDbRef = mDatabase.getReference("Chapter 2");
    user = new User("Title 2", "Content 2");
    mDbRef.setValue(user);

    mDbRef = mDatabase.getReference("Chapter 3");
    user = new User("Title 3", "Content 3");
    mDbRef.setValue(user);

    mDbRef = mDatabase.getReference("Chapter 4");
    user = new User("Title 4", "Content 4");
    mDbRef.setValue(user);

    Firebase.setAndroidContext(this);

    listView =  findViewById(R.id.listView);
    arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, notes);
    listView.setAdapter(arrayAdapter);

    url = new Firebase("https://advaitavedanta-f1443.firebaseio.com/");

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent intent = new Intent(getApplicationContext(),MainContent.class);
            intent.putExtra("noteId", Description);

        }
    });
    url.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {

            Title = dataSnapshot.child("title").getValue(String.class);
            mybook.add(Title);
            Description = dataSnapshot.child("description").getValue(String.class);

        }

        @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(FirebaseError firebaseError) {

        }
    });
}

}`

【问题讨论】:

  • 您已经发布了您拥有的代码,但您收到的具体问题是什么?代码不起作用吗? 有什么问题?
  • 我在列表视图中获取数据,但是当我单击任何项​​目时我没有获取数据。下一个活动是空白的。我想知道,当我单击列表视图中的任何项目时,它应该在下一个活动中显示相应的内容(假设我单击“标题 1”下一个活动应该显示“内容 1”。请参阅顶部的 firebase 数据。
  • 好的。我知道如何让这个工作。创建您自己的 Adapter 类并在您的 Adapter 类的 getView 方法中,设置 onClickListener 中的内容。您还需要将应用程序上下文作为构造函数的参数之一。
  • This article 很好地解释了如何制作自定义适配器类。

标签: firebase android-studio listview


【解决方案1】:

由于您在添加 firebase 数据时使用 User 对象,因此请使用用户对象列表在添加的孩子身上检索它。

制作用户列表

ArrayList<User> users = new ArrayList<>();

然后在你的孩子身上添加这样做

String title = dataSnapshot.child("title").getValue(String.class);
String description= dataSnapshot.child("description").getValue(String.class);
users.add(new User(title, description));

最后,在你的列表上点击修改额外的

Intent intent = new Intent(getApplicationContext(),MainContent.class);
intent.putExtra("content_description", users.get(position).getDescription());
startActivity(intent);

注意:您也可以将整个对象放入intent中,但您需要使User对象实现parcelable。

建议:使用适当的变量名和适当的类名。相信我,这很重要。例如,Title 和 Content 不应该在 User 类中,它更像是在 Book 类中。

希望这能启发你的伙伴,干杯

【讨论】:

    【解决方案2】:

    我发现您的代码存在潜在问题。 在你的,

    listView.setOnItemClickListener(...) , onItemClick()

    没有实际启动新活动的代码。 你只是在形成Intent

    之后,

    意图意图 = new Intent(getApplicationContext(),MainContent.class);
    intent.putExtra("noteId", Description);

    你应该添加,

    开始活动(意图);

    希望能帮到你。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-20
      • 2019-01-12
      • 1970-01-01
      相关资源
      最近更新 更多