2017-09-24 47 views
2

我搜索了其他解決方案,但coudn't找不到任何類似於我的情況,起初我試圖獲取文本視圖和過濾器的內容,但不成功,請幫助。所以在這裏我創建了一個列表視圖,它由三個文本視圖充滿,每個文本視圖都是一列,數據是從服務器動態添加的。如何根據用戶搜索過濾內容。 這是下面的代碼。如何過濾ListView中的內容

MainActivity.java

public class MainActivity extends AppCompatActivity { 
private void showJSON2(String response){ 
    String id=""; 
    String name=""; 
    String date_from=""; 
    String date_to=""; 
    try { 
     JSONObject jsonObject = new JSONObject(response); 
     JSONArray result = jsonObject.getJSONArray(JSON_ARRAY2); 
     ListView listView = (ListView) findViewById(R.id.list_view); 
     list = new ArrayList<HashMap<String, String>>(); 
     for (int i=0; i<result.length(); i++) { 
      JSONObject notice = result.getJSONObject(i); 
      id = notice.getString(KEY_ID); 
      name = notice.getString(KEY_NAME); 
      date_from = notice.getString(KEY_DATE_FROM); 
      date_to = notice.getString(KEY_DATE_TO); 

      HashMap<String, String> temp= new HashMap<String, String>(); 
      temp.put(FIRST_COLUMN, id); 
      temp.put(SECOND_COLUMN, name); 
      temp.put(THIRD_COLUMN, date_from); 
      temp.put(FOURTH_COLUMN, date_to); 
      list.add(temp); 

     } 

     ListViewAdapters adapter = new ListViewAdapters(this, list); 
     listView.setAdapter(adapter); 

     listView.setOnItemClickListener(new AdapterView.OnItemClickListener() 
     { 
      @Override 
      public void onItemClick(AdapterView<?> parent, final View view, int position, long id) 
      { 
       int pos=position+1; 
       Toast.makeText(MainActivity.this, Integer.toString(pos)+" Clicked", Toast.LENGTH_SHORT).show(); 
      } 

     }); 

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

ListViewAdapters.java -

public class ListViewAdapters extends BaseAdapter{ 
public ListViewAdapters(Activity activity,ArrayList<HashMap<String, String>> list){ 
    super(); 
    this.activity=activity; 
    this.list=list; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    // TODO Auto-generated method stub 
    LayoutInflater inflater=activity.getLayoutInflater(); 
    if(convertView == null){ 
     convertView=inflater.inflate(R.layout.colmn_row, null); 
     txtFirst=(TextView) convertView.findViewById(R.id.name); 
     txtSecond=(TextView) convertView.findViewById(R.id.gender); 
     txtThird=(TextView) convertView.findViewById(R.id.age); 
     txtFourth=(TextView) convertView.findViewById(R.id.status); 
    } 
    HashMap<String, String> map=list.get(position); 
    txtFirst.setText(map.get(FIRST_COLUMN)); 
    txtSecond.setText(map.get(SECOND_COLUMN)); 
    txtThird.setText(map.get(THIRD_COLUMN)); 
    txtFourth.setText(map.get(FOURTH_COLUMN)); 
    return convertView; 
    } 
} 
+0

使用[此](https://gist.github.com/pskink/2dd4d17a93caf02ff696533e82f952b0)通用'Filterable'適配器(而不是'BaseAdapter'),爲你的適配器的基類,並重寫兩個方法'onBind()'和'matches()' – pskink

+0

你將如何過濾?使用什麼價值? – npk

+0

@npk用戶輸入的值。 –

回答

2

在MainActivity.java,

public class MainActivity extends AppCompatActivity { 

    ArrayList<HashMap<String, String>> list = new ArrayList<>(); 
    ListViewAdapters adapter; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_default); 

     adapter = new ListViewAdapters(this, list); 
     listView.setAdapter(adapter); 
    } 

    private void showJSON2(String response) { 
     ... 
     list.clear(); 
     ... 
     list.add(temp); 
     ... 

     // ListViewAdapters adapter = new ListViewAdapters(this, list); 
     // listView.setAdapter(adapter); 
     adapter.notifyDataSetChanged(); 
     ... 
    } 

} 

在適配器類,

public class ListViewAdapters extends BaseAdapter { 

    ArrayList<HashMap<String, String>> origList = new ArrayList<>(); 

    public ListViewAdapters(Activity activity,ArrayList<HashMap<String, String>> list) { 
     super(); 
     this.activity=activity; 
     this.list=list; 
     this.origList=list; 
    } 

    ... 

    void filter(String filterString) { 
     list = new ArrayList<>(); 
     for (HashMap<String, String> item: origList) { 
      // use your own logic to filter 
      if (itemShouldBeAdded) 
       list.add(item); // add if item is inside filter 
     } 
     notifyDataSetChanged(); 
    } 

    void cancelFilter() { 
     list = origList; 
     notifyDataSetChanged(); 
    } 

} 

現在,過濾,調用函數的過濾器。

adapter.filter("some_string"); 

爲了消除濾波器,

adapter.cancelFilter(); 
+0

爲什麼不使用'Filterable'界面? https://stackoverflow.com/questions/24769257/custom-listview-adapter-with-filter-android –

+0

我不知道它:( – npk

1

我使用這種方式,根據用戶的輸入來過濾名單。

private void setFilter(EditText et_search_bar, ArrayList<String> list, OnListUpdated onListUpdated) { 

    if (et_search_bar != null) { 
     TextWatcher textWatcher; 
     textWatcher = new TextWatcher() { 
      @Override 
      public void onTextChanged(CharSequence s, int start, int before, int count) { 

       // TODO Auto-generated method stub 
      } 

      @Override 
      public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

       // TODO Auto-generated method stub 
      } 

      @Override 
      public void afterTextChanged(Editable s) { 
       // filter your list from your input 
       if (!previousValue.equalsIgnoreCase(s.toString())) { 
        if (!TextUtils.isEmpty(s.toString())) { 
         previousValue = s.toString(); 
         ArrayList<String> temp = new ArrayList<>(); 
         for (String bookmarkBean : list) { 
          //or use .contains(text) 
          if (bookmarkBean.toLowerCase().contains(s.toString())) { 
           temp.add(bookmarkBean); 
          } 
         } 
         //update recyclerview 
         if (onListUpdated != null) { 


          onListUpdated.onListFiltered(temp); 
         } 
        } else onListUpdated.onListFiltered(filterBean); 
       } 
      } 
     }; 
     et_search_bar.addTextChangedListener(textWatcher); 
    } 
} 

 public interface OnListUpdated { 
     void onListFiltered(ArrayList<String> list);} 



     setFilter(et_search_bar, filterBean, new OnListUpdated() { 
     @Override 
     public void onListFiltered(ArrayList<String> list) { 
     //    notify your adapter 
     } 
     });