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

时间:2017-12-21 08:43:39

标签: java android firebase firebase-realtime-database

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

1 个答案:

答案 0 :(得分:0)

请在适配器中使用setTag()getTag(),但为了更好的做法,请使用RecyclerView代替ListView