我想搜索字符串數組並將結果獲取到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