2011-11-17 74 views
-2

我已經通過其他類似的問題,但我無法找到解決方案。當用戶按下按鍵序列時,我想使自定義數組適配器可過濾。如何在自定義數組適配器的listview中實現過濾器?

我是新來的。請讓我知道我應該添加什麼以及在哪裏。

public class ShowTags extends ListActivity { 
Bundle savedInstanceState; 
private final Context activityContext = this; 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    this.savedInstanceState = savedInstanceState; 
    new FetchTagFromDatabase().execute(); 
    populatetagList(); 
} 


/** 
* 
*/ 
private void populatetagList() { 
    ListView listView = getListView(); 
    listView.setTextFilterEnabled(true); 
    OnItemClickListener listener = new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View view,int position, 
       long id) { 

      String tagId = (((TextView) view.findViewById(R.id.title)).getTag()).toString(); 
      Intent intent = new Intent(activityContext, ShowPostForTag.class); 
      intent.putExtra("tagId", tagId); 
      startActivity(intent); 
     } 
    }; 
    listView.setOnItemClickListener(listener); 
}  


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.show_tags_option_menu, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
     case R.id.refresh_tags: 
      this.onCreate(savedInstanceState); 
      break; 
    } 
    return true; 
} 


private class FetchTagFromDatabase extends AsyncTask<Void, Void, TagListAdaptor> { 
    private ProgressDialog dialog; 

    @Override 
    protected void onPreExecute(){ 
     dialog = new ProgressDialog(ShowTags.this); 
     dialog.setCancelable(true); 
     dialog.setMessage("Loading..."); 
     dialog.show(); 
    } 

    @Override 
    protected TagListAdaptor doInBackground(Void... arg0) { 

     List<Tag> tags = DatabseUtil.fetchAllTags(false, ShowTags.this); 
     TagListAdaptor adaptor = new TagListAdaptor(ShowTags.this,R.layout.tag_row,tags); 
     return adaptor; 
    } 

    @Override 
    protected void onPostExecute(TagListAdaptor adaptor) { 
     setListAdapter(adaptor); 
     dialog.dismiss(); 
    } 
} 

private class TagListAdaptor extends ArrayAdapter<Tag> implements Filterable{ 
    private List<Tag> tags; 

    public TagListAdaptor(Context context, 
      int textViewResourceId, 
      List<Tag> tags) { 
     super(context, textViewResourceId,tags); 
     this.tags = tags; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View view = convertView; 
     if (view == null) { 
      LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      view = layoutInflater.inflate(R.layout.tag_row, null); 
     } 
     Tag tag = tags.get(position); 
     TextView titleTextView = (TextView) view.findViewById(R.id.title); 
     titleTextView.setText(tag.getTitle()); 
     titleTextView.setTag(tag.getId()); 
     Log.i("SHOW TAGS", tag.getTitle()); 
     TextView postCountTextView = (TextView) view.findViewById(R.id.post_count); 
     postCountTextView.setText(((Integer)tag.getPostCount()).toString()); 
     return view; 

    } 
} 

}

+1

請描述多一點你究竟做了什麼,怎麼做,哪些做不到。是的,你有代碼*(代碼是一個很好的問題的一部分)*,但有點太多了。包括我在內的大多數人都不會花一點時間來破譯它所做的和意義。修剪它到相關的部分,以小的,容易得到的片段。否則,人們進來並基本閱讀*「這是我的完整代碼,我想要XY,爲我修好並告訴我你什麼時候完成了」*。這不會激勵許多人利用他們的個人時間給你一個答案。 – 2011-11-17 16:02:59

回答

相關問題