Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/217.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在ListView中过滤内容_Java_Android_Listview_Filter - Fatal编程技术网

Java 如何在ListView中过滤内容

Java 如何在ListView中过滤内容,java,android,listview,filter,Java,Android,Listview,Filter,我在谷歌上搜索了其他解决方案,但找不到任何与我的情况类似的解决方案,起初我试图获取文本视图和过滤器的内容,但没有成功,请帮助。因此,我在这里创建了一个listview,它由三个textview膨胀而成,三个textview的形式类似于一个表,每个textview是一个列,数据是从服务器动态添加的。如何根据用户搜索筛选内容。 下面是代码 MainActivity.java public class MainActivity extends AppCompatActivity { private v

我在谷歌上搜索了其他解决方案,但找不到任何与我的情况类似的解决方案,起初我试图获取文本视图和过滤器的内容,但没有成功,请帮助。因此,我在这里创建了一个listview,它由三个textview膨胀而成,三个textview的形式类似于一个表,每个textview是一个列,数据是从服务器动态添加的。如何根据用户搜索筛选内容。 下面是代码

MainActivity.java

public class MainActivity extends AppCompatActivity {
private void showJSON2(String response){
    String id="";
    String name="";
    String date_from="";
    String date_to="";
    try {
        JSONObject jsonObject = new JSONObject(response);
        JSONArray result = jsonObject.getJSONArray(JSON_ARRAY2);
        ListView listView = (ListView) findViewById(R.id.list_view);
        list = new ArrayList<HashMap<String, String>>();
        for (int i=0; i<result.length(); i++) {
            JSONObject notice = result.getJSONObject(i);
            id = notice.getString(KEY_ID);
            name = notice.getString(KEY_NAME);
            date_from = notice.getString(KEY_DATE_FROM);
            date_to = notice.getString(KEY_DATE_TO);

            HashMap<String, String> temp= new HashMap<String, String>();
            temp.put(FIRST_COLUMN, id);
            temp.put(SECOND_COLUMN, name);
            temp.put(THIRD_COLUMN, date_from);
            temp.put(FOURTH_COLUMN, date_to);
            list.add(temp);

        }

        ListViewAdapters adapter = new ListViewAdapters(this, list);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> parent, final View view, int position, long id)
            {
                int pos=position+1;
                Toast.makeText(MainActivity.this, Integer.toString(pos)+" Clicked", Toast.LENGTH_SHORT).show();
            }

        });

    } catch (JSONException e) {
        e.printStackTrace();
    }
  }
}
public类MainActivity扩展了AppCompatActivity{
私有void showJSON2(字符串响应){
字符串id=“”;
字符串名称=”;
字符串date_from=“”;
字符串date_to=“”;
试一试{
JSONObject JSONObject=新JSONObject(响应);
JSONArray result=jsonObject.getJSONArray(JSON_ARRAY2);
ListView ListView=(ListView)findViewById(R.id.list\u视图);
列表=新的ArrayList();

对于(inti=0;i我使用这种方法根据用户的输入过滤我的列表

   private void setFilter(EditText et_search_bar, ArrayList<String> list, OnListUpdated onListUpdated) {

    if (et_search_bar != null) {
        TextWatcher textWatcher;
        textWatcher = new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                // TODO Auto-generated method stub
            }

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

                // TODO Auto-generated method stub
            }

            @Override
            public void afterTextChanged(Editable s) {
                // filter your list from your input
                if (!previousValue.equalsIgnoreCase(s.toString())) {
                    if (!TextUtils.isEmpty(s.toString())) {
                        previousValue = s.toString();
                        ArrayList<String> temp = new ArrayList<>();
                        for (String bookmarkBean : list) {
                            //or use .contains(text)
                            if (bookmarkBean.toLowerCase().contains(s.toString())) {
                                temp.add(bookmarkBean);
                            }
                        }
                        //update recyclerview
                        if (onListUpdated != null) {


                            onListUpdated.onListFiltered(temp);
                        }
                    } else onListUpdated.onListFiltered(filterBean);
                }
            }
        };
        et_search_bar.addTextChangedListener(textWatcher);
    }
}
private void setFilter(编辑文本集搜索栏、数组列表、OnListUpdated OnListUpdated){
如果(搜索栏!=null){
文本观察者文本观察者;
textWatcher=新的textWatcher(){
@凌驾
public void onTextChanged(字符序列、int start、int before、int count){
//TODO自动生成的方法存根
}
@凌驾
更改前文本之前的公共void(字符序列s、int start、int count、int after){
//TODO自动生成的方法存根
}
@凌驾
公共无效后文本已更改(可编辑){
//从输入中筛选列表
如果(!previousValue.equalsIgnoreCase(s.toString())){
如果(!TextUtils.isEmpty(s.toString())){
previousValue=s.toString();
ArrayList temp=新的ArrayList();
for(字符串书签bean:list){
//或使用。包含(文本)
if(bookmarkBean.toLowerCase().contains(s.toString())){
临时添加(书签bean);
}
}
//更新回收视图
如果(onListUpdated!=null){
onListUpdated.onListFiltered(临时);
}
}else onListUpdated.onListFiltered(filterBean);
}
}
};
et_search_bar.addTextChangedListener(textWatcher);
}
}

public interface OnListUpdated{
void onListFiltered(ArrayList列表);}
setFilter(搜索栏、过滤器bean、新联机更新(){
@凌驾
public void onListFiltered(ArrayList列表){
//通知您的适配器
}
});
在MainActivity.java中

public class MainActivity extends AppCompatActivity {

    ArrayList<HashMap<String, String>> list = new ArrayList<>();
    ListViewAdapters adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_default);

        adapter = new ListViewAdapters(this, list);
        listView.setAdapter(adapter);
    }

    private void showJSON2(String response) {
        ...
        list.clear();
        ...
        list.add(temp);
        ...

        // ListViewAdapters adapter = new ListViewAdapters(this, list);
        // listView.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        ...
    }

}
要取消筛选

