java – Change text in textview inside ViewPager2 on button click from Fragment-ThrowExceptions

Exception or error:

I have a fragment containing viewPager2 and a few buttons… ViewPager2 contains a textView whose text I want to change on button click(present inside fragment not viewPager2).

Interface

public interface IUpdateCursor {

    void cursorOn_nextWord();
    void cursorOn_nextLine();
    void cursorOn_nextTextBlock();

    void cursorOn_previousWord();
    void cursorOn_previousLine();
    void cursorOn_previousTextBlock();
}

Fragment

public class Read_fragment extends Fragment implements View.OnClickListener {

    private ImageButton imgbtn_back,imgbtn_left_top, imgbtn_right_top, imgbtn_left_down, imgbtn_right_down;
    private Button btn_done, btn_center_top, btn_center_down;
    private ViewPager2 viewPager;
    com.example.vision.HighlightedTextView textView;

    public Read_fragment() {
        setRetainInstance(true);
    }

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

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        viewPager.setAdapter(new ReadViewPagerAdapter(getContext()));
//        displayOCRText(getAllImages(Application_TEMP_DIR,
//                getFilenameFilter(new String[]{"jpeg"})));
    }

    private void initViews(@NonNull View view) {
        imgbtn_back = view.findViewById(R.id.imgbtn_back);
        imgbtn_left_top = view.findViewById(R.id.imgbtn_left_top);
        imgbtn_right_top = view.findViewById(R.id.imgbtn_right_top);
        imgbtn_left_down = view.findViewById(R.id.imgbtn_left_down);
        imgbtn_right_down = view.findViewById(R.id.imgbtn_right_down);
        btn_done = view.findViewById(R.id.btn_done);
        btn_center_top = view.findViewById(R.id.btn_center_top);
        btn_center_down = view.findViewById(R.id.btn_center_down);

        viewPager = view.findViewById(R.id.viewPager);
        textView = view.findViewById(R.id.textView5);

        imgbtn_back.setOnClickListener(this);
        imgbtn_left_top.setOnClickListener(this);
        imgbtn_right_top.setOnClickListener(this);
        imgbtn_left_down.setOnClickListener(this);
        imgbtn_right_down.setOnClickListener(this);
        btn_done.setOnClickListener(this);
        btn_center_top.setOnClickListener(this);
        btn_center_down.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()){
            case R.id.imgbtn_back       :{/*go back to preview without saving changes.*/navigateTo(R.id.action_read_fragment_to_preview_fragment);}break;
            case R.id.imgbtn_left_top   :{      /*previous word or alphabet.*/ imgbtn_left_top_CLICK();       }break;
            case R.id.imgbtn_right_top  :{      /*next word or alphabet.*/ imgbtn_right_top_CLICK();       }break;
            case R.id.imgbtn_left_down  :{      /*next sentence or paragraph*/  imgbtn_left_down_CLICK(view);      }break;
            case R.id.imgbtn_right_down :{      /*previous sentence or paragraph*/  imgbtn_right_down_CLICK(view);      }break;
            case R.id.btn_done          :{      /*apply changes and go back to preview.*/        }break;
            case R.id.btn_center_top    :{      /*mark in on first click, mark out on second click, then show cut, copy paste options.*/        }break;
            case R.id.btn_center_down   :{      /*Read from cursor location to end of page.*/        }break;
        }
    }

    private void navigateTo(@IdRes int actionResId){
        final NavController navController = Navigation.findNavController(Objects.requireNonNull(getActivity()), R.id.container);
        navController.navigate(actionResId);
    }

    private void imgbtn_left_top_CLICK(){
//        UpdateCursor.setUpdateCursor();
//        iUpdateCursor.updateCursor(false,true);
        IUpdateCursor iUpdateCursor = (IUpdateCursor) this.getContext();
        iUpdateCursor.cursorOn_previousWord();
    }

    private void imgbtn_right_top_CLICK(){
//        this.iUpdateCursor = (IUpdateCursor) viewPager.getRootView();
//        iUpdateCursor.updateCursor(true,true);

        IUpdateCursor iUpdateCursor = (IUpdateCursor) this.getContext();
        iUpdateCursor.cursorOn_nextWord();
    }

    private void imgbtn_left_down_CLICK(View view){
//        this.iUpdateCursor = (IUpdateCursor) view;
//        iUpdateCursor.updateCursor(false,false);
        IUpdateCursor iUpdateCursor = (IUpdateCursor) this;
        iUpdateCursor.cursorOn_previousLine();
    }

    private void imgbtn_right_down_CLICK(View view){
//        this.iUpdateCursor = (IUpdateCursor) view;
//        iUpdateCursor.updateCursor(true,false);
        IUpdateCursor iUpdateCursor = (IUpdateCursor) this;
        iUpdateCursor.cursorOn_nextLine();
    }
}

