1

我有一个String[]call lv_arr,我的活动如下所示:

看起来像这样

它包含一个EditText和以下,我lv_arr[]使用此代码显示一个以上:

listView = (ListView) findViewById(R.id.listview);  
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice,lv_arr);  
listView.setAdapter(adapter);

当前lv_arr[]仅包含一个字符串(即"Notes"),并且每当用户添加一个字符串时,它都会被更新。我想添加TextWatcher我的EditText,所以我尝试了以下方法:

   <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/addLabelText"
        android:hint="Add Label">
        <requestFocus/>

    </EditText>

和:

addLabelText = (EditText) findViewById(R.id.addLabelText);
        addLabelText.addTextChangedListener(listWatcher);
}

private  final TextWatcher listWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int i, int i1, int i2) {
            System.out.println(s);
        }

        @Override
        public void onTextChanged(CharSequence s, int i, int i1, int i2) {

        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    };

我想要一个最终输出,每当用户在其中键入任何内容时,EditText我的列表中的重要值都应该显示在它的下方。当它EditText进入空白状态时,下面的列表应显示所有元素。否则只有匹配的值应该是可见的。

我尝试使用 for 循环,以便获得匹配的值,但我在某处犯了错误......

4

5 回答 5

0

使用可过滤的。

例如,这里是帖子

于 2016-10-26T12:43:44.797 回答
0

我认为您可以使用可过滤来实现这一点。

尝试如下:

private  final TextWatcher listWatcher = new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence s, int i, int i1, int i2) {
       adapter.getFilter().filter(s);
    }

    @Override
    public void afterTextChanged(Editable editable) {

    }
};

实现 Filterable 到 Adapter 和代码如下

 @Override
public Filter getFilter() {
    if (itemFilter == null) {
        itemFilter = new ItemFilter();
    }
    return itemFilter;

}

private class ItemFilter extends Filter {

    @Override
    protected FilterResults performFiltering(CharSequence constraint) {

        final String searchString = constraint.toString().toLowerCase();
        Log.i("TAG", "Query=>" + constraint);
        final FilterResults filterResults = new FilterResults();

        if (constraint != null && constraint.toString() != "") {
            doctorsList.clear();
            // search content in DoctorList list
            for (i=0;i<itemNameArray.length();i++) {
                if (itemNameArray[i].toLowerCase().contains(searchString)) {
                    arrayList.add(itemNameArray[i]);

                }
            }

            filterResults.count = arrayList.size();
            Log.i("TAG", "FilterResultCount=>" + filterResults.count);
            filterResults.values = arrayList;
            Log.i("TAG", "FilterResultValues=>" + filterResults.values);

        } else {

            filterResults.count = arrayList.size();
            filterResults.values = arrayList;

        }
        return filterResults;

    }

    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {

        arrayList = (ArrayList<String>) results.values;
        notifyDataSetChanged();
        Log.i("TAG", "SearchResult" + arrayList);


    }
}
于 2016-10-26T13:23:05.933 回答
0

启用搜索过滤器

addLabelText.addTextChangedListener(new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
        // When user changed the Text
        MainActivity.this.adapter.getFilter().filter(cs);   
    }

    @Override
    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
            int arg3) {
        // TODO Auto-generated method stub

    }

    @Override
    public void afterTextChanged(Editable arg0) {
        // TODO Auto-generated method stub                          
    }
});
于 2016-10-26T14:03:36.123 回答
0

因此,为了实现我想要的输出,我编写如下代码:

1)我将我的 EditText 更改为 SearchView

<SearchView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/addLabelText"
            />

2) 在 SearchView 上添加 setOnQueryTextListener

addLabelText =(SearchView) findViewById(R.id.addLabelText);
        addLabelText.setOnQueryTextListener(this);

3)实现QueryTextChangeListener 4)然后,

@Override
    public boolean onQueryTextSubmit(String query) {

        return false;
    }

    @Override
    public boolean onQueryTextChange(String newText) {
        adapter.getFilter().filter(newText);
        return false;
    }

这里的适配器是我用于列表视图的适配器,即

adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice,lv_arr);
        listView.setAdapter(adapter);

参考链接: http ://www.tutorialsbuzz.com/2014/08/android-filters-for-simple-listview.html

于 2016-10-26T14:10:56.620 回答
0

我在这里发布我的答案 - 可能对某人有用

我试图解决这个问题 - 当列表视图 - > 编辑文本有一个文本观察器时无法获得位置。

这是对我有用的解决方案:

在获取视图 -

当我添加文本观察者侦听器来编辑文本时,我还添加了以下行

edittext.setTag(R.id.position, 位置)

在你的 afterTextChanged - int position = edittext.getTag(R.id.position)

给出正确的位置编号,您可以根据位置编号进行修改。

于 2017-04-26T13:30:35.947 回答