如何从firebase获取数据时使过滤器工作

时间:2018-04-02 06:35:20

标签: android firebase firebase-realtime-database android-alertdialog

以下代码用于打开AlertDialog,使用户可以在警报对话框中的项目上搜索数据

public void FilterIcon(){


    AlertDialog.Builder builder = new 
    AlertDialog.Builder(getActivity());

    selecedItem = new ArrayList<>();
    builder.setCancelable(false);
    builder.setCustomTitle(LayoutInflater.from(getActivity()).inflate(R.layout.dialog_layout,null))

            .setSingleChoiceItems(R.array.Category,-1,  new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    switch (i){

                        case  0:
                            Selection = 0;
                            Toast.makeText(getActivity(),"BOOKS",Toast.LENGTH_SHORT).show();
                            break;

                        case 1:
                            Selection =1;
                            selecedItem.add("PC GAMES");
                            Toast.makeText(getActivity(),"PC  GAMES",Toast.LENGTH_SHORT).show();
                            break;

                        case 2:
                            Selection = 2;
                            selecedItem.add("PS3 GAMES");
                            Toast.makeText(getActivity(),"PS3 GAMES",Toast.LENGTH_SHORT).show();
                            break;

                        case 3:
                            Selection =3;
                            selecedItem.add("PS4 GAMES");
                            Toast.makeText(getActivity(),"PS4 GAMES",Toast.LENGTH_SHORT).show();
                            break;
                    }

                }



            });
    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int id) {

            selecedItem.clear();




        }
    })
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {

                    dialog.cancel();
                }
            });







    AlertDialog dialog =  builder.create();

    dialog.show();
}


public int getSelection() {
    return Selection;
}

我创建了getSelection()方法,以便知道用户选择了什么,并将该值传递给另一个activity.So如果用户按下了索引0处的项目,则选择设置为0.其他活动是负责搜索的人。其他活动的代码是:

package com.example.database.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.database.R;
import com.example.database.models.Post;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;

import java.util.ArrayList;

public class RecentPostsFragment extends PostListFragment {

private RecyclerView mRecycler;
private DatabaseReference myDatabase;

public int selection;



public RecentPostsFragment(){


}



@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    myDatabase = FirebaseDatabase.getInstance().getReference();

    mRecycler = (RecyclerView)view.findViewById(R.id.messages_list);

}

@Override
public Query getQuery(DatabaseReference databaseReference) {

    Log.d("TAG", "getQuery: Selection"+ getSelection());

    if(getSelection()==0){

        return databaseReference.child("Book");



    }else if(getSelection()==1) {

        return databaseReference.child("PC GAMES");
    }
    else if(getSelection()==2){

        return  databaseReference.child("PS3 GAMES");
    }

    else {

        return databaseReference.child("PS4 GAMES");
    }


}


@Override
public void setLayout() {
    super.setLayout();
    StaggeredGridLayoutManager mManager = new StaggeredGridLayoutManager(2,1);
    mManager.setReverseLayout(true);
    mRecycler.setLayoutManager(mManager);
}

}

我面临的问题是选择始终为零......我似乎可以将getSelection()返回的值作为另一个值。它总是返回零。但是一段时间后创建活动再次它调用正确的值我怎样才能使它成为过滤器瞬间

0 个答案:

没有答案