【问题标题】:Search in a ListView doesn't work properly在 ListView 中搜索无法正常工作
【发布时间】:2012-11-16 12:16:39
【问题描述】:

我有一个包含大约 30 个项目的 listView,并通过 editText 向它添加了搜索功能。当我在文本字段中输入“a”时,所有以“a”开头的内容都会显示在列表中,但是当我输入另一个字母时,即使列表包含带有“a”的项目+我输入的另一个字母,列表也会消失。

让我感到困惑的另一件事是列表中有一个名为 IWU Trading 的项目,这是我唯一可以搜索的项目,这意味着如果我输入“I”+“W”,该项目就会显示在 listView 中。但是如果我输入 'a' + 'r' 一个名为 'art' 的项目就会出现。

我的问题。 我该怎么做才能使此搜索功能起作用? 为什么会这样?

我的 XML

    <EditText
    android:id="@+id/searchEditText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/selectCustomerTextView"
    android:layout_marginTop="20dp"
    android:hint="@string/typeToSearch"
    android:ems="10"
    android:imeOptions="actionDone"
    android:inputType="textNoSuggestions"/>


<ListView
    android:id="@+id/list_view"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/nextButton"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/searchEditText" 
    android:choiceMode="singleChoice"
    android:dividerHeight="5dp" >
</ListView>

我的代码:

    private ArrayAdapter<Customer> _oldAdapter = null;

    public void onCreate(Bundle savedInstanceState) {

    super.onCreate( savedInstanceState );
    setContentView(R.layout.activity_customer_pick);
    EditText searchText = (EditText) findViewById(R.id.searchEditText);
    ListView listView = (ListView) findViewById(R.id.list_view);
    listView.setClickable(true);

    searchText.addTextChangedListener(filterTextWatcher);

    _oldAdapter = _phoneDAL.BindValues(this);

    listView.setAdapter(_oldAdapter);

    listView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
              String s = _oldAdapter.getItem(arg2).toString();
              _listViewPostion = arg2;

              Toast.makeText(CustomerPick.this, "Du valde: " + s, arg2).show();
        }
    });
}

搜索方式:

private TextWatcher filterTextWatcher = new TextWatcher() {

    public void afterTextChanged(Editable s) {
    }

    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
    }

    public void onTextChanged(CharSequence s, int start, int before,
            int count) {
        ArrayAdapter<Customer> _adapter = new ArrayAdapter<Customer>(CustomerPick.this, android.R.layout.simple_list_item_single_choice, new ArrayList<Customer>());

        ListView listView = (ListView) findViewById(R.id.list_view);

        int textLength = s.length();

        if(textLength == 0){
            listView.setAdapter(_oldAdapter);
            return;
        }
                for (int i = 0; i < listView.getAdapter().getCount(); i++)
                {
                    String word = _oldAdapter.getItem(i).getCustName().toLowerCase();
                    if (textLength <= word.length())
                    {
                        if(s.toString().equalsIgnoreCase(word.substring(0, textLength)))
                        {
                            _adapter.add(_oldAdapter.getItem(i));
                        }
                    }
                }
                listView.setAdapter(_adapter);
    }
};

}

我的 CustomerClass(私有 ArrayAdapter _oldAdapter = null;)

public class Customer {
private final int _custNumber;
private final String _custName;

public Customer(int custNumber, String custName){
    _custNumber = custNumber;
    _custName = custName;
}

public int getCustNumber() {
    return _custNumber;
}


public String getCustName() {
    return _custName;
}


@Override
public String toString(){
    return _custName;
}

}

【问题讨论】:

  • 请添加您的 oldAdapter 类。
  • 检查this
  • 这是过滤ListView 的不好方法。如果您要使用默认的ArrayAdapter,那么您只需从afterTextChanged 调用((ArrayAdapter)listview.getAdapter).filter(s);
  • 我没有发现明显的错误。但是这个问题似乎很容易调试:你不能记录 word、s.toString() 和 word.substring(0, textLength) 并准确地查看问题所在吗?还是使用调试器?
  • 嗯,我会尝试调试它。它可以是 virtualDevice 中的某种自动完成功能吗? *推测

标签: android android-layout listview search


【解决方案1】:

通过添加解决了问题:

ListView listView = (ListView) findViewById(R.id.list_view);
((Filterable) listView.getAdapter()).getFilter().filter(s);

进入TextWatcherafterTextChanged方法(阅读Luksprog的评论后)。

【讨论】:

    猜你喜欢
    • 2015-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-04
    • 2021-11-10
    • 2017-01-28
    • 2013-12-23
    相关资源
    最近更新 更多