recycleview生成无限循环

nkoocmlb  于 2021-07-03  发布在  Java
关注(0)|答案(2)|浏览(370)

我´我试图使一个商店被过滤搜索是由纺纱机。一开始我试着只用其中的两个,效果很好。当我试着要第三个的时候问题就来了。微调器数据用于在firebase数据库中进行搜索。我不知道´我不知道用两个或三个纺纱机有什么不同。如果有人能帮助我,我将非常感激
过滤式破碎机*

public class FilterFragment extends Fragment
implements AdapterView.OnItemSelectedListener{

public FilterFragment() {
    // Required empty public constructor
}

String txtBrand,txtKind, subKing;
Spinner spinBrand,spinKind, spinSubkind;
RecyclerView rv;
List<String>Brands;
List<String>Kinds;
List<Producto> productslist;
AdapterAllProducts adapterAllProducts;
public DatabaseReference mReference;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v =inflater.inflate(R.layout.filter_fragment, container, false);
    spinBrand=v.findViewById(R.id.spinnerMarca);
    spinKind=v.findViewById(R.id.spinnerTipoPieza);
    spinSubKind=v.findViewById(R.id.spinnerSubtipo);
    rv=v.findViewById(R.id.rvAvanzado);
    Brands= Arrays.asList(getResources().getStringArray(R.array.marcas));
    Kinds= Arrays.asList(getResources().getStringArray(R.array.tiposPiezas));
    mReference= FirebaseDatabase.getInstance().getReference("Product");
    spinBrand.setOnItemSelectedListener(this);
    spinKind.setOnItemSelectedListener(this);
    spinSubKind.setOnItemSelectedListener(this);
    return v;
}

public void loadRecycler(final String Marca, String Tipo, final String SubTipo){
    rv.setLayoutManager(new LinearLayoutManager(getContext()));
    productoslist= new ArrayList<>();
    adapterAllProducts = new AdapterAllProducts(productoslist,getActivity());
    adapterAllProducts.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Producto prod = productoslist.get(rv.getChildAdapterPosition(v));
            String pid = prod.getPid();
            Preferencias.setPid(getActivity(),pid);
            Fragment fragmentuser = new ProductsFragment();
            FragmentManager ManagerUser = getFragmentManager();
            FragmentTransaction transactionUser = ManagerUser.beginTransaction();
            transactionUser.replace(R.id.contentfragment,fragmentuser);
            transactionUser.commit();

        }
    });
    rv.setAdapter(adapterAllProducts);

    if (Brand.equals("Brands")&&Tipo.equals("Tipos")&&SubTipo.equals("subtipos")){
        mReferencia.child("Product").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                productoslist.clear();
                for (DataSnapshot Snapshot : snapshot.getChildren()
                ) {
                    Producto producto = Snapshot.getValue(Producto.class);
                    productoslist.add(producto);

                }
                adapterAllProducts.notifyDataSetChanged();

            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
        });
    }else{
        if(!Brand.equals("Brands")&&Tipo.equals("Tipos")&&SubTipo.equals("subtipos")){
            Query q = mReferencia.orderByChild("marca").equalTo(Marca);
            q.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot snapshot) {

                    productoslist.clear();
                    for (DataSnapshot Snapshot : snapshot.getChildren()
                    ) {
                        Producto producto = Snapshot.getValue(Producto.class);
                        productoslist.add(producto);
                    }
                    adapterAllProducts.notifyDataSetChanged();

                }

                @Override
                public void onCancelled(@NonNull DatabaseError error) {

                }
            });
        }
        else if (Brand.equals("Brands") && !Tipo.equals("Tipos")&&SubTipo.equals("subtipos")){
            Query q = mReferencia.orderByChild("tipo").equalTo(Tipo);
            q.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot snapshot) {

                    productoslist.clear();
                    for (DataSnapshot Snapshot : snapshot.getChildren()
                    ) {
                        Producto producto = Snapshot.getValue(Producto.class);
                        productoslist.add(producto);
                    }
                    adapterAllProducts.notifyDataSetChanged();

                }

                @Override
                public void onCancelled(@NonNull DatabaseError error) {

                }
            });
        }
        else if (Brand.equals("Brands") && !Tipo.equals("Tipos")&&!SubTipo.equals("subtipos")){
            Query q = mReferencia.orderByChild("tipo").equalTo(Tipo);
            q.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot snapshot) {

                    productoslist.clear();
                    for (DataSnapshot Snapshot : snapshot.getChildren()
                    ) {
                        Producto producto = Snapshot.getValue(Producto.class);
                        if (producto.getSubtipo().equals(SubTipo)) {
                            productoslist.add(producto);
                        }
                    }
                    adapterAllProducts.notifyDataSetChanged();

                }

                @Override
                public void onCancelled(@NonNull DatabaseError error) {

                }
            });
        }
        else if (!Brand.equals("Brands") && !Tipo.equals("Tipos")&&!SubTipo.equals("subtipos")){
            Query q = mReferencia.orderByChild("tipo").equalTo(Tipo);
            q.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot snapshot) {

                    productoslist.clear();
                    for (DataSnapshot Snapshot : snapshot.getChildren()
                    ) {
                        Producto producto = Snapshot.getValue(Producto.class);
                        if (producto.getMarca().equals(Marca)){
                            if (producto.getSubtipo().equals(SubTipo)) {
                                productoslist.add(producto);
                            }
                        }
                    }
                    adapterAllProducts.notifyDataSetChanged();

                }

                @Override
                public void onCancelled(@NonNull DatabaseError error) {

                }
            });
        }
    }

}

