【问题标题】:On first time query1.addevent listener not getting called在第一次 query1.add 事件监听器没有被调用
【发布时间】:2017-12-22 00:30:33
【问题描述】:

我必须使用 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;
        }
    }

【问题讨论】:

    标签: java android firebase firebase-realtime-database


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多