CrazyMac CrazyMac - 3 months ago 17
Android Question

Android Trouble fetching the value from EditText in a Fragment which is using a ViewPager

I have a simple fragment with a ViewPager. In the same fragment, I have a EditText to capture the text entered by user.

I have a Adapter which extends PagerAdapter and I have got OnClick event for a button in the instantiateItem method as shown below. When I try to get the reference for this EditText both inside the OnClick method or outside, I don't see the value being fetched. Strangely, I see that sometimes, it displays the result and sometimes it doesn't

here is my Adapter class..

public class ProposalSwipeAdapter extends PagerAdapter {

private ArrayList<String> imageResourcess = new ArrayList<>();
private String[] imageResources = {"James", "Mick"};

private Context context;
private LayoutInflater inflater;
TextView notesEntered;
EditText notes = null;
View itemView;
String notedEnteredByUser;

public ProposalSwipeAdapter(Context context, ArrayList<ProposalDetailModel> list) {
this.context = context;
//this.imageResourcess = list;
for (ProposalDetailModel objectProposal : list
) {
//ProposalDetailModel objectProposal1 = new ProposalDetailModel();
//objectProposal1.setIntroWhatDoWeDo(objectProposal.getIntroWhoAreWe());
//objectProposal1.setDateOfBusiness(objectProposal.getDateOfBusiness());
this.imageResourcess.add("\t\t\t\t\t\t\t\t\t\t INTRODUCTION \n\n\n\n\nABOUT THE COMPANY:" + objectProposal.getIntroWhoAreWe() +
"\n\nWHAT DO WE DO:" + objectProposal.getIntroWhatDoWeDo() +
"\n\nTOTAL EMPLOYEES:" +
"\n\nOFFICIAL LOCATION:" +
"\n\nFOUNDING YEAR:");


this.imageResourcess.add("\t\t\t\t\t\t\t\t\t\t PRODUCT/SERVICE \n\n\n\nSERVICE/PRODUCT OFFERED:" + objectProposal.getIntroWhoAreWe() +
"\n\nTYPE OF CUSTOMERS:" + objectProposal.getAddres() +
"\n\nWHY WILL THEY BUY IT:" +
"\n\nPRODUCT PRICE:" +
"\n\nCOMPETITORS:" +
"\n\nSERVICE DELIVERY:");

}

}

@Override
public int getItemPosition(Object object) {
int index = imageResourcess.indexOf(object);
if (index == -1)
return POSITION_NONE;
else
return index;
}

@Override
public int getCount() {
return imageResourcess.size();
//return imageResources.length;
}

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

@Override
public Object instantiateItem(ViewGroup container, int position) {
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
itemView = inflater.inflate(R.layout.proposal_swipe, null);
Button proposalNotesBtn;

TextView tv = (TextView) itemView.findViewById(R.id.proposalText);
tv.setText(imageResourcess.get(position).toString());
container.addView(itemView);
proposalNotesBtn = (Button) itemView.findViewById(R.id.proposalNotesBtn);


proposalNotesBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
notesEntered = (TextView) itemView.findViewById(R.id.enteredNotes);
notes = (EditText) itemView.findViewById(R.id.notesText);
notedEnteredByUser = notes.getText().toString();
notesEntered.setText(notedEnteredByUser);
Log.i("NOTES ENTERED", notedEnteredByUser);
}
});
if(itemView != null) {
((ViewGroup)itemView.getParent()).removeView(itemView);
}
container.addView(itemView);
return itemView;
}

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


and the fragment here..

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollview"
android:layout_width="wrap_content"
android:layout_height="match_parent"
>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:id="@+id/proposalText"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_marginTop="80dp"
android:paddingLeft="10dp"
android:textStyle="bold" />

<EditText
android:id="@+id/notesText"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_below="@id/proposalText"
android:layout_margin="10dp"
android:background="@drawable/back"
android:inputType="text" />

<Button
android:id="@+id/proposalNotesBtn"
android:layout_width="130dp"
android:layout_height="50dp"
android:text="Save Notes" />

<TextView
android:id="@+id/enteredNotes"
android:layout_width="match_parent"
android:layout_height="100dp"
android:textColor="@color/colorPrimary"
android:layout_below="@id/proposalNotesBtn" />

</LinearLayout>
</ScrollView>


Please guide me if I am missing any.

Answer

I was able to get the text by using TextWatcher method. Also, I implemented FragmentPagerAdapter instead of PagerAdapter which was very useful.

notes = (EditText) itemView.findViewById(R.id.notesText);
TextWatcher textWatcher = new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        notedEnteredByUser = charSequence.toString();
        Log.i("TEXTWATCHER", notedEnteredByUser);
    }

    @Override
    public void afterTextChanged(Editable editable) {

    }
};
notes.addTextChangedListener(textWatcher);
Comments