Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.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
Android Listview搜索Json_Android_Json_Listview_Search - Fatal编程技术网

Android Listview搜索Json

Android Listview搜索Json,android,json,listview,search,Android,Json,Listview,Search,我有个问题。我想用SearchView或EditText搜索我的listview,如果有人键入一些文本,我会过滤我的项目。但是我在这里找不到正确的Answare,因为我的json文件来自php/mysql数据库。有人能帮我吗 这是我的密码 public class MainActivity extends AppCompatActivity implements ListView.OnItemClickListener { private ListView listView; private

我有个问题。我想用SearchView或EditText搜索我的listview,如果有人键入一些文本,我会过滤我的项目。但是我在这里找不到正确的Answare,因为我的json文件来自php/mysql数据库。有人能帮我吗

这是我的密码

public class MainActivity extends AppCompatActivity  implements ListView.OnItemClickListener {
private ListView listView;
private String JSON_STRING;
EditText et;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    listView = (ListView) findViewById(R.id.listView);
    listView.setOnItemClickListener(this);
    listView.smoothScrollToPosition(0);
    listView.setTextFilterEnabled(true);
    et = (EditText) findViewById(R.id.editText2);
    getJSON();

    }

private void showEmployee() {
    JSONObject jsonObject = null;
    final ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
    try {
        jsonObject = new JSONObject(JSON_STRING);
        final JSONArray result = jsonObject.getJSONArray(Config.TAG_JSON_ARRAY);

        for (int i = 0; i < result.length(); i++) {
            JSONObject jo = result.getJSONObject(i);
            String id = jo.getString(Config.TAG_ID);
            String kfz = jo.getString(Config.TAG_KFZ);
            String bland = jo.getString(Config.TAG_BLAND);
            String kreis = jo.getString(Config.TAG_KREIS);

            HashMap<String, String> employees = new HashMap<>();
            employees.put(Config.TAG_ID, id);
            employees.put(Config.TAG_KFZ, kfz);
            employees.put(Config.TAG_BLAND, bland);
            employees.put(Config.TAG_KREIS, kreis);
            list.add(employees);
        }


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

    final ListAdapter adapter = new SimpleAdapter(
            MainActivity.this, list, R.layout.list_item,
            new String[]{Config.TAG_KFZ, Config.TAG_KREIS},
            new int[]{R.id.kfz, R.id.kreis});

    listView.setAdapter(adapter);


}

private void getJSON(){
    class GetJSON extends AsyncTask<Void,Void,String> {

        ProgressDialog loading;
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            loading = ProgressDialog.show(MainActivity.this,"Suche KFZ Kennzeichen","Warten...",false,false);

        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            loading.dismiss();
            JSON_STRING = s;
            showEmployee();
        }

        @Override
        protected String doInBackground(Void... params) {
            RequestHandler rh = new RequestHandler();
            String s = rh.sendGetRequest(Config.URL_GET_ALL);

            return s;
        }
    }
    GetJSON gj = new GetJSON();
    gj.execute();
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Intent intent = new Intent(this, KfzDetails.class);
    HashMap<String,String> map =(HashMap)parent.getItemAtPosition(position);
    String kfzid = map.get(Config.TAG_ID).toString();
    intent.putExtra(Config.EMP_ID,kfzid);
    startActivity(intent);
}
公共类MainActivity扩展AppCompative实现ListView.OnItemClickListener{
私有列表视图列表视图;
私有字符串JSON_字符串;
编辑文本;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView=(listView)findViewById(R.id.listView);
setOnItemClickListener(this);
listView.smoothScrollToPosition(0);
setTextFilterEnabled(true);
et=(EditText)findViewById(R.id.editText2);
getJSON();
}
私人雇员(){
JSONObject JSONObject=null;
最终ArrayList=新ArrayList();
试一试{
jsonObject=新的jsonObject(JSON_字符串);
最终JSONArray结果=jsonObject.getJSONArray(Config.TAG\uJSON\uArray);
for(int i=0;i

}哦,我找到了SimpleAdapter的解决方案

simpledapter mAdapter;//起初

过滤器运行,但它会复制我的条目
我怎样才能解决这个问题,或者更好,我怎样才能只搜索KFZ而不搜索Kreis

这就是
SimpleAdapter
实现其
过滤器的方式,正如我所说的,您需要使用不同的
适配器
,尝试一些自定义适配器,如:
类适配器扩展
{…
并重写其
onBind
匹配方法
final SimpleAdapter mAdapter =  new SimpleAdapter(
            MainActivity.this,list, R.layout.list_item,
            new String[]{Config.TAG_KFZ, Config.TAG_KREIS},
            new int[]{R.id.kfz, R.id.kreis});

    listView.setAdapter(mAdapter);

    et.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            mAdapter.getFilter().filter(s);

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });