-2

well im doing a recycler view with a database on firebase . that is working perfectly... now i want to implement a share button but i cant make the button reference each img. i found a tutorial to do so but when i try to implement this line (if i remove it the apps goes well but doest reference to the button position)

public BlogViewHolder(View itemView) {    
          ...
           button = (Button)mView.findViewById(R.id.share);
               ...

the app crashes when i open the activity im only using toast to testing.

public class NoticiasActivity extends AppCompatActivity {

    private RecyclerView mBlogList;
    FirebaseDatabase database;
    DatabaseReference myRef;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_noticias);

        //Recycler View
        mBlogList = (RecyclerView)findViewById(R.id.blog_list);
        mBlogList.setHasFixedSize(true);
        mBlogList.setLayoutManager(new LinearLayoutManager(this));

        // Send a Query to the database
        database = FirebaseDatabase.getInstance();
        myRef = database.getReference("Data");
    }

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

        FirebaseRecyclerAdapter<ModelClass, BlogViewHolder> firebaseRecyclerAdapter =
                new FirebaseRecyclerAdapter<ModelClass, BlogViewHolder>(
                        ModelClass.class,
                        R.layout.design_row,
                        BlogViewHolder.class,
                        myRef)  {



                    @Override
                    protected void populateViewHolder(BlogViewHolder viewHolder, ModelClass model,int position) {
                        viewHolder.setTitle(model.getTitle());
                        viewHolder.setImage(getApplicationContext(), model.getImage());
                        viewHolder.button.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                Toast.makeText(view.getContext(), "esto es un boton"+ view.getNextFocusUpId(), Toast.LENGTH_SHORT).show();

                            }
                        });
                    }
                };
        mBlogList.setAdapter(firebaseRecyclerAdapter);
    }
    //View Holder For Recycler View
    public static class BlogViewHolder extends RecyclerView.ViewHolder  {
        View mView;
        Button button ;

        public BlogViewHolder(View itemView) {

            super(itemView);
           button = (Button)mView.findViewById(R.id.share);





            mView= itemView;

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // click en la imagen hace algo
                    //añadir luego
                    Toast.makeText(v.getContext(), "hola", Toast.LENGTH_SHORT).show();

                }
            });

        }
        public void setTitle(String title){
            TextView post_title = (TextView)mView.findViewById(R.id.titleText);
            post_title.setText(title);
        }
        public void setImage(Context ctx , String image){
            ImageView post_image = (ImageView)mView.findViewById(R.id.imageViewy);
            // We Need TO pass Context
            Picasso.with(ctx).load(image).into(post_image);
        }    
    }
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • You should add Logcat to the question. https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – OneCricketeer Sep 06 '17 at 23:32

1 Answers1

3

You need to switch these lines

button = (Button)mView.findViewById(R.id.share);
mView= itemView;

Or find the button from the itemView

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245