在选择微调器项目时不会填充ListView

时间:2016-12-20 13:47:15

标签: android listview firebase-realtime-database spinner android-arrayadapter

我制作了两个微调器 - 一个用于选择城市,另一个用于选择城市内的区域。我从Firebase数据库中选择后,我正在获取城市,区域和ListView的数据。 / p>

我在自定义ListView的帮助下填充ArrayAdapter,它将数据存储在ArrayList中,它将以“vendorsInformation”类对象的形式存储数据。

我从Firebase数据库处理数据的java类是:

public class Location_Selection extends Fragment {
    ArrayList<String> listVendorEmail=new ArrayList<>();
    ArrayList<String> listVendorName=new ArrayList<>();
    ArrayList<String> listVendorPhoneNo=new ArrayList<>();

    ArrayList<vendorsInfomation> list_All_info_vendor=new ArrayList<>();

    String categorySelected;

    String citySelected;
    String areaSelected;

    public  Location_Selection(String categorySelected)
    {
        this.categorySelected= categorySelected;

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.city_selection_page,container,false);

        final Spinner areaSelector=(Spinner)view.findViewById(R.id.select_area);
        final Spinner selectCity=(Spinner)view.findViewById(R.id.selectCity);


        ListView listView = (ListView) view.findViewById(R.id.displayAllAstrologers);
        final displayingListAfterAreas listOfVendorsAdapter=new displayingListAfterAreas(getContext(),0,list_All_info_vendor);
        listView.setAdapter(listOfVendorsAdapter);


        selectCity.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, final View view, int position, long id) {

                citySelected = selectCity.getSelectedItem().toString();


                final ArrayList<String> listAreas = new ArrayList<String>();


                String selectedCity = parent.getItemAtPosition(position).toString();
                FirebaseDatabase database = FirebaseDatabase.getInstance();
                DatabaseReference reference = database.getReference(selectedCity);

                reference.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {

                        if (dataSnapshot.hasChildren()) {
                            for (DataSnapshot child : dataSnapshot.getChildren()) {
                                String areaId = child.getKey().toString();
                                String areaName = child.getValue().toString();
                                listAreas.add(areaName);
                            }
                        }

                        ArrayAdapter<String> adapter = new ArrayAdapter(view.getContext(),
                                android.R.layout.simple_spinner_item, listAreas);

                        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                        areaSelector.setAdapter(adapter);
                    }


                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        areaSelector.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {


                areaSelected=areaSelector.getSelectedItem().toString();

                listVendorEmail.clear();
                listVendorPhoneNo.clear();
                listVendorName.clear();

                FirebaseDatabase database1 = FirebaseDatabase.getInstance();
                DatabaseReference referenceForEmail = database1.getReference(categorySelected + "-" + citySelected + "-" + areaSelected + "/email");

                referenceForEmail.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {

                        if (dataSnapshot.hasChildren()) {
                            for (DataSnapshot child : dataSnapshot.getChildren()) {

                                String getEmailNo = child.getKey().toString();
                                String getEmailId = child.getValue().toString();
                                listVendorEmail.add(getEmailId);
                            }
                        }


                        }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }

                });

                DatabaseReference referenceForName=database1.getReference(categorySelected + "-" + citySelected + "-" + areaSelected + "/name");

                referenceForName.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        if (dataSnapshot.hasChildren()) {
                            for (DataSnapshot child : dataSnapshot.getChildren()) {
                                String getNameId = child.getKey().toString();
                                String getName = child.getValue().toString();
                                listVendorName.add(getName);
                            }
                        }
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });


                DatabaseReference referenceForPhoneNo=database1.getReference(categorySelected+"-"+citySelected+"-"+areaSelected+"/phoneNo");

                referenceForPhoneNo.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        if(dataSnapshot.hasChildren())
                        {
                            for(DataSnapshot child: dataSnapshot.getChildren())
                            {
                                String getPhoneNoId=child.getKey().toString();
                                String getPhoneNo=child.getValue().toString();
                                listVendorPhoneNo.add(getPhoneNo);
                            }
                        }
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });
                for(int i=0;i<listVendorPhoneNo.size();i++)
                {
                    vendorsInfomation newObjectToStore=new vendorsInfomation(listVendorName.get(i),listVendorEmail.get(i),listVendorPhoneNo.get(i));
                    list_All_info_vendor.add(newObjectToStore);
                }
                listOfVendorsAdapter.notifyDataSetChanged();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        return  view;
    }
       }

我的自定义适配器类如下:

public class displayingListAfterAreas extends ArrayAdapter<vendorsInfomation> {


    Context context;
    ArrayList<vendorsInfomation> listDisplayingVendors;

