无法搜索(使用searchview)列表视图中使用Json检索的记录

时间:2017-07-21 05:45:15

标签: android json searchview

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)

1 个答案:

答案 0 :(得分:0)

对于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();
        }
    }
}