gpt4 book ai didi

java - 有没有更有效的方法来更新 ListView 或循环遍历字符串数组

转载 作者:行者123 更新时间:2023-11-30 01:25:38 24 4
gpt4 key购买 nike

我目前正在尝试为 android 编写一个简单的单词搜索程序。它应该匹配用户搜索的每个字母并显示本地词典中的所有匹配项。

一切都适用于较小的字典,但当我尝试使用较大的字典时,程序崩溃了。我的猜测是我循环遍历每个单词的搜索功能效率低下。

以下是我的项目的代码。

public class MainActivity extends ActionBarActivity {
String[] items;
int length;
ArrayList<String> listItems;
ArrayAdapter<String> adapter;
ListView listView;
EditText editText;

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView=(ListView)findViewById(R.id.listview);
editText=(EditText)findViewById(R.id.txtsearch);
items = readFile().split("\\n");
initList();



editText.addTextChangedListener(new TextWatcher() {

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
length=s.toString().length();
}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.toString().equals("")) {
initList();
}
else {
searchItem(s.toString());
}
}

@Override
public void afterTextChanged(Editable s) {
if (s.toString().length() < length) {
initList();
for (String item:items) {
if (!item.toLowerCase().contains(s.toString().toLowerCase())) {
listItems.remove(item);
}
}
}
}
});
}

public String readFile(){

InputStream input = getResources().openRawResource(getResources().getIdentifier("words","raw", getPackageName()));
BufferedReader br = null;
StringBuilder sb = new StringBuilder();

String finalstring = "";

try {
String line;
br = new BufferedReader(new InputStreamReader(input));

while ((line = br.readLine()) != null) {
// finalstring += line + "\n";
sb.append(line + "\n");
}

} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

return sb.toString();

}

public void searchItem (String searchword) {
for(String item:items){
if(!item.contains(searchword)){
listItems.remove(item);
}
}
adapter.notifyDataSetChanged();
}


public void initList(){

listItems=new ArrayList<>(Arrays.asList(items));
adapter=new ArrayAdapter<String>(this, R.layout.list_item, R.id.txtitem, listItems);
listView.setAdapter(adapter);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}

如前所述,我认为我的 SearchItem(String SearchWord) 是问题所在,但适配器的数据更新可能有问题?

注意:我使用了断点,当调用 SearchItem(String SearchWord) 中的循环时它崩溃了。

非常感谢你们!

最佳答案

我更改了 editText 并改为使用 searchView。这解决了问题!

关于java - 有没有更有效的方法来更新 ListView 或循环遍历字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36453647/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com