2010-02-05 3 views
6

안녕 얘들 아 내 첫 게시물을 여기에 ... 내 arrayadapter에 arraylist 필터를 사용자 지정 필터를 작성하려고 내 listview 단추를 클릭하면 필터링됩니다.안드로이드 어떻게 내 ArrayAdapter에 대한 필터를 덮어 씁니까?

예를 들어 나는 내 버튼을 클릭하면

public void onClick(View arg0) { 
      String abc = "abc"; 
      m_adapter.getFilter().filter(abc); 
     } 

그러나, 내 응용 프로그램이 예기치 않게 종료 내 버튼을 클릭합니다. 다음은 arrayadapter와 filter에 대한 코드입니다. 도와주세요.

package com.ntu.rosemobile.searchlist; 

public class ResultsAdapter extends ArrayAdapter<SearchItem> implements Filterable{ 

public ArrayList<SearchItem> subItems; 
public ArrayList<SearchItem> allItems; 
private LayoutInflater inflater; 
private PTypeFilter filter; 

public ResultsAdapter(Context context, int textViewResourceId, ArrayList<SearchItem> items) { 

    super(context, textViewResourceId, items); 
     this.subItems = items; 
     this.allItems = this.subItems; 
     inflater= LayoutInflater.from(context); 
} 

@Override 
public Filter getFilter() { 
    if (filter == null){ 
     filter = new PTypeFilter(); 
    } 
    return filter; 
    } 



//@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
     View v = convertView; 
     if (v == null) { 

      v = inflater.inflate(R.layout.listrow, null); 
     } 
     SearchItem o = subItems.get(position); 
     if (o != null) { 
       TextView pname = (TextView) v.findViewById(R.id.productname); 
       TextView neg = (TextView) v.findViewById(R.id.negNum); 
       TextView pos = (TextView) v.findViewById(R.id.posNum); 
       TextView neu = (TextView) v.findViewById(R.id.neuNum); 

       WebImageView productPhoto = (WebImageView)v.findViewById(R.id.pPhoto); 
       if(productPhoto!=null){ 
        productPhoto.setImageUrl(o.getImageUrl().toString()); 
        productPhoto.loadImage(); 
       } 
       if(pname!= null){ 
        pname.setText(o.getProductName().toString()); 
       }      
       if (neg != null) { 
         String a = "" + o.getNegativeReviews(); 
         neg.setText(a);        
       } 
       if(neu != null){ 
        String a = "" + o.getNeutralReviews(); 
        neu.setText(a); 
       } 
       if(pos != null){ 
        String a = "" + o.getPositiveReviews(); 
        pos.setText(a); 
       } 
     } 
     return v; 
} 

private class PTypeFilter extends Filter{ 


    @SuppressWarnings("unchecked") 
    @Override 
    protected void publishResults(CharSequence prefix, 
            FilterResults results) { 
     // NOTE: this function is *always* called from the UI thread. 
     subItems = (ArrayList<SearchItem>)results.values; 

     notifyDataSetChanged(); 
    } 

    @SuppressWarnings("unchecked") 
    protected FilterResults performFiltering(CharSequence prefix) { 
      // NOTE: this function is *always* called from a background thread, and 
      // not the UI thread. 

      FilterResults results = new FilterResults(); 
      ArrayList<SearchItem> i = new ArrayList<SearchItem>(); 

      if (prefix!= null && prefix.toString().length() > 0) { 

       for (int index = 0; index < allItems.size(); index++) { 
        SearchItem si = allItems.get(index); 
        if(si.getPType().compareTo(prefix.toString()) == 0){ 
        i.add(si); 
        } 
       } 
       results.values = i; 
       results.count = i.size();     
      } 
      else{ 
       synchronized (allItems){ 
        results.values = allItems; 
        results.count = allItems.size(); 
       } 
      } 

      return results; 
    } 
    }  
} 
+1

'LogCat'을 사용하고 코드 내에 디버그 문을 남길 수 있습니다. 'LogCat'은 응용 프로그램이 예기치 않게 추락 한 이유를 알아내는 데 가장 좋으며, 어디서 왜 그 이유를 알려줍니다. –

+0

앤서니 (Anthony)가 말했듯이 스택 추적을 해주세요. –

+0

안녕하세요 anthony 신속한 회신 주셔서 감사합니다 .. theres 내 arraylist에 대한 바인딩 예외의 인덱스 것 같습니다 ... 어댑터에 의해 발생합니다 .get ... 거기에 내가 덮어 忘れ하는 기능이 무엇입니까? – alan

답변

9

ArrayAdapter 클래스의 getCount() 메서드를 재정의해야합니다.

+0

어떻게 할 수 있습니까? – heyjii

+0

ArrayAdapter를 확장하는 클래스를 작성해, 그 메소드 내에'getCount()'메소드를 배치합니다. (또는이 일반적인 자바 질문에 대한 구글.) – RaphMclee

+0

어딘가에 대한 설명서가 있습니까? 인터넷 검색은 이와 같은 답변을 제시했지만 실제 게재 방법을 찾을 수는 없습니다. – Dave

관련 문제