2017-03-18 17 views
0

問題があります。 SearchViewまたはEditTextでリストビューを検索し、誰かが何らかのテキストを入力した場合、アイテムをフィルタリングしたい。私のjsonファイルはPHP/mysqlデータベースから来ているので、私は正しい答えを見つけられません。誰かが私を助けることができますか?ここでAndroidリストビュー検索Json

は私のコードである

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

}

答えて

0

ああ、私は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を検索することができること、またはより良い修正することができますどのように私のエントリ を複製しないクライス

+0

のためにこれがあります'SimpleAdapter'が' Filter'をどのように実装しているかは、私が言ったように、別の 'Adapter'を使う必要があります。' class Adapter extends' [MatchableArrayAdapter](https://gist.github.com/pskink)/2dd4d17a93caf02ff696533e82f952b0) ' {...'}と 'onBind'メソッドと' matches'メソッドをオーバーライドします – pskink

関連する問題