hamed sm
hamed sm

Reputation: 1

How to make changes in PagerAdapter using events ؟

I am new in programming and I have created a slider using pager adapter in which I want to receive the contents of the edit text that is inside the slider through an event.

public class Slider_Listening_Adapter extends PagerAdapter {

    public TextView Txt_Listening_Text;
    EditText Edt_Listening;
    ImageView Img_Listening_Play;
    Button Btn_Listening_Next;
    Context C;
    ArrayList<Listening_Array> Listening = new ArrayList<>();
    String Text;

    public Slider_Listening_Adapter(Context c, ArrayList<Listening_Array> listening) {
        C = c;
        Listening = listening;


    }

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

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

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

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {

        View view = LayoutInflater.from(C).inflate(R.layout.listening_layout, container, false);


        Txt_Listening_Text = view.findViewById(R.id.txt_listening_text);
        Edt_Listening = view.findViewById(R.id.edt_listening);
        Img_Listening_Play = view.findViewById(R.id.img_listening_play);
        Btn_Listening_Next = view.findViewById(R.id.btn_listening_next);

        Text = Listening.get(position).getFull_Text().replace(Listening.get(position).getVacancy(), ".........");

        Txt_Listening_Text.setText(Listening.get(position).getFull_Text().replace(Listening.get(position).getVacancy(), "........."));

        Btn_Listening_Next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Txt_Listening_Text.setTextColor(Color.RED);
                Toast.makeText(C, "it is ok ", Toast.LENGTH_SHORT).show();
            }
        });
        container.addView(view);


        return view;

    }

public class Activity_Litening extends AppCompatActivity {

    ViewPager View_Pager_Listening;
    ArrayList<com.example.project_university.Class.Listening_Array> Listening_Array = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_litening);
        Listening_Array.add(new Listening_Array("1", "ali", 1));
        Listening_Array.add(new Listening_Array("2", "ali", 1));
        Listening_Array.add(new Listening_Array("3", "ali", 1));

        View_Pager_Listening = findViewById(R.id.view_pager_listening);

        Slider_Listening_Adapter test=new Slider_Listening_Adapter(getApplicationContext(),Listening_Array);
        test.notifyDataSetChanged();
        View_Pager_Listening.beginFakeDrag();
        View_Pager_Listening.setAdapter(test);

    }
}

The event I wrote works only when the items of the Array list include the contents of the slider of a number, otherwise the event will occur on the next page! And in the last slide, because there is no slide next to it, it works correctly. I don't know where the problem is, and unfortunately, it is not even an error.

Upvotes: 0

Views: 20

Answers (0)

Related Questions