有没有更有效的方法来更新列表视图或循环遍历字符串数组
Is there a more efficient way to update the listview or loop through a string array
我目前正在尝试为 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)
中的循环时它崩溃了。
非常感谢你们!
将您的代码放入处理程序中,它会在您搜索一段时间后运行。
new Handler().postDelayed(new Runnable(
public void run(){
// your update code here..
}
), 3000);
这称为用户输入编辑文本时的呼吸时间。所以他一开始打字就不会开始搜索单词,但 3 秒后就会开始搜索。
希望对您有所帮助。
好的,这里有很多问题我想解决:
如果您有一个大型数据集并想在其中进行搜索,我强烈建议您使用 SQLite 数据库。这正是该数据库的用途。有关详细信息,请参阅 Storing and Searching for Data
像这样的长 运行ning 任务应该始终 运行 作为 AsyncTask
考虑一下您的存储管理!例如。您的 readFile 将文件读取为字符串,然后将其拆分为数组。为什么不在加载时直接拆分它。您保留所有字符串的数组,然后从中创建一个列表,然后将其复制到 ArrayList 中。从该列表中删除所有不需要的条目。当您考虑所有的复制工作以及您编写代码的频率 运行 时,我认为您可以通过消除任务找到更快的解决方案!
我更改了 editText 并改为使用 searchView。这解决了问题!
我目前正在尝试为 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)
中的循环时它崩溃了。
非常感谢你们!
将您的代码放入处理程序中,它会在您搜索一段时间后运行。
new Handler().postDelayed(new Runnable(
public void run(){
// your update code here..
}
), 3000);
这称为用户输入编辑文本时的呼吸时间。所以他一开始打字就不会开始搜索单词,但 3 秒后就会开始搜索。
希望对您有所帮助。
好的,这里有很多问题我想解决:
如果您有一个大型数据集并想在其中进行搜索,我强烈建议您使用 SQLite 数据库。这正是该数据库的用途。有关详细信息,请参阅 Storing and Searching for Data
像这样的长 运行ning 任务应该始终 运行 作为 AsyncTask
考虑一下您的存储管理!例如。您的 readFile 将文件读取为字符串,然后将其拆分为数组。为什么不在加载时直接拆分它。您保留所有字符串的数组,然后从中创建一个列表,然后将其复制到 ArrayList 中。从该列表中删除所有不需要的条目。当您考虑所有的复制工作以及您编写代码的频率 运行 时,我认为您可以通过消除任务找到更快的解决方案!
我更改了 editText 并改为使用 searchView。这解决了问题!