【问题标题】:AutoCompleteTextView arrayList update not working when fragment changed片段更改时 AutoCompleteTextView arrayList 更新不起作用
【发布时间】:2015-07-08 21:42:16
【问题描述】:

首先,我的英语能力很弱。

我使用 android.support.v4.view.ViewPager 和片段。

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

试试
1.onPageSelected(int position)中的(clear, set, notify..)组合

aList.clear();  
autoSearchAdapter.clear();  
autoTextView.setAdapter(autoSearchAdapter);  
autoSearchAdapter.notifyDataSetChanged();  
  1. onPageSelected(int position)中使用处理程序

提示。有时 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) {
        }
    });
}

【问题讨论】:

  • 你做错了:根本不需要setOnPageChangeListener,在这里查看我的答案:stackoverflow.com/a/19860624/2252830
  • 不使用setOnPageChangeListener,如何查看fragment的变化?
  • 你只需要在runQuery方法中查看当前页面是什么就可以了
  • 好的,谢谢。我不知道确切。试试你的答案!

标签: android android-fragments android-edittext autocompletetextview


【解决方案1】:

在 autoSearchAdapter 中你必须创建方法来设置数组列表,

private ArrayList<Test> test;

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

你必须打电话后

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

它就像魅力一样。

【讨论】:

  • 你的意思是使用自定义适配器而不是 [autoSearchAdapter = new ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, aList);] ?
猜你喜欢
  • 2018-10-31
  • 2015-03-28
  • 1970-01-01
  • 1970-01-01
  • 2022-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多