    public displayingListAfterAreas(Context context, int resource, ArrayList<vendorsInfomation> objects) {

        super(context, resource, objects);
        this.context=context;
        listDisplayingVendors=objects;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v;
        if (convertView == null) {

            v = inflater.inflate(R.layout.format_of_list_of_categories, null);
        } else {
            v = convertView;
        }

        ImageView image_of_customer=(ImageView)v.findViewById(R.id.image_of_customer);
        TextView name_of_customer=(TextView)v.findViewById(R.id.name_of_customer);
        TextView email_of_customer=(TextView)v.findViewById(R.id.email_id_of_customer);
        TextView phone_of_customer=(TextView)v.findViewById(R.id.phone_no_of_customer);

        image_of_customer.setImageResource(R.drawable.customer_photo);
        name_of_customer.setText(listDisplayingVendors.get(position).getVendorName());
        email_of_customer.setText(listDisplayingVendors.get(position).getVendorEmailId());
        phone_of_customer.setText(listDisplayingVendors.get(position).getVendorPhoneNo());

        return v;


    }
}

我的vendorsInformation.java类所形成的对象存储在ArrayList中:

public class vendorsInfomation {

    String vendorName;
    String vendorEmailId;
    String vendorPhoneNo;

    public vendorsInfomation(String vendorName,String vendorEmailId,String vendorPhoneNo)
    {
        this.vendorName=vendorName;
        this.vendorEmailId=vendorEmailId;
        this.vendorPhoneNo=vendorPhoneNo;

    }

    public String getVendorName() {
        return vendorName;
    }

    public String getVendorEmailId() {
        return vendorEmailId;
    }

    public String getVendorPhoneNo() {
        return vendorPhoneNo;
    }

    public void setVendorName(String vendorName) {
        this.vendorName = vendorName;
    }

    public void setVendorEmailId(String vendorEmailId) {
        this.vendorEmailId = vendorEmailId;
    }

    public void setVendorPhoneNo(String vendorPhoneNo) {
        this.vendorPhoneNo = vendorPhoneNo;
    }

}

我没有在视图中填充ListView,而调试流程时不会进入此for循环。

for(int i=0;i<listVendorPhoneNo.size();i++)
                {
                    vendorsInfomation newObjectToStore=new vendorsInfomation(listVendorName.get(i),listVendorEmail.get(i),listVendorPhoneNo.get(i));
                    list_All_info_vendor.add(newObjectToStore);


                }

因此list_All_info_vendor ArrayList中没有项目。

2 个答案:

答案 0 :(得分:1)

在Firebase方法完成之前,您的所有列表都是空的。

如果需要适配器来响应该数据,则需要设置数据存在的适配器。

例如

IEnumerable<X>[]

但是,理想情况下,您应该重新构建数据库,以便可以从单个数据库引用中获取每条信息,因为您可能无法保证for(DataSnapshot child: dataSnapshot.getChildren()) { String getPhoneNoId=child.getKey().toString(); String getPhoneNo=child.getValue().toString(); listVendorPhoneNo.add(getPhoneNo); } for(int i=0;i<listVendorPhoneNo.size();i++) { vendorsInfomation newObjectToStore=new vendorsInfomation(listVendorName.get(i),listVendorEmail.get(i),listVendorPhoneNo.get(i)); list_All_info_vendor.add(newObjectToStore); } listOfVendorsAdapter.notifyDataSetChanged(); listVendorName.get(i)对应于相同的数据,或者这些列表的长度是相同的,所以你最终会得到一个索引越界异常,因为你只是在电话号码的长度上循环

答案 1 :(得分:0)

由于Firebase使用异步任务。当您执行List循环时,for可能为空,因此它不会进入For循环。您需要在onDataChange方法中移动该循环。

DatabaseReference referenceForPhoneNo=database1.getReference(categorySelected+"-"+citySelected+"-"+areaSelected+"/phoneNo");

            referenceForPhoneNo.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    if(dataSnapshot.hasChildren())
                    {
                        for(DataSnapshot child: dataSnapshot.getChildren())
                        {
                            String getPhoneNoId=child.getKey().toString();
                            String getPhoneNo=child.getValue().toString();
                            listVendorPhoneNo.add(getPhoneNo);
                        }
                    }

 // Move this inside onDataChange.
                 for(int i=0;i<listVendorPhoneNo.size();i++)
            {
                vendorsInfomation newObjectToStore=new vendorsInfomation(listVendorName.get(i),listVendorEmail.get(i),listVendorPhoneNo.get(i));
                list_All_info_vendor.add(newObjectToStore);
            }
            listOfVendorsAdapter.notifyDataSetChanged();

                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });

        }