搜索视图未显示任何数据

时间:2019-07-20 08:57:19

标签: javascript android-studio android-recyclerview adapter searchview

我有搜索视图,可在回收视图中搜索来自firestore的数据。 在youtube视频教程之后,我的搜索栏没有显示与用户输入匹配的数据。 当我在搜索栏中写东西时,它不会搜索任何东西,就像它只是一个文本视图一样。 请找到以下代码:

//我的搜索行为//

package com.example.boc.Pdf;

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;

import android.util.Log;
import android.widget.SearchView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.boc.ImageView.MyPdfAdapter;
import com.example.boc.R;
import com.example.boc.models.DownModel;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.QuerySnapshot;

import java.util.ArrayList;
import java.util.Objects;

public class pdfDownloader extends AppCompatActivity {
    FirebaseFirestore db;
    FirebaseAuth mAuth;
    FirebaseAuth.AuthStateListener mAuthListner;
    FirebaseUser mUser;
    RecyclerView mRecyclerView;
    SearchView searchView ;
    ArrayList<DownModel> downModelArrayList = new ArrayList<>();
    MyPdfAdapter myAdapter;
    public static final String TAG="LOGIN";



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        searchView = findViewById( R.id.searchView );
        setContentView( R.layout.activity_pdf_downloader);
        mAuth = FirebaseAuth.getInstance();
        mUser = FirebaseAuth.getInstance().getCurrentUser();
        mAuthListner = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                if (mUser != null) {
                    dataFromFirebase();
                }
                else
                {
                    Log.d(TAG,"AuthStateChanged:Logout");
                }

            }
        };


        setUpRV();
        setUpFB();
        dataFromFirebase();
    }
// allowing users to see the links ////
    protected void onStart() {
        super.onStart();
        if (mUser != null) {
            dataFromFirebase();
        }
        else
        {
            Log.d(TAG,"AuthStateChanged:Logout");
        }

        if  (searchView !=null ){
            searchView.setOnQueryTextListener( new SearchView.OnQueryTextListener() {
                @Override
                public boolean onQueryTextSubmit(String query) {
                    return false;
                }

                @Override
                public boolean onQueryTextChange(String newText) {
                    search(newText);
                    return true;
                }
            } );
        }

    }

    private void search (String str){
        ArrayList<DownModel> mylist = new ArrayList<>(  );
        for (DownModel object : downModelArrayList){
            if (object.get_name().toLowerCase().contains( str.toLowerCase() )) {
                mylist.add( object );
            }


        }
        MyPdfAdapter myPdfAdapter = new MyPdfAdapter(pdfDownloader.this ,mylist );
        mRecyclerView .setAdapter( myPdfAdapter );

    }



    private void dataFromFirebase() {
        if(downModelArrayList.size()>0)
            downModelArrayList.clear();


        db.collection("files")
                .get()
                .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<QuerySnapshot> task) {
                        for(DocumentSnapshot documentSnapshot: Objects.requireNonNull( task.getResult() )) {

                            DownModel downModel= new DownModel(documentSnapshot.getString("name"),
                                    documentSnapshot.getString("link"));
                            downModelArrayList.add(downModel);

                        }

                        myAdapter= new MyPdfAdapter(pdfDownloader.this,downModelArrayList);
                        mRecyclerView.setAdapter(myAdapter);
                    }
                })

                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Toast.makeText(pdfDownloader.this, "Error ;-.-;", Toast.LENGTH_SHORT).show();
                    }
                })

        ;


    }

    private void setUpFB(){
        db=FirebaseFirestore.getInstance();
    }

    private void setUpRV(){
        mRecyclerView= findViewById(R.id.recycle);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
    }



}

//我的适配器//

package com.example.boc.ImageView;

import android.app.DownloadManager;
import android.content.Context;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.boc.Pdf.pdfDownloader;
import com.example.boc.R;
import com.example.boc.models.DownModel;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.storage.FirebaseStorage;

import java.util.ArrayList;
import java.util.List;

