我想搜索字符串数组并将结果获取到ListView中。Android通过字符串数组搜索并将结果导入ListView
所以,如果我有
<string-array name="Colors">
<item>Red</item>
<item>Yellow</item>
<item>Blue</item>
<item>DarkRed</item>
</string>
,我搜索“红”,我应该进入ListView的两个项目。对于每个项目,我想知道ID到字符串数组和字符串值,这将显示在ListView中。
在搜索结果时,我想显示一个ProgressBar(不确定状态),当所有完成时消失。
第一步是将字符串数组获取到List或String []中,然后创建一个新的Thread来比较数组中的每个项目,并将ListView与搜索文本匹配。
我不知道哪个是最好的做法。我的代码是这样的:
public class SearchActivity extends Activity {
private ProgressBar mProgress;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search);
mProgress = (ProgressBar) findViewById(R.id.Progress);
new Thread(new Runnable() {
public void run() {
int item_pos = 0;
int item_count = 0;
String[] Colors = getResources().getStringArray(R.array.Colors);
item_count = Colors.length();
mProgress.setVisibility(View.VISIBLE);
while (item_pos < item_count) {
// Compare with the search text
// Add it to the ListView (I don't know how)
item_pos +=1;
}
mProgress.setVisibility(View.GONE);
}
}).start();
}
}
所以,我的问题:
- 我如何获得这两个项目的id和字符串,则比较搜索文本的每个文本值?
- 如何添加一个项目到ListView?
为什么ProgressBar不可见?该进度XML代码是这样的:
<ProgressBar android:id="@+id/Progress" style="@android:style/Widget.ProgressBar.Small" android:layout_width="fill_parent" android:layout_height="wrap_content" android:indeterminate="true" android:visibility="visible" />
感谢您的所有建议和帮助!
感谢您的帮助。我会在一个Async类中做这个,我想我会从中获得双重优势。感谢你的宝贵时间! – ali 2012-04-23 19:36:26