autoCompleteTextView只在删除一个字母后才显示结果。

huangapple go评论123阅读模式
英文:

autoCompleteTextView shows results only after deleting a letter

问题

我创建了一个布局,其中包含一个autoCompleteTextView,应该显示一些书名。

我注意到在某些情况下它不显示任何结果,除非我删除一个字母,然后它才显示正确的结果。

例如,我有一个包含单词Potter的书名字符串数组。

当我输入Potter时,它不显示任何结果:

autoCompleteTextView只在删除一个字母后才显示结果。

然而,如果我删除一个字母,突然间它就显示出来了:

autoCompleteTextView只在删除一个字母后才显示结果。

我用来填充列表的代码是:

atv_Search.addTextChangedListener( new TextWatcher() {

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


        if (s.length() > 0) {

			一些获取书名的代码

        }
    }


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

    }

    @Override
    public void afterTextChanged(Editable s) {

		Set<String> set = new LinkedHashSet<>( autoBookNames );
		autoBookNames.clear();
		autoBookNames.addAll( set );
		
		// 直到这一点,一切都正常工作,我可以看到authBookNames不是空的,但在删除之前不显示

		listAutoAdapter = new ArrayAdapter<String>( getApplicationContext(), R.layout.item_auto_search, autoBookNames );
		atv_Search.setThreshold( 1 );
		atv_Search.setAdapter( listAutoAdapter );

    }
} );

正如我在代码中所写的,当我进行调试时,即使在删除之前,我确实获得了所有的结果,但由于某种原因它没有显示出来。

有什么原因吗?我的意思是,大多数用户不会想到删除一个字母来显示结果。

谢谢

英文:

I created a layout that has an autoCompleteTextView in it that should show some names of books.

I noticed that it show no results in some cases unless I delete one letter and then it shows the correct results.

For example, I have an array string of books that has the word Potter in them.

When I type Potter, it shows no results:

autoCompleteTextView只在删除一个字母后才显示结果。

However, if I then delete one letter it suddenly shows:

autoCompleteTextView只在删除一个字母后才显示结果。

The code im using to populate the list is:

atv_Search.addTextChangedListener( new TextWatcher() {

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


        if (s.length() > 0) {

			SOME CODE TO GET NAMES

        }
    }


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

    }

    @Override
    public void afterTextChanged(Editable s) {

		Set<String> set = new LinkedHashSet<>( autoBookNames );
		autoBookNames.clear();
		autoBookNames.addAll( set );
		
		// UNTIL THIS POINT IT ALL WORKS GOOD AND I SEE THAT authBookNames IS NOT EMPTY, YET NOT SHOWING UNTIL DELETE

		listAutoAdapter = new ArrayAdapter<String>( getApplicationContext(), R.layout.item_auto_search, autoBookNames );
		atv_Search.setThreshold( 1 );
		atv_Search.setAdapter( listAutoAdapter );

    }
} );

As I wrote in the code when I debugged it, I did get all the results even before the deletion but it didn't show it for some reason.

Any reason for this? I mean most users won't think to delete a letter to show the results.

Thank you

答案1

得分: 1

try ==>;

listAutoAdapter = new ArrayAdapter<String>( getApplicationContext(), R.layout.item_auto_search, autoBookNames );
atv_Search.setAdapter( listAutoAdapter );

atv_Search.addTextChangedListener( new TextWatcher() {

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

 listAutoAdapter.getFilter().filter(s);  

}


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

}

@Override
public void afterTextChanged(Editable s) {



}
} );

let me know if this works
英文:

try==>

listAutoAdapter = new ArrayAdapter&lt;String&gt;( getApplicationContext(), R.layout.item_auto_search, autoBookNames );
    atv_Search.setAdapter( listAutoAdapter );

 atv_Search.addTextChangedListener( new TextWatcher() {

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

 listAutoAdapter.getFilter().filter(s);  

}


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

}

@Override
public void afterTextChanged(Editable s) {


    

}
} );

let me know if this works

答案2

得分: 0

你认为在 “获取名称的一些代码” 中,比较字符串的索引是否错误(例如,最后一个索引小于正确的索引1个字符)

英文:

Do you think that your compare strings' index in "SOME CODE TO GET NAMES" is wrong (such as last index is smaller than the correct one 1 character)

答案3

得分: 0

移动

listAutoAdapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.item_auto_search, autoBookNames);
atv_Search.setThreshold(1);
atv_Search.setAdapter(listAutoAdapter);

尝试将上述代码移动到 onTextChanged 中,然后测试一次,Ben!

英文:

Move

  listAutoAdapter = new ArrayAdapter&lt;String&gt;( getApplicationContext(), R.layout.item_auto_search, autoBookNames );
        atv_Search.setThreshold( 1 );
        atv_Search.setAdapter( listAutoAdapter );

into onTextchanged and try once, Ben!

huangapple
  • 本文由 发表于 2020年9月29日 06:08:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/64110261.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定