Ad

Button.setText() Does Not Work Inside OnClick() Method

I have a view pager where each page has a cardview with an image, some text, and a button. I need the button text of each page to change after being clicked. So inside ViewPagerAdapter mapSize.setText() works only outside setOnClickListener(), while inside onClick it does not work. This is my ViewPagerAdapter class

public class ViewPagerAdapter extends PagerAdapter {

    private List<Model> models;
    private LayoutInflater layoutInflater;
    private Context context;
    private Button mapSize;
    private Integer size;
    private static final String TAG = "ViewPagerAdapter";

    public ViewPagerAdapter(Context context, List<Model> models, String playerName) {
        this.models = models;
        this.layoutInflater = layoutInflater;
        this.context = context;
    }

    @Override
    public int getCount() {
        return models.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
        return view.equals(o);
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, final int position) {
        layoutInflater = LayoutInflater.from(context);
        View view = null;

        ImageView imageView;
        TextView title, desc;
        imageView = view.findViewById(R.id.gameModeImage);
        title = view.findViewById(R.id.title);
        desc = view.findViewById(R.id.description);
        imageView.setImageResource(models.get(position).getImage());
        title.setText(models.get(position).getTitle());
        desc.setText(models.get(position).getDescription());
        mapSize = view.findViewById(R.id.mapSizeBtn);

        mapSize.setText("hello2"); //here works

        mapSize.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mapSize.setText("hello"); //here does not work
            }
        });
        container.addView(view, 0);

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "This page was clicked: " + position);
                    if (position != 1)
                        Toast.makeText(context, "Coming Soon.", Toast.LENGTH_SHORT).show();
                    else {
                        Intent newGameActivity = new Intent(context, com.game.project.GameActivity.class);
                        newGameActivity.putExtra("size", size);
                        context.startActivity(newGameActivity);
                    }
                }
        });


        return view;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View)object);
    }



}
Ad

Answer

You reassign your button each item a new item instantiates in your adapter. You must reffer to your button in this way:

@NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, final int position) {
        layoutInflater = LayoutInflater.from(context);
        View view = null;

        ImageView imageView;
        TextView title, desc;
        imageView = view.findViewById(R.id.gameModeImage);
        title = view.findViewById(R.id.title);
        desc = view.findViewById(R.id.description);
        imageView.setImageResource(models.get(position).getImage());
        title.setText(models.get(position).getTitle());
        desc.setText(models.get(position).getDescription());
        final Button mapSize = view.findViewById(R.id.mapSizeBtn);

        mapSize.setText("hello2"); //here works

        mapSize.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mapSize.setText("hello"); //here does not work
            }
        });
        container.addView(view, 0);

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "This page was clicked: " + position);
                    if (position != 1)
                        Toast.makeText(context, "Coming Soon.", Toast.LENGTH_SHORT).show();
                    else {
                        Intent newGameActivity = new Intent(context, com.game.project.GameActivity.class);
                        newGameActivity.putExtra("size", size);
                        context.startActivity(newGameActivity);
                    }
                }
        });


        return view;
    }
Ad
source: stackoverflow.com
Ad