Firebase RecyclerView Onclick具有更多数据

时间:2018-08-04 19:21:55

标签: android android-recyclerview

我一直在尝试在RecyclerView上获取Onclik事件,以显示RecyclerView在新活动中具有的相同数据,甚至包含以前没有的更多数据。现在我有姓名,电话号码之类的数据,但是我想添加更类似的网站...到目前为止,当我单击“视图”时,我尝试的代码崩溃了。我不知道怎么了有人可以帮忙吗?

这是我的主要片段

public class Entreprisesfrag extends android.support.v4.app.Fragment {

RecyclerView recyclerView;
DatabaseReference dbref;
FirebaseDatabase firebaseDatabase;
LinearLayout linearLayout;
LinearLayoutManager linearLayoutManager;

public Entreprisesfrag() {
    // Required empty public constructor
}
@Override
public void onCreate( Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);


}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_entreprises, container, 
false);

    recyclerView= view.findViewById(R.id.recv);

    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager (new LinearLayoutManager(getContext()));
    linearLayout= view.findViewById(R.id.entreprise_list_layout);

    firebaseDatabase= FirebaseDatabase.getInstance();
    dbref= firebaseDatabase.getReference("Entreprises");
return view;
}

@Override
public void onStart() {
    super.onStart();

FirebaseRecyclerAdapter<entreprises_class,ViewHolder>firebaseRecyclerAdapter= 
new FirebaseRecyclerAdapter<entreprises_class, ViewHolder>
            (entreprises_class.class,R.layout.entreprises_list, 
ViewHolder.class,dbref.orderByChild("nom")) {
        @Override
        protected void populateViewHolder(ViewHolder viewHolder, 
entreprises_class model, int position) {
            viewHolder.details(getActivity(), model.getNom(),model.getNum(), 
model.getSecteur(), model.getSpécialité(), model.getVille(), 
model.getImage());

        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) 
{

            ViewHolder viewHolder = super.onCreateViewHolder(parent, 
viewType);

            viewHolder.setOnClickListener(new ViewHolder.ClickListener() {
                @Override
                public void onItemClick(View view, int position) {

                    TextView textView= 
view.findViewById(R.id.entreprise_nom);
                    TextView textView1= 
view.findViewById(R.id.entreprise_secteur);
                    TextView textView2= 
view.findViewById(R.id.entreprise_sous_secteur);
                    TextView textView3= 
view.findViewById(R.id.entreprise_ville);
                    TextView textView4= 
view.findViewById(R.id.entreprise_num);


                    ImageView 
imageView=view.findViewById(R.id.entreprise_image);

                    String snom= textView.getText().toString();
                    String ssec= textView1.getText().toString();
                    String ssp= textView2.getText().toString();
                    String sv= textView3.getText().toString();
                    String snum= textView4.getText().toString();


                    Drawable drawable= imageView.getDrawable();

                    Bitmap bitmap=((BitmapDrawable)drawable).getBitmap();

                    Intent i= new Intent(view.getContext(), 
entProfileFull.class);
                    ByteArrayOutputStream byteArrayOutputStream= new 
ByteArrayOutputStream();

                    bitmap.compress(Bitmap.CompressFormat.PNG, 100, 
byteArrayOutputStream);

                    byte[] bytes= byteArrayOutputStream.toByteArray();

                    i.putExtra("image", bytes);
                    i.putExtra("nom", snom);
                    i.putExtra("secteur", ssec);
                    i.putExtra("spécialité", ssp);
                    i.putExtra("ville", sv);
                    i.putExtra("num", snum);
                    startActivity(i);






                }

                @Override
                public void onItemLongClick(View view, int position) {

                }
            });
            return viewHolder;
        }
    };

    recyclerView.setAdapter(firebaseRecyclerAdapter);
}