adapter.cancelFilter();

使用泛型
Filterable
适配器(而不是
BaseAdapter
)作为适配器的基类,只覆盖两个方法
onBind()
matches()
您将如何筛选?使用什么值?@npk用户输入的值。@pskink能否请您提供一个我很难解决的示例。谢谢。类似这样的内容:
class YourCustomAdapter Extendes MatchableArrayAdapter{…
并重写上述2个方法+构造函数,在您的情况下使用
ArrayList
为什么不使用
Filterable
接口?我不知道:(
public class MainActivity extends AppCompatActivity {

    ArrayList<HashMap<String, String>> list = new ArrayList<>();
    ListViewAdapters adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_default);

        adapter = new ListViewAdapters(this, list);
        listView.setAdapter(adapter);
    }

    private void showJSON2(String response) {
        ...
        list.clear();
        ...
        list.add(temp);
        ...

        // ListViewAdapters adapter = new ListViewAdapters(this, list);
        // listView.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        ...
    }

}
public class ListViewAdapters extends BaseAdapter {

    ArrayList<HashMap<String, String>> origList = new ArrayList<>();

    public ListViewAdapters(Activity activity,ArrayList<HashMap<String, String>> list) {
        super();
        this.activity=activity;
        this.list=list;
        this.origList=list;
    }

    ...

    void filter(String filterString) {
        list = new ArrayList<>();
        for (HashMap<String, String> item: origList) {
            // use your own logic to filter
            if (itemShouldBeAdded)
                list.add(item); // add if item is inside filter
        }
        notifyDataSetChanged();
    }

    void cancelFilter() {
        list = origList;
        notifyDataSetChanged();
    }

}
adapter.filter("some_string");
adapter.cancelFilter();