2017-03-12 18 views
0

私のlistViewをフィルタリングしようとしています。しかし、私がEditTextに文字を入力すると、私のlistViewは空になります。 Log.dは、私のm_adapterに0カウントがあることを示しています。Filter listView from ItemAdapter

私はグーグルではありましたが、私にはうまくいかない解決策は見当たりませんでした。

ItemAdapter:

public class ItemAdapter extends ArrayAdapter<Item> { 

static ArrayList<Item> objects; 

public ItemAdapter(Context context, int textViewResourceId, ArrayList<Item> objects) { 
    super(context, textViewResourceId, objects); 
    this.objects = objects; 
} 

public View getView(int position, View convertView, ViewGroup parent){ 

    View v = convertView; 

    if (v == null) { 
     LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     v = inflater.inflate(R.layout.phonenumber_item, null); 
    } 

    Item i = objects.get(position); 

    if (i != null) { 

     TextView ttd = (TextView) v.findViewById(R.id.toptextdata); 
     TextView btd = (TextView) v.findViewById(R.id.desctext);   

     if (ttd != null){ 
      ttd.setText(i.getName()); 
     } 

     if (btd != null){ 
      btd.setText(i.getDetails()); 
     } 
    } 
    return v; 
    } 
} 

フラグメント:

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 

    View rootView = inflater.inflate(R.layout.search_list, container, false); 
    inputSearch = (EditText) rootView.findViewById(R.id.inputSearch); 

    m_adapter = new ItemAdapter(getActivity(), R.layout.phonenumber_item, m_parts); 
    setListAdapter(m_adapter); 

    viewParts = new Runnable() { 
     public void run() { 
      handler.sendEmptyMessage(0); 
     } 
    }; 

    Thread thread = new Thread(null, viewParts); 
    thread.start(); 

    return rootView; 
} 

private Handler handler = new Handler() { 
    public void handleMessage(Message msg) {    

     m_parts.add(new Item("Phonenumber 1", "xxx-xxxxxx"));    
     m_parts.add(new Item("Phonenumber 2", "xxx-xxxxxx")); 

     m_adapter = new ItemAdapter(getActivity(), R.layout.phonenumber_item, m_parts); 

     setListAdapter(m_adapter); 

     inputSearch.addTextChangedListener(new TextWatcher() { 

      @Override 
      public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) { 

       m_adapter.getFilter().filter(cs); 
       Log.d("Debug", "Count: " + m_adaper.getCount()); 

      } 

      @Override 
      public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, 
              int arg3) { 
       // TODO Auto-generated method stub 

      } 

      @Override 
      public void afterTextChanged(Editable arg0) { 
       // TODO Auto-generated method stub 
      } 
     }); 
    } 
}; 
+0

コードの改善のためのちょっとしたヒント:新しいアダプタを2回作成し、このアダプタを「ListView」に2回設定すると、そうしないでください。その代わりに 'onCreateView()'で一度作成し、必要に応じて配列に新しい項目を追加してから、 'adapter 'を呼び出してください。 notifyDataSetChanged() '新しいデータを' ListView'に反映します –

答えて

0

私は少し違う、それをすることによってそれを解決しました。ここにコードがあります。 クレジットは、このgithubのに行く: https://github.com/ihrupin/samples/tree/master/android/Filter_ListView_Sample_Custom_Objects/src/com/hrupin/sample/filterlist

PhonenumberSearchNumbers.java

public class PhonenumberSearchNumbers { 

public static List<PhonenumberSearchItem> getItems() { 
    List<PhonenumberSearchItem> m_parts = new ArrayList<PhonenumberSearchItem>(); 
    m_parts.add(new PhonenumberSearchItem("Number 1", "xxx-xxxxx")); 
    m_parts.add(new PhonenumberSearchItem("Number 2", "xxx-xxxxx")); 
    m_parts.add(new PhonenumberSearchItem("Number 3", "xxx-xxxxx")); 
    } 
} 

PhonenumberSearchAdapter.java

public class PhonenumberSearchAdapter extends ArrayAdapter<PhonenumberSearchItem> { 
private LayoutInflater vi; 
private static int mResource = R.layout.search_item; 

public PhonenumberSearchAdapter(Activity activity, final List<PhonenumberSearchItem> items) { 
    super(activity, mResource, items); 
    vi = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

} 

public static class ViewHolder { 
    private TextView textViewName; 
    private TextView textViewNumber; 

    public ViewHolder(View v) { 
     this.textViewName = (TextView) v.findViewById(R.id.toptextdata); 
     this.textViewNumber = (TextView) v.findViewById(R.id.desctext); 
    } 
} 

public View getView(int position, View convertView, ViewGroup parent) { 
    ViewHolder holder; 
    if (convertView == null) { 
     convertView = vi.inflate(mResource, null); 
     holder = new ViewHolder(convertView); 
     convertView.setTag(holder); 
    } else { 
     holder = (ViewHolder) convertView.getTag(); 
    } 

    final PhonenumberSearchItem item = getItem(position); 
    if (item != null) { 
     holder.textViewName.setText(Html.fromHtml(item.getName())); 
     holder.textViewNumber.setText(Html.fromHtml(item.getNumber())); 
    } 
    return convertView; 
    } 
} 

PhonenumberSearchItem.java

​​

をMainFragment.java

public class PhonenumberAllaFragment extends ListFragment implements TextWatcher { 

private EditText inputSearch; 
private static List<PhonenumberSearchItem> numbers = PhonenumberSearchNumbers.getItems(); 
private EditText editTextFilter; 
private PhonenumberSearchAdapter adapter; 

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

} 

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

@Override 
public void afterTextChanged(Editable s) { 

}