在Recyclerview点击监听器上更新值

时间:2017-02-18 14:42:09

标签: android firebase firebase-realtime-database firebaseui

我使用firebase开发app来上传文件并在Recyclerview中查看 当我点击任何项目应该带我到另一个活动并在webView中查看文件时问题是当我点击任何项目时将第一个文件上传到firebase,我需要当我点击任何项目时获取此项目的文件< / p>

public class postShows extends AppCompatActivity {

    private RecyclerView postlist;
    private DatabaseReference mdatabase;
    private FirebaseAuth auth;
    ImageView imagepostl;
    TextView textname;
    String  ma,mmmmmm;
    PostUtils m;


    Context context;
    public static String imageURL, fileType,fileNAme;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_post_shows);
        mdatabase = FirebaseDatabase.getInstance().getReference().child(PostNew.item);
        auth = FirebaseAuth.getInstance();
        imagepostl = (ImageView) findViewById(R.id.imagefilesource);
        textname = (TextView) findViewById(R.id.textfilename);
        postlist = (RecyclerView) findViewById(R.id.postRecycle);
        postlist.setHasFixedSize(true);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        postlist.setLayoutManager(linearLayoutManager);

    }

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

        FirebaseRecyclerAdapter<PostUtils, PostViewHolder> firebaseRecyclerAdapter = new
                FirebaseRecyclerAdapter<PostUtils, PostViewHolder>(
                        PostUtils.class,
                        R.layout.post_row,
                        PostViewHolder.class,
                        mdatabase


                )

                {


                    @Override
                    protected void populateViewHolder(PostViewHolder viewHolder, PostUtils m, int position) {
                        fileType = m.getfiletype();
                        fileNAme=m.getFileName();

                        //   viewHolder.setDesc(m.getDescription());
                        if (m.getfiletype().equals("application/vnd.openxmlformats-officedocument.wordprocessingml.document")) {
                            // imagepostl.setImageResource(R.drawable.word);
                            viewHolder.setFileName(m.getFileName());
                            viewHolder.setWord(imagepostl);
                            mmmmmm= viewHolder.setImageURL(ma,m);

                        } else if (m.getfiletype().equals("application/pdf")) {
                            viewHolder.setFileName(m.getFileName());
                            viewHolder.setPDF(imagepostl);
                            mmmmmm= viewHolder.setImageURL(ma,m);


                        } else if (m.getfiletype().contains("image")) {

                            viewHolder.setimage(imagepostl);
                            mmmmmm= viewHolder.setImageURL(ma,m);


                        } else {
                            viewHolder.setFileName(m.getFileName());
                            {
                                viewHolder.setimage(imagepostl);
                            }
                        }
                    }
                    @Override
                    public PostViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) {
                        PostViewHolder viewHolder = super.onCreateViewHolder(parent, viewType);
                        viewHolder.setOnClickListener(new PostViewHolder.ClickListener() {

                            @Override
                            public void onItemClick(View view, int position) {
                                Intent intent = new Intent(Kasittestbank.getAppContext(), Kasit.class);
                                intent.putExtra("imageURL",mmmmmm);
                                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                Kasittestbank.getAppContext().startActivity(intent);
                            }

                            @Override
                            public void onItemLongClick(View view, int position) {
                                Toast.makeText(Kasittestbank.getAppContext(), "Item long clicked at " + position, Toast.LENGTH_SHORT).show();

                            }
                        });
                        return viewHolder;
                    }
                };

        postlist.setAdapter(firebaseRecyclerAdapter);
    }


    public static class PostViewHolder extends RecyclerView.ViewHolder  {

        View mview;
        Bitmap bitmap;
        FileOpen fileOpen;


        public PostViewHolder(View itemView) {
            super(itemView);
            mview = itemView;
            mview.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mClickListener.onItemClick(v, getAdapterPosition());

                }
            });
        }

        private PostViewHolder.ClickListener mClickListener;

        //Interface to send callbacks...
        public interface ClickListener {
            public void onItemClick(View view, int position);

            public void onItemLongClick(View view, int position);
        }

        public void setOnClickListener(PostViewHolder.ClickListener clickListener) {
            mClickListener = clickListener;
        }



        public void setFileName(String name) {

            TextView textName = (TextView) mview.findViewById(R.id.textfilename);
            textName.setText(name);


        }



        public void setWord(ImageView imageResourses) {

            ImageView imageView = (ImageView) mview.findViewById(R.id.imagefilesource);

            imageView.setImageResource(R.drawable.word);


        }

        public void setPDF(ImageView imageResourses) {

            ImageView imageView = (ImageView) mview.findViewById(R.id.imagefilesource);

            imageView.setImageResource(R.drawable.pdf);


        }

        public void setimage(ImageView imageResourses) {

            ImageView imageView = (ImageView) mview.findViewById(R.id.imagefilesource);

            imageView.setImageResource(R.drawable.image);


        }
                public String setImageURL(String url, PostUtils maa){

                url=maa.getImageUrl();

                    return url;

                }


    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {


            case R.id.logut:


                Logut();
                startActivity(new Intent(getApplicationContext(), LoginActivity.class));
        }
        return super.onOptionsItemSelected(item);
    }

    public void Logut() {
        auth.signOut();
        finish();
    }

}

1 个答案:

答案 0 :(得分:0)

定义recyclerView时,应该覆盖的一个方法是onBindViewHolder。在该方法的内部,您可以定义recyclerView的每个元素的内容。

因此,如果您希望每行都可以点击,请认为您需要这样的内容:

@Override
public void onBindViewHolder(PostViewHolder postViewHolder, int i) {
  postViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          //Definde what happend when the item is clicked
          //To identify which row, you can use postViewHolder.getAdapterPosition();
        }
    });

}

如果有效,请告诉我。