public void recherche(String texte){
    Query mQuery= dbref.orderByChild("spécialité");




FirebaseRecyclerAdapter<entreprises_class,ViewHolder>firebaseRecyclerAdapter= 
new FirebaseRecyclerAdapter<entreprises_class, ViewHolder>
            (entreprises_class.class,R.layout.entreprises_list, 
ViewHolder.class,mQuery) {
        @Override
        protected void populateViewHolder(ViewHolder viewHolder, 
entreprises_class model, int position) {
            viewHolder.details(getActivity(), model.getNom(),model.getNum(), 
model.getSecteur(), model.getSpécialité(), model.getVille(), 
model.getImage());

        }


        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) 
{

            ViewHolder viewHolder = super.onCreateViewHolder(parent, 
viewType);

            viewHolder.setOnClickListener(new ViewHolder.ClickListener() {
                @Override
                public void onItemClick(View view, int position) {

                    TextView textView= 
view.findViewById(R.id.entreprise_nom);
                    TextView textView1= 
view.findViewById(R.id.entreprise_secteur);
                    TextView textView2= 
view.findViewById(R.id.entreprise_sous_secteur);
                    TextView textView3= 
view.findViewById(R.id.entreprise_ville);
                    TextView textView4= 
view.findViewById(R.id.entreprise_num);


                    ImageView 
imageView=view.findViewById(R.id.entreprise_image);

                    String snom= textView.getText().toString();
                    String ssec= textView1.getText().toString();
                    String ssp= textView2.getText().toString();
                    String sv= textView3.getText().toString();
                    String snum= textView4.getText().toString();


                    Drawable drawable= imageView.getDrawable();

                    Bitmap bitmap=((BitmapDrawable)drawable).getBitmap();

                    Intent i= new Intent(view.getContext(), 
entProfileFull.class);
                    ByteArrayOutputStream byteArrayOutputStream= new 
ByteArrayOutputStream();

                    bitmap.compress(Bitmap.CompressFormat.PNG, 100, 
byteArrayOutputStream);

                    byte[] bytes= byteArrayOutputStream.toByteArray();

                    i.putExtra("image", bytes);
                    i.putExtra("nom", snom);
                    i.putExtra("secteur", ssec);
                    i.putExtra("spécialité", ssp);
                    i.putExtra("ville", sv);
                    i.putExtra("num", snum);
                    startActivity(i);






                }

                @Override
                public void onItemLongClick(View view, int position) {

                }
            });
            return viewHolder;
        }
    };

    recyclerView.setAdapter(firebaseRecyclerAdapter);

}



@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    menu.clear();
    inflater.inflate(R.menu.recherche_activity_menu_item, menu);
    MenuItem item = menu.findItem(R.id.action_search);

    SearchView searchView = (SearchView) item.getActionView();
    searchView.setQueryHint("Nom, Spécialité ou Ville");
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            recherche(query);

            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            recherche(newText);


            return false;
        }
    });

}


@Override
public boolean onOptionsItemSelected(MenuItem item) {

    if (item.getItemId()==R.id.recherche)
    {

    }
    else if (item.getItemId()==R.id.filtrer)
    { 
        FiltreDialog();
    }
    return super.onOptionsItemSelected(item);
}

private void FiltreDialog() {
    String [] Options={"Spécialité", "Ville"};
    AlertDialog.Builder builder= new AlertDialog.Builder(getContext());
    builder.setTitle("Afficher par:")
            .setIcon(R.drawable.ic_sort_black_24dp)
            .setItems(Options, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if(which==0){

FirebaseRecyclerAdapter<entreprises_class,ViewHolder>firebaseRecyclerAdapter= 
new FirebaseRecyclerAdapter<entreprises_class, ViewHolder>

(entreprises_class.class,R.layout.entreprises_list, 
ViewHolder.class,dbref.orderByChild("spécialité")) {
                            @Override
                            protected void populateViewHolder(ViewHolder 
viewHolder, entreprises_class model, int position) {
                                viewHolder.details(getActivity(), 
model.getNom(),model.getNum(), model.getSecteur(), model.getSpécialité(), 
model.getVille(), model.getImage());

                            }
                        };
                        recyclerView.setAdapter(firebaseRecyclerAdapter);

                    }
                    else if(which==1){

FirebaseRecyclerAdapter<entreprises_class,ViewHolder>firebaseRecyclerAdapter= 
new FirebaseRecyclerAdapter<entreprises_class, ViewHolder>

(entreprises_class.class,R.layout.entreprises_list, 
ViewHolder.class,dbref.orderByChild("ville")) {
                            @Override
                            protected void populateViewHolder(ViewHolder 
viewHolder, entreprises_class model, int position) {
                                viewHolder.details(getActivity(), 
model.getNom(),model.getNum(), model.getSecteur(), model.getSpécialité(), 
model.getVille(), model.getImage());

                            }
                        };
                        recyclerView.setAdapter(firebaseRecyclerAdapter);

                    }

                }
            });
    builder.show();
}

}