public String FillspinSubKind(String txtSubKind){
    switch (subTipo){
        case "boots":
            ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(),R.array.subkind_boots,R.layout.support_simple_spinner_dropdown_item);
            spinSubTipo.setAdapter(adapter);
            txtSubKind = spinSubTipo.getSelectedItem().toString();
            break;
        case "Sports":
            ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(getActivity(),R.array.subkind_sports,R.layout.support_simple_spinner_dropdown_item);
            spinSubTipo.setAdapter(adapter1);
            txtSubKind= spinSubTipo.getSelectedItem().toString();
            break;
        case "Formal":
            ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(getActivity(),R.array.subkind_formal,R.layout.support_simple_spinner_dropdown_item);
            spinSubTipo.setAdapter(adapter2);
            txtSubKind= spinSubTipo.getSelectedItem().toString();
            break;
        case "Merchandising":
            ArrayAdapter<CharSequence> adapter3 = ArrayAdapter.createFromResource(getActivity(),R.array.subkind_merch,R.layout.support_simple_spinner_dropdown_item);
            spinSubTipo.setAdapter(adapter3);
            txtSubKind= spinSubTipo.getSelectedItem().toString();
            break;
    }
    return txtSubTipo;
}
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
    txtMarca = spinMarca.getSelectedItem().toString();
    txtTipo = spinTipo.getSelectedItem().toString();
    FillspinSubKind(txtKind);
    loadRecycler(txtMarca,txtTipo,txtSubKind);
}

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

}}

我´我会感激任何帮助我的人。发帖资料:英语不是我的母语,所以我为我的英语水平道歉。我也试着翻译代码中的名字。

ql3eal8s

ql3eal8s1#

我决定感谢你的帮助,但最后我看到了我的无限循环,这是onitemclicklistener方法的问题,最后我把这个方法的数据改成了一个开关,它工作得很好。我就是这样做的

@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
    switch (adapterView.getId()){
        case R.id.spinnerBrand:
            txtBrand = spinBrand.getSelectedItem().toString();
            txtKind = spinKind.getSelectedItem().toString();
            fillRecycler(txtBrand ,txtKind ,txtSubKind);
            break;
        case R.id.spinnerKind:
            txtBrand = spinBrand.getSelectedItem().toString();
            txtKind = spinKind.getSelectedItem().toString();
            FillspinSubKind(txtKind );
            fillRecycler(txtBrand ,txtKind ,txtSubKind);
            break;

        case R.id.spinnerSubkind:
            txtBrand = spinBrand.getSelectedItem().toString();
            txtKind = spinKind.getSelectedItem().toString();
            txtSubKind = spinSubKind.getSelectedItem().toString();
            fillRecycler(txtBrand ,txtKind ,txtSubKind);
            break;
    }

}
rkttyhzu

rkttyhzu2#

是什么 spinSubTipo ? 在哪里示例化?如果我猜对了,一定是一样的 spinSubKind . 所以如果这是真的,你设定了一个 OnItemSelectedListener 为了 spinSubKind 而在 onItemSelected 你打电话来 FillspinSubKind 方法和步骤 FillspinSubKind 你打电话来 spinSubTipo.setAdapter 那个原因 OnItemSelectedListenerspinSubKind 将会触发循环。

相关问题