ViewPager Adapter

public class ReadViewPagerAdapter extends RecyclerView.Adapter<ReadViewPagerAdapter.ViewHolder>  {

    private LayoutInflater mInflater;
    private Vector<Bitmap> images;

    public ReadViewPagerAdapter(Context context) {
        this.mInflater = LayoutInflater.from(context);
        this.images = new Vector<>(5);
        this.images = getAllImages(Application_TEMP_DIR,
                getFilenameFilter(new String[]{"jpeg"}));

    }

    @NonNull
    @Override
    public ReadViewPagerAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.list_item_view_pager_read, parent, false);
        return new ReadViewPagerAdapter.ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ReadViewPagerAdapter.ViewHolder myHolder, int position) {
        //TODO: set image in imageView.
        Bitmap bitmap = images.get(position);
        new OCR(myHolder).displayOCRText(bitmap);
    }

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

    class ViewHolder extends RecyclerView.ViewHolder implements OCRcallbacks, IUpdateCursor{

        com.example.vision.HighlightedTextView textView;
        FirebaseVisionText OCRText;

        ViewHolder(View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.textView5);
        }

        @Override
        public void setText(FirebaseVisionText texts) {
            this.OCRText = texts;
            this.textView.setText(this.OCRText.getText());
            this.textView.setVisionText(texts);
        }


        @Override
        public void cursorOn_nextWord() {
            this.textView.cursorOn_nextWord();
        }

        @Override
        public void cursorOn_nextLine() {
            this.textView.cursorOn_nextLine();
        }

        @Override
        public void cursorOn_nextTextBlock() {
            this.textView.cursorOn_nextTextBlock();
        }

        @Override
        public void cursorOn_previousWord() {
            this.textView.cursorOn_previousWord();
        }

        @Override
        public void cursorOn_previousLine() {
            this.textView.cursorOn_previousLine();
        }

        @Override
        public void cursorOn_previousTextBlock() {
            this.textView.cursorOn_previousTextBlock();
        }
    }
}

Kindly help me. And if possible please give me an insight into interface implementation as listeners and callbacks and how does interface act as references.

How to solve:

I had similar issue.
I solve it by keeping a parentView pointer in my adapter and in adapter constructor i add a parameter named “parentView” (type: viewGroup) which set it.

So each time i need to have an access to parent button / another view i used:(and you can also add a click listener – i add an example below )

parentView.findViewById(R.id.Parent-View-Name);

in my adapter.


This is how it should work in your case:

ADAPTER

Step 1

 private ViewGroup parentView; // Add new Var
 // change/add new constracotr 
 public ReadViewPagerAdapter(Context context,ViewGroup parent) {
    this.mInflater = LayoutInflater.from(context);
    //******
    this.parentView = parent;
    //******
    this.images = new Vector<>(5);
    this.images = getAllImages(Application_TEMP_DIR,
            getFilenameFilter(new String[]{"jpeg"}));

}

Step 2
When ever you would like to find your button just use “parentView.findViewById”

(ImageButton)parentView.findViewById(R.id.fragment_button).setOnClickListener(v1 -> {// Put Here what ever you want to happen when click happen}

I hope i help 🙂 let me know

Leave a Reply

Your email address will not be published. Required fields are marked *