【问题标题】:Android search through an string-array and get results into a ListViewAndroid 通过字符串数组搜索并将结果放入 ListView
【发布时间】:2012-04-23 18:27:49
【问题描述】:

我想搜索一个字符串数组并将结果放入 ListView。

所以,如果我有

<string-array name="Colors">
    <item>Red</item>
    <item>Yellow</item>
    <item>Blue</item>
    <item>DarkRed</item>
</string>

并且我搜索“红色”,我应该进入 ListView 两个项目。对于每个项目,我想知道字符串数组中的 ID 和字符串值,它们将显示在 ListView 中。

在搜索结果时,我想显示一个 ProgressBar(状态不确定),当一切完成后它就会消失。

第一步是将字符串数组放入 List 或 String[],然后创建一个新线程来比较数组中的每个项目,并将匹配搜索文本的项目放在 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();
    }
}

所以,我的问题:

  1. 如何获取项目 ID 和字符串,然后将每个文本值与搜索文本进行比较?
  2. 如何向 ListView 添加项目?
  3. 为什么 ProgressBar 不可见? 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" />
    

感谢您的所有建议和帮助!

【问题讨论】:

    标签: android arrays string listview search


    【解决方案1】:

    这是我解决这个问题的方法。

    首先,我会像这样得到我的string-array

        Resources res = getResources();
        String[] colors = res.getStringArray(R.array.Colors);
        // transform into ArrayList for ease of use
        ArrayList<String> colorsList = Arrays.asList(colors); 
    

    在列表中搜索所需的序列并删除任何不需要的元素:

    for (String s : colorsList) {  
       if (!s.contains("red")) { // hardcoded, only to illustrate my logic
          colorsList.remove(s);
       }
    }  
    

    既然您有一个没有不需要的元素的列表,只需将其绑定到您的ListView,为此我建议您使用ArrayAdapter。该文档包含一篇关于 basic ListView usage 的精彩文章。

    如果您的 Activity 仅包含一个 ListView 作为它的唯一元素,您可以从 ListActivity 扩展您的 Activity,而不是 Activity。它将为您带来一些新的好处,例如只需调用 getListView() 即可轻松获取您的 ListView。

    就您的 ProgressBar 而言,我建议您查看 AsyncTask 类,它将提供一种更优雅的方式来处理您的应用程序中的线程。 Android 开发团队确实建议您使用 AsyncTask,而不是您现在使用的经典 Runnable 方法。

    最后,如果你正在寻找更多关于 ListViews 的代码 sn-ps,你真的应该看看 here,它充满了来自 Android 团队的示例。当我开始使用 Android 并且无法理解 ListViews 时,这对我很有帮助。

    希望对您有所帮助!

    【讨论】:

    • 感谢您的帮助。我将在 Async 类中执行此操作,我想我会从中获得双重优势。再次感谢您的宝贵时间!
    猜你喜欢
    • 2023-04-01
    • 1970-01-01
    • 2018-03-19
    • 2013-10-31
    • 1970-01-01
    • 2018-10-05
    • 2013-05-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多