【问题标题】:Can't search (with searchview) for a record in list view that is retrieved with Json无法在列表视图中搜索(使用 searchview)使用 Json 检索的记录
【发布时间】:2017-07-21 05:45:15
【问题描述】:
package rjj.tutorial_jsonandlistview;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;


import android.view.View;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;



public class DisplayListView extends AppCompatActivity {

    String json_string;
    JSONObject jsonObject;
    JSONArray jsonArray;
    ContactAdapter contactAdapter;
    ListView listView;
    SearchView sv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.display_listview_layout);
        listView = (ListView)findViewById(R.id.listview);

        contactAdapter = new ContactAdapter(this,R.layout.row_layout);
        listView.setAdapter(contactAdapter);
        json_string = getIntent().getExtras().getString("json_data");
        //Searchview
        sv = (SearchView)findViewById(R.id.search);

        sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                contactAdapter.getFilter().filter(newText);
                return false;
            }
        });
        //End of Searchview

        try {
            jsonObject = new JSONObject(json_string);
            jsonArray = jsonObject.getJSONArray("server_response");
            int count = 0;
            String id ,firstname , surname, age , username, password;


            while(count<jsonArray.length()){
                JSONObject JO = jsonArray.getJSONObject(count);
                id = JO.getString("id");
                firstname = JO.getString("firstname");
                surname = JO.getString("surname");
                age = JO.getString("age");
                username = JO.getString("username");
                password = JO.getString("password");
                Contacts contact = new Contacts(id, firstname, surname, age,username,password);
                contactAdapter.add(contact);


                count++;

            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    public void hello(View view) {

        Intent intent = new Intent(this, MainActivity.class);
        final TextView textView = (TextView)findViewById(R.id.tx_id);
        String id = textView.getText().toString();
        intent.putExtra("id", id);
        startActivity(intent);
    }
}

我添加了 searchview 小部件来过滤,但我很困惑为什么它至少不搜索或过滤记录。请解释我在哪里做错了。我是初学者,所以请以您首先学习编程的方式解释它。

以下是我输入内容时的错误日志:

07-21 13:41:52.108 17026-17026/rjj.tutorial_jsonandlistview E/EGL_emulation: tid 17026: eglSurfaceAttrib(1199): error 0x3009 (EGL_BAD_MATCH)

【问题讨论】:

    标签: android json searchview


    【解决方案1】:

    对于 searchview 小部件,将此方法设置为覆盖并尝试。

    @Override
    public boolean onQueryTextChange(String newText) {
        contactAdapter.getFilter().filter(newText);
        return true; //here make it true
    }
    

    并且在contactAdapter中添加了@Override

    public Filter getFilter()
    {
        return new Filter(){
    
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                constraint = constraint.toString().toLowerCase();
                FilterResults result = new FilterResults();
    
                if (constraint != null && constraint.toString().length() > 0) {
                    List<String> founded = new ArrayList<String>();
                    for(YourListItemType item: origData){
                        if(item.toString().toLowerCase().contains(constraint)){
                            founded.add(item);
                        }
                    }
                    result.values = founded;
                    result.count = founded.size();
                }else {
                    result.values = origData;
                    result.count = origData.size();
                }
                return result;
            }
    
            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                clear();
                for (String item : (List<String>) results.values) {
                     add(item);
                }
                notifyDataSetChanged();
            }
        }
    }
    

    【讨论】:

    • 我应该把公共过滤器放在哪里?
    • 应该是适配器
    • 我真的不明白,我应该在哪里输入这个代码? ://
    猜你喜欢
    • 2015-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-26
    • 2018-08-13
    • 2016-09-14
    • 2016-12-23
    • 1970-01-01
    相关资源
    最近更新 更多