在第一次query1.addevent监听器没有被调用

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在第一次query1.addevent监听器没有被调用相关的知识,希望对你有一定的参考价值。

我必须使用firebase在android中开发一个聊天应用程序,它由不同的组组成,可以选择离开和加入组,第一次它不显示已加入和已离开的组,但第二次滚动列表视图或更改视图时它显示正确。我必须解决问题,请查看以下代码并显示应修复哪些问题。

public class Allgroups extends AppCompatActivity 
    private DatabaseReference mDatabaseRef;
    private List<ImageUpload> imgList;
    private List<groupuserstoken> imgList1;
    ListView listView;
    private ImageListAdapter adapter;
    private DatabaseReference databaseReference;
    ProgressDialog progressDialog;


    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_allgroups);
        progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("Please wait loading groups...");

        listView=(ListView)findViewById(R.id.allgroups11);
        mDatabaseRef = FirebaseDatabase.getInstance().getReference();
        imgList=new ArrayList<>();
        final Query query = mDatabaseRef.child("Groups");
        listView.smoothScrollByOffset(10);
        query.addValueEventListener(new ValueEventListener() 
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) 
                for (DataSnapshot snapshot : dataSnapshot.getChildren()) 
                    ImageUpload img = snapshot.getValue(ImageUpload.class);
                    imgList.add(img);
                

                adapter = new ImageListAdapter(Allgroups.this, R.layout.custommenu, imgList);
                adapter.notifyDataSetChanged();
                listView.setAdapter(adapter);
            
            @Override
            public void onCancelled(DatabaseError databaseError) 

            
        );
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() 
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) 
                String name=imgList.get(i).getName();
                String path=imgList.get(i).getUrl();
                String topic=imgList.get(i).getTopic();
                Intent intent= new Intent(getApplicationContext(),Main4Activity.class);
                intent.putExtra("name",name);
                intent.putExtra("path",path);
                intent.putExtra("topic",topic);
                startActivity(intent);
            
        );
    





 public class ImageListAdapter extends ArrayAdapter<ImageUpload> 
        private Activity context;
        private int resource;
        private List<ImageUpload> listImage;

        private DatabaseReference mDatabaseRef;
        private List<groupuserstoken> imgList1;
        ProgressDialog progressDialog;
        int pos;




        public ImageListAdapter(@NonNull Activity context, @LayoutRes int resource, @NonNull List<ImageUpload> objects) 
            super(context, resource, objects);
            this.context = context;
            this.resource = resource;
            listImage = objects;
        


             TextView tvName;
               TextView tvDes;
            TextView statuss;
             Button leave;
             ImageView img;





        @NonNull
        @Override
        public View getView(final int position, @Nullable  View convertView, @NonNull ViewGroup parent) 

            View v = convertView;

            if (v == null) 
                LayoutInflater vi;
                vi = LayoutInflater.from(getContext());
                v = vi.inflate(R.layout.custommenu, null);
            
          //  final LayoutInflater inflater = context.getLayoutInflater();
         //   v = LayoutInflater.from(getContext()).inflate(R.layout.custommenu, parent, false);

            ImageUpload listImage1 = getItem(position);

            if (listImage1 !=null) 


                tvName = (TextView) v.findViewById(R.id.as);
                tvDes = (TextView) v.findViewById(R.id.aa);
                statuss = (TextView) v.findViewById(R.id.users);
                img = (ImageView) v.findViewById(R.id.addgroup1);
                leave = (Button) v.findViewById(R.id.leavegroup);

                mDatabaseRef = FirebaseDatabase.getInstance().getReference();
                final String name = listImage.get(pos).getName();
                final Query query1 = mDatabaseRef.child(name + "user");
                query1.addValueEventListener(new ValueEventListener() 
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) 

                        imgList1 = new ArrayList<>();
                        for (DataSnapshot snapshot : dataSnapshot.getChildren()) 
                            groupuserstoken img = snapshot.getValue(groupuserstoken.class);
                            imgList1.add(img);
                        
                        String token = FirebaseInstanceId.getInstance().getToken();
                        if (!(imgList1.isEmpty())) 

                            if (!(imgList1.contains(token))) 
                                leave.setText("Leave Group");
                            
                        


                    

                    @Override
                    public void onCancelled(DatabaseError databaseError) 


                    
                );

                tvName.setText(listImage1.getName());
                tvDes.setText(listImage1.getDescription());
                statuss.setText(listImage1.getMembers() + " Users ");
                Glide.with(context).load(listImage1.getUrl()).into(img);
            


            leave.setOnClickListener(new View.OnClickListener() 
                @Override
                public void onClick(View v) 
                    String groupname = listImage.get(position).getName();
                    mDatabaseRef = FirebaseDatabase.getInstance().getReference().child(groupname+"user").child(FirebaseInstanceId.getInstance().getToken());
                    mDatabaseRef.child("token").setValue(FirebaseInstanceId.getInstance().getToken());
                
            );
            return v;
        
    
答案

请在适配器中使用setTag()getTag(),但更好的做法是使用RecyclerView而不是ListView

以上是关于在第一次query1.addevent监听器没有被调用的主要内容,如果未能解决你的问题,请参考以下文章

即使事件被触发,Laravel 监听器也没有被触发

商店的变更监听器没有在 componentWillUnmount 上被删除?

oracle配置监听时提示端口被占用

vue+mousemove移入事件只触发一次怎么设置?

如何删除 addClsOnOver 监听器 ExtJS

Rabbit 侦听器方法没有被传入消息调用