0

I have used a viewpager in my app in which i have a number of pagers. In this pages i have a TextView and a Button and i want to be able to change the textview in the one pager when the button in that pager is clicked.

I have the code below, but the text doesn't change when i click on the button.

@Override
public Object instantiateItem(ViewGroup collection, int position) {
    View view = null;
    LayoutInflater h = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    view = h.inflate(R.layout.content, null);

    tvName = (TextView) view.findViewById(R.id.tvName);
    btnView = (Button) view.findViewById(R.id.viewBTN);
    tvName.setText("Original Text")

    btnView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            tvName.setText("new Test")
        }
    });


    collection.addView(view);

    return view;

}
John
  • 347
  • 1
  • 5
  • 16

4 Answers4

2

you can make an array of TextView. and use the Textview from the array for the particular position. Here is the full code.

private TextView[] mTextVIews;
private final LayoutInflater layoutInflater;
private Context mContext;
ArrayList<String> mList;


 public MyAdapter(Context mContext, ArrayList<String> mList) {
    layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    this.mContext = mContext;
    this.mList = mList;
    mTextVIews = new TextView[mList.size()];
}

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

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

@Override
public Object instantiateItem(ViewGroup container, final int position) {
    View view = layoutInflater.inflate(R.layout.My_layout, container, false);

    btnView = (Button) view.findViewById(R.id.viewBTN);
    tvName = (TextView) view.findViewById(R.id.tvName);
    mTextVIews[position] = tvName;

    btnView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mTextVIews[position].setText("new Test");
        }
    });


    container.addView(view);
    return view;
}
1

Use view.findViewById(R.id.tvName); inside clickListener

Vuk Vukcevic
  • 131
  • 8
  • While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – rajah9 Jul 29 '19 at 12:32
  • I got to this solution with trial and error, I don't really know the deep explanation behind it. I know that it works and I decided to post it just in case someone else also googles this question. – Vuk Vukcevic Jul 29 '19 at 12:37
0

just put this line

view.btnView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        view.tvName.setText("new Test")
    }
});
Aadil
  • 32
  • 4
0

Use the same code in the java file of that page. example If your first page is named as page1.java than make the change in that java file . For more details of viewpager you can visit badassandroiddevlopers.blogspot.in .