import static android.os.Environment.DIRECTORY_DOWNLOADS;
import static com.example.boc.Pdf.pdfDownloader.TAG;

public class MyPdfAdapter extends RecyclerView.Adapter<MyViewHolder> {

    com.example.boc.Pdf.pdfDownloader pdfDownloader;
    MyViewHolder mViewholder;
    ArrayList<DownModel> downModels;
    FirebaseFirestore db;
    List<DownModel> itemList;


    public MyPdfAdapter(pdfDownloader pdfDownloader , ArrayList<DownModel> downModels) {
        this.pdfDownloader = pdfDownloader;
        this.downModels = downModels;


    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

        LayoutInflater layoutInflater = LayoutInflater.from(pdfDownloader.getBaseContext());
        View view = layoutInflater.inflate( R.layout.elements, null, false);


        return new MyViewHolder(view);
    }


    public void updateList (ArrayList<DownModel> downmodel2) {
        if (downModels != null && downModels.size() > 0) {
            downModels.clear();
            downModels.addAll( downmodel2 );
            this.notifyDataSetChanged();
        }
    }


    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder myViewHolder, final int i) {

        myViewHolder.mName.setText(downModels.get(i).getName());
        myViewHolder.deletePdfBtn.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Context context =myViewHolder.mName.getContext();
                if (context !=null){
                    FirebaseFirestore db = FirebaseFirestore.getInstance();
                    FirebaseStorage firebaseStorage = FirebaseStorage.getInstance();
                    String filenamed = (String) myViewHolder.mName.getText();
                    String url = downModels.get( i ).getLink() ;
                    firebaseStorage.getReferenceFromUrl( url ).delete()
                            .addOnSuccessListener( new OnSuccessListener<Void>() {
                                @Override
                                public void onSuccess(Void aVoid) {
                                    Log.d( TAG, "DocumentSnapshot successfully written!" );
                                }
                            } );
                    db.collection( "files" ).document(filenamed).delete()

                            .addOnSuccessListener( new OnSuccessListener<Void>() {
                                @Override
                                public void onSuccess(Void aVoid) {
                                    Log.d( TAG, "DocumentSnapshot successfully written!" );
                                    downModels.remove(i);
                                    notifyDataSetChanged();


                                }
                            } )
                            .addOnFailureListener( new OnFailureListener() {

                                @Override
                                public void onFailure(@NonNull Exception e) {
                                    Log.w( TAG, "Error writing document", e );

                                }
                            } );

                }

                else {
                    return ;


                }

            };
        });

        myViewHolder.mDownload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                downloadFile(myViewHolder.mName.getContext(),downModels.get(i).getName(),".pdf",DIRECTORY_DOWNLOADS,downModels.get(i).getLink());
            }
        });


    }

    public void downloadFile(Context context, String fileName, String fileExtension, String destinationDirectory, String url) {

        DownloadManager downloadmanager = (DownloadManager) context.
                getSystemService(Context.DOWNLOAD_SERVICE);
        Uri uri = Uri.parse(url);
        DownloadManager.Request request = new DownloadManager.Request(uri);

        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
        request.setDestinationInExternalFilesDir(context, destinationDirectory, fileName + fileExtension);

        downloadmanager.enqueue(request);
    }

    @Override
    public int getItemCount() {
        return downModels.size();
    }}

//我的下载模型//

package com.example.boc.models;

public class DownModel {

    String Name,Link;
    private String name,link;

    public String getName() {
        return Name;
    }

    public void setName(String name) {
        this.Name = name;
    }

    public String getLink() {
        return Link;
    }

    public void setLink(String link) { this.Link = link; }


    public String get_name() {
        return name;
    }


    public DownModel(String Name, String Link){
        this.Link=Link;
        this.Name=Name;
        this.name = name;


    }

}

看起来我在加载包括搜索输入在内的新列表时遇到了问题,任何人都知道要遵循的教程或是否有办法找到问题的根源?

0 个答案:

没有答案