android+自定义过滤器,在android中自定义ArrayAdapter中的自定义getFilter

我无法在自定义arrayAdapter中实现自定义getFilter。其实我不知道如何实现它。尝试各种代码,但仍没有运气。这里是我的自定义阵列适配器。

package com.test.FilterableList.Adapters;

import java.util.ArrayList;

import android.content.Context;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.ArrayAdapter;

import android.widget.TextView;

import com.test.FilterableList.Models.ListTO;

import com.test.FilterableList.R;

import android.widget.Filterable;

public class FilterableAdapter extends ArrayAdapter implements Filterable {

// declaring our ArrayList of items

public ArrayList objects;

/* here we must override the constructor for ArrayAdapter

* the only variable we care about now is ArrayList objects,

* because it is the list of objects we want to display.

*/

public FilterableAdapter(Context context, int textViewResourceId, ArrayList objects) {

super(context, textViewResourceId, objects);

this.objects = objects;

}

/*

* we are overriding the getView method here - this is what defines how each

* list item will look.

*/

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

// assign the view we are converting to a local variable

View v = convertView;

// first check to see if the view is null. if so, we have to inflate it.

// to inflate it basically means to render, or show, the view.

if (v == null) {

LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

v = inflater.inflate(R.layout.list_item, null);

}

/*

* Recall that the variable position is sent in as an argument to this method.

* The variable simply refers to the position of the current object in the list. (The ArrayAdapter

* iterates through the list we sent it)

*

* Therefore, i refers to the current Item object.

*/

ListTO i = objects.get(position);

if (i != null) {

// This is how you obtain a reference to the TextViews.

// These TextViews are created in the XML files we defined.

TextView tt = (TextView) v.findViewById(R.id.list_name);

if (tt != null){

tt.setText(i.FileName);

}

}

// the view must be returned to our activity

return v;

}

}

这里是ListTO类。

package com.test.FilterableList.Models;

public class ListTO {

public int Id;

public String FileName;

public String FileUri;

public ListTO(int id, String fileName, String fileUri) {

Id = id;

FileName = fileName;

FileUri = fileUri;

}

}

这里是布局。

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

android:background="@drawable/blacklikenbackground"

tools:context=".AllListActivity" >

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="Search"

android:id="@+id/inputSearch"

/>

android:id="@+id/test_list"

android:layout_width="match_parent"

android:layout_height="wrap_content">

这里的搜索关键字来自“inputSearch”EditText。

这里是文本更改的侦听器。

inputSearch.addTextChangedListener(new TextWatcher() {

@Override

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

// When user changed the Text

// Toast.makeText(getActivity(), cs.toString(), Toast.LENGTH_LONG).show();

m_adapter.getFilter().filter(cs);

}

@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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值