片段更改时,AutoCompleteTextView arrayList更新无效

时间:2015-07-08 13:34:42

标签: android android-fragments android-edittext autocompletetextview

第一,我的英语技能很差。

我使用android.support.v4.view.ViewPager和fragment。

当我滑动片段页面时,想要更新AutoCompleteTextView适配器。

尝试
1. onPageSelected(int position)

中的(清除,设置,通知......)组合
aList.clear();  
autoSearchAdapter.clear();  
autoTextView.setAdapter(autoSearchAdapter);  
autoSearchAdapter.notifyDataSetChanged();  
  1. onPageSelected(int position)
  2. 中使用处理程序

    提示。有时AutoCompleteTextView arrayList工作。(50%)和它在AutoCompleteTextView中写句子后工作,刷一下页面。此时,arraylist扩大了。所有列表都可见。

    代码的一部分
        公共类MainActivity扩展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) {
            }
        });
    }
    

1 个答案:

答案 0 :(得分:0)

在autoSearchAdapter中你必须创建设置arry list的方法,

private ArrayList<Test> test;

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

之后你必须打电话

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

它就像魅力一样。