这是我的新活动

public class entProfileFull extends AppCompatActivity {

Toolbar mtoolbar;
TextView textView, textView1, textView2, textView3, textView4;
ImageView imageView;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ent_profile_full);

    mtoolbar=findViewById(R.id.toolbar);

    setSupportActionBar(mtoolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    textView=findViewById(R.id.nom_entreprise_full);
    textView1=findViewById(R.id.secteur_entreprise_full);
    textView2= findViewById(R.id.sp_entreprise_full);
    textView3=findViewById(R.id.entreprise_ville);
    textView4=findViewById(R.id.entreprise_num_full);



    imageView=findViewById(R.id.entreprise_image_full);


    byte[] bytes=getIntent().getByteArrayExtra("image");
    String nom= getIntent().getStringExtra("nom");
    String secteur= getIntent().getStringExtra("secteur");
    String spécialité= getIntent().getStringExtra("spécialité");
    String ville= getIntent().getStringExtra("ville");
    String num= getIntent().getStringExtra("num");



    Bitmap bm= BitmapFactory.decodeByteArray(bytes, 0, bytes.length);

    textView.setText(nom);
    textView1.setText(secteur);
    textView2.setText(spécialité);
    textView3.setText(ville);
    textView4.setText(num);


    imageView.setImageBitmap(bm);



}




public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
        case android.R.id.home:
            finish();
            return true;
    }
    return super.onOptionsItemSelected(item);
}

}

这是我的ViewHolder类

public class ViewHolder extends RecyclerView.ViewHolder {

View mView;

public ViewHolder(View itemView) {
    super(itemView);

    mView=itemView;

    itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            vclickListener.onItemClick(view, getAdapterPosition());
        }
    });

    itemView.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View view) {
            vclickListener.onItemLongClick(view, getAdapterPosition());
            return true;
        }
    });
}

public void details(Context cntx, String nom, String num, String secteur, 
String spécialité,String ville, String image){

    TextView textView= mView.findViewById(R.id.entreprise_nom);
    TextView textView1= mView.findViewById(R.id.entreprise_secteur);
    TextView textView2= mView.findViewById(R.id.entreprise_sous_secteur);
    TextView textView3= mView.findViewById(R.id.entreprise_ville);
    TextView textView4= mView.findViewById(R.id.entreprise_num);
    ImageView imageView= mView.findViewById(R.id.entreprise_image);

    textView.setText(nom);
    textView1.setText(secteur);
    textView2.setText(spécialité);
    textView3.setText(ville);
    textView4.setText(num);
    Picasso.get().load(image).into(imageView);


}
private ViewHolder.ClickListener vclickListener;

public interface ClickListener{
    void onItemClick(View view, int position);
    void onItemLongClick(View view, int position);

}
public void setOnClickListener(ViewHolder.ClickListener clickListener){
    vclickListener = clickListener;

}

}

1 个答案:

答案 0 :(得分:0)

您不应从ViewHolder构造函数中调用itemView.setOnClickListener()。因为ViewHolder构造函数是从onCreateViewHolder方法调用的。 Henc,您将没有这个职位。因此,将itemView.setOnClickListener()方法保留在onBindView()方法内

这将解决问题