Обновление AutoCompleteTextView arrayList не работает при изменении фрагмента

Фрист, у меня слабое знание английского.

Я использую android.support.v4.view.ViewPager и фрагмент.

Когда я пролистываю страницу фрагмента, хочу обновить адаптер AutoCompleteTextView.

try
1.combination for (clear, set, notify..) в onPageSelected(int position)

aList.clear();  
autoSearchAdapter.clear();  
autoTextView.setAdapter(autoSearchAdapter);  
autoSearchAdapter.notifyDataSetChanged();  
  1. использовать обработчик в onPageSelected(int position)

намекать. иногда работает AutoCompleteTextView arrayList (50%), и он работает после написания предложения в AutoCompleteTextView, проведите пальцем по странице. в этот момент арралист расширился. также весь список виден.

часть кода
открытый класс MainActivity extends ActionBarActivity {

static String uName;
TestAdapter demoPagerAdapter;
ViewPager mViewPager;

static AutoCompleteTextView autoTextView;
static ArrayAdapter<String> autoSearchAdapter;
public static ArrayList<String> aList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    //hide actionbar
    getSupportActionBar().hide();
    setContentView(R.layout.activity_main);
    // fragments, so use getSupportFragmentManager.
    demoPagerAdapter = new TestAdapter(getSupportFragmentManager());
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(demoPagerAdapter);

    getAccountInfo();

    SetAutoSearchAdapter();

    KeyBoardHide();

}
private void SetAutoSearchAdapter(){
    //AutoCompleteTextView setting
    autoTextView = (AutoCompleteTextView) findViewById(R.id.autocomplete_textview);
    aList=new ArrayList<String>();
    autoSearchAdapter =
            new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, aList);
    autosearchInit();
    autoTextView.setAdapter(autoSearchAdapter);

    //updating autosearch adapter
    mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        }

        @Override
        public void onPageSelected(int position) {
            if (position == 0) {
                aList.clear();
                Toast.makeText(getApplicationContext(), "fragment 0", Toast.LENGTH_LONG).show();
                //autosearchInit();
                aList.add("aaaaaa00");
                aList.add("bbbbb00");
                aList.add("ccccc00");
                autoSearchAdapter.notifyDataSetChanged();
            } else if (position == 1) {
                Toast.makeText(getApplicationContext(), "fragment 1.", Toast.LENGTH_LONG).show();
                aList.clear();
                aList.add("aaaaaa11");
                aList.add("bbbbb11");
                aList.add("ccccc11");
                autoSearchAdapter.notifyDataSetChanged();
            } else if (position == 2) {
                Toast.makeText(getApplicationContext(), "fragment 2", Toast.LENGTH_LONG).show();
                aList.clear();
                autoSearchAdapter.clear();
                autoTextView.setAdapter(autoSearchAdapter);
                aList.add("aaaaa22");
                aList.add("bbbbb22");
                aList.add("ccccc22");
                autoSearchAdapter.notifyDataSetChanged();
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {
        }
    });
}

person sung uk park    schedule 08.07.2015    source источник


Ответы (1)


в autoSearchAdapter вам нужно создать метод для установки списка arry,

private ArrayList<Test> test;

Public void setAryList(ArrayList<Test> test){
this.test = test;
}

после того, как вы должны позвонить

autoSearchAdapter.setAryList(aList);  
autoSearchAdapter.notifyDataSetChanged();  

это работает как шарм.

person Darshan Mistry    schedule 08.07.2015
comment
вы имеете в виду использовать собственный адаптер вместо [autoSearchAdapter = new ArrayAdapter‹String›(this, android.R.layout.simple_dropdown_item_1line, aList);]? - person sung uk park; 08.07.2015