简体   繁体   English

使用 Firebase 中的数据创建 Listview

[英]Create Listview using data from Firebase

I should see in all the "Incontro" field that are in my database (They are events to understand us), see with the user who is logged in at that moment which of these events participates, his Id is in Incontro -> 123123... .-> "Participants" -> "here the id".我应该在我的数据库中的所有“Incontro”字段中看到(它们是了解我们的事件),与当时登录的用户一起查看其中哪些事件参与,他的 ID 在 Incontro -> 123123 中。 .. .->“参与者”->“这里是 id”。 Basically I have to create a listview of all the events that participates and show in the list the various data of that event.基本上我必须创建所有参与的事件的列表视图,并在列表中显示该事件的各种数据。 How can I do?我能怎么做? I put below the code that I managed to do but when I start nothing appears.我把我设法做的代码放在下面,但是当我开始时什么都没有出现。

Database example: https://imgur.com/a/dD4sTFn数据库示例: https://imgur.com/a/dD4sTFn

public class ListaIncontriFragment extends AppCompatActivity {
    
    ListView listView;
    ArrayList<String> arrayList = new ArrayList<>();
    ArrayAdapter<String> arrayAdapter;
    private List<Incontro> Incontri;

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

    String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();

    Incontri = new ArrayList<>();
    listView=(ListView) findViewById(R.id.list_view);
    arrayAdapter= new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,arrayList);
    listView.setAdapter(arrayAdapter);

        for(Incontro incontro : Incontri) {

            DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Incontro")
                    .child(incontro.getIncontroidId())
                    .child("Partecipanti");
            DatabaseReference uidRef = reference.child(uid);

            ValueEventListener valueEventListener = new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    if (dataSnapshot.exists()) {
                        String value = dataSnapshot.getValue(Incontro.class).toString();

                        arrayList.add(value);
                        arrayAdapter.notifyDataSetChanged();

                        

                    }
                }

                @Override
                public void onCancelled(@NonNull DatabaseError error) {
                    Log.d("TAG", error.getMessage());
                }
            };
            uidRef.addListenerForSingleValueEvent(valueEventListener);

        }
    }



}

So it would be a good idea to paste your ArrayAdapter code as well.因此,最好也粘贴您的 ArrayAdapter 代码。 A few steps to solve this:解决这个问题的几个步骤:

  1. In your ArrayAdapter class, I'd verify that you've implemented a getItem() method that returns the correct data type from your arrayList.在您的 ArrayAdapter class 中,我将验证您是否已实现 getItem() 方法,该方法从 arrayList 返回正确的数据类型。

  2. It also wouldn't hurt to add a method to again set the arrayList in your Fragment to your adapter's own list.添加一个方法来再次将 Fragment 中的 arrayList 添加到适配器自己的列表中也没有什么坏处。 And do that right before you call notifyDataSetChanged()并在您调用 notifyDataSetChanged() 之前执行此操作

  3. Worst case, you reinitialize your adapter again before that call to verify your adapter is actually working.最坏的情况是,您在调用之前再次重新初始化您的适配器以验证您的适配器是否实际工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM