【问题标题】:Android Listview search JsonAndroid Listview搜索Json
【发布时间】:2017-08-10 06:35:10
【问题描述】:

我有问题。如果有人键入一些文本,我想使用 SearchView 或 EditText 搜索我的列表视图并过滤我的项目。但是我在这里找不到正确的答案,因为我的 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);
}

}

【问题讨论】:

    标签: android json listview search


    【解决方案1】:

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

    SimpleAdapter mAdapter; // 一开始

    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) {
    
            }
        });
    

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

    【讨论】:

    • 这就是SimpleAdapter 实现其Filter 的方式,正如我所说,您需要使用不同的Adapter,尝试一些自定义适配器,例如:class Adapter extends MatchableArrayAdapter&lt;YourItemDataModel&gt; { ... 和覆盖其onBindmatches 方法
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-20
    • 2017-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多