سؤال

I'm using a fairly simple custom pager adapter in Android, and I'd like to keep track of the contents of the edittext fields so that each new slide retains the same text if entered. Anyone have any insight on this? I'm guessing I'll need to set up a listener for text change, but I'm unsure of where to put it. Here is my adapter:

private class CustomPagerAdapter extends PagerAdapter {

    public int getCount() {
        return 3;
    }

    public Object instantiateItem(ViewGroup collection, int position) {

        LayoutInflater inflater = (LayoutInflater) collection.getContext()
                .getSystemService(LAYOUT_INFLATER_SERVICE);
        View view;

        switch(position) {
            case 0:
                view = inflater.inflate(R.layout.slide0, null, false);
                break;
            case 1:
                view = inflater.inflate(R.layout.slide1, null, false);
                break;
            case 2:
                view = inflater.inflate(R.layout.slide2, null, false);
                break;
            default:
                view = inflater.inflate(R.layout.slide0, null, false);
        }
        collection.addView(view,0);

        return view;
    }



    @Override
    public void destroyItem(ViewGroup arg0, int arg1, Object arg2) {
        arg0.removeView((View) arg2);
    }
    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == arg1;
    }
    @Override
    public Parcelable saveState() {
        return null;
    }

    @Override
    public void setPrimaryItem(ViewGroup container, int position, Object object) {
        super.setPrimaryItem(container, position, object);
        mCurrentSlide = (View) object;

    }
}
هل كانت مفيدة؟

المحلول

Ok, here is how I did it:

Set a member variable in your main class:

mText = "";

Set up a textWatcher in instantiateItem() in your adapter class:

EditText edtSlide = (EditText)view.findViewById(R.id.edtSlide);
//Set a listener for the editText that updates the member variable on keystroke.
    TextWatcher tw = new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            mText = s.toString();
        }

        };

    edtSlide.addTextChangedListener(tw);

Then in the setPrimaryItem() method in your adapter class:

@Override
public void setPrimaryItem(ViewGroup container, int position, Object object) {
    super.setPrimaryItem(container, position, object);
    EditText text = (EditText)((View) object).findViewById(R.id.edtSlide);
    text.setText(mText);
}

نصائح أخرى

Create a TextWatcher and then add it to each of your EditText:

TextWatcher tw = new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {                   
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {                    
            }

            @Override
            public void afterTextChanged(Editable s) {
            // Save the text to some global variable
            }
        };

EditText et = (EditText) findViewById(R.id.editText1);
EditText et2 = (EditText) findViewById(R.id.editText2);

et.addTextChangedListener(tw);
et2.addTextChangedListener(tw);

EDIT:

In the code above there is no easy way to know which of the EditText invoked the event. In that case you need to provide each EditText with its own TextWatcher:

et1.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            global_string_variable_for_et1 = s.toString();
        }
    });

Edit2:

TextWatcher[] tw = new TextWatcher[12];
for (int i = 0; i < tw.length; i++) {
tw[i] = new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {}
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
        @Override
        public void afterTextChanged(Editable s) {
        // Save the text to some global variable array
        txt_array[i] = s.toString();
        }
    };
}

for (int i = 0; i < et.length; i++) {7
    et[i].addTextChangedListener(tw[i]);
}

I was able to get it by placing a TextWatcher as shown below.

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);

And inside the instantiateItem, I am using the String notedEnteredByUser.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top