1

約30個のアイテムを含むlistViewがあり、editTextを介して検索機能を追加しました。テキストフィールドに「a」と入力すると、「a」で始まるものがすべてリストに表示されますが、別の文字を入力すると、リストに「a」と入力した他の文字が含まれていても、リストは表示されなくなります。

私を混乱させるもう1つのことは、リストに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);
    }
};

}

My CustomerClass(private 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;
}

}

4

1 に答える 1

1

次を追加して問題を解決しました。

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

(Luksprogのコメントを読んだ後)のafterTextChanged方法に。TextWatcher

于 2012-11-16T12:48:31.853 に答える