2013-04-22 1 views
0

사용자 정의 배열 어댑터를 기반으로 작성된 ListView에 필터를 구현했습니다. 이 목록에는 유명인의 이름과 그 유명 인사의 사진이 표시됩니다.사용자 정의 배열 어댑터 (안드로이드)로 ListView 필터링

public class Celebrities extends ListActivity { 

private EditText filterText = null; 
ArrayAdapter<CelebrityEntry> adapter = null; 
private TextWatcher filterTextWatcher = new TextWatcher() { 

    public void afterTextChanged(Editable s) { 
    } 

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

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

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

    //disables the up button 
    getActionBar().setDisplayHomeAsUpEnabled(true); 

    filterText = (EditText) findViewById(R.id.search_box); 
    filterText.addTextChangedListener(filterTextWatcher); 

    adapter = new CelebrityEntryAdapter(this, getModel()); 
    setListAdapter(adapter); 
} 

그리고 CelebrityEntry.java에서 toString() 방법을 무시했습니다

public final class CelebrityEntry { 

private String name; 
private int pic; 

public CelebrityEntry(String name, int pic) { 
    this.name = name; 
    this.pic = pic; 
} 

/** 
* @return name of celebrity 
*/ 
public String getName() { 
    return name; 
} 
/** 
* override the toString function so filter will work 
*/ 
public String toString() { 
    return name; 
} 
/** 
* @return picture of celebrity 
*/ 
public int getPic() { 
    return pic; 
} 

}

그러나, 내가 응용 프로그램을 부팅 및 필터링을 시작할 때, 각 목록 항목이 적절한있다을 사진이 포함되어 있지만 이름은 원래 목록에 불과하며 연예인 몇 명이 실제로 필터를 수행했는지 잘립니다. Kirsten Dunst가 목록의 첫 번째 항목이고 Adam Savage가 두 번째 항목이라고합니다. Adam Savage를 필터링하면 사진이 나옵니다.하지만이 두 가지 정보가 단일 개체의 요소 임에도 불구하고 Kirsten Dunst은 여전히 ​​이름을 말합니다.

분명히 이것은 바람직한 결과가 아닙니다. 생각?

+0

어댑터를 포함 할 수 있습니까? – eski

+1

어댑터가 문제가되었습니다. 오늘 저녁에 그걸 알아 냈어. 감사! –

답변

1

어댑터를 어떻게 사용하고 있는지 잘 모르겠습니다. 목록 뷰를 필터링하는 데 게으른로드 (스크롤 할 때 새 뷰를 부 풀리지 않고 행 뷰를 재활용합니다)를 필터링하는 방법을 보여 드리겠습니다. 당신의 textWatcher에서 지금

private class SlowAdapter extends BaseAdapter { 
    private LayoutInflater mInflater; 

    public SlowAdapter(Context context) { 
     mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 
    public int getCount() { 
     if (filtered) { 
      return filteredItems.length; 
     } else { 
      return unfilteredItems.length; 
     } 
    } 
    public Object getItem(int position) { 
     return position; 
    } 
    public long getItemId(int position) { 
     return position; 
    } 
    public View getView(int position, View convertView, ViewGroup parent) { 
     LinearLayout rowView; 

     if (convertView == null) { 
      rowView = (LinearLayout)mInflater.inflate(R.layout.row, parent, false); 
     } else { 
      rowView = (LinearLayout)convertView; 
     } 
     ImageView celebrity_image = rowView.findViewById(R.id.celebrity_image); 
     TextView celebrity_name = rowView.findViewById(R.id.celebrity_name); 

     if (!filtered) { // use position to get the filtered item. 
      CelebrityEntry c = filteredItems[position]; 
      // do what you do to set the image and text for a celebrity. 

     } else { // use position to get the unfiltered item. 
      CelebrityEntry c = unfilteredItems[position]; 
      // do what you do to set the image and text for a celebrity.     
     } 
     return rowView; 
    } 
} 

를 배열 filteredItems로 문자열을 기반으로 유명 인사를 필터링, 그럼 그냥 = 사실 필터링 설정하고 새로운 SlowAdapter을 만들고 ListView에 해당 설정하십시오 SlowAdapter 내부 클래스를 만듭니다.

필터링되지 않은 항목이없는 경우 unfilteredItems가 사용되며 이후의 전체 소스 필터링에 사용됩니다.

관련 문제