阵列适配器中的自定义过滤

时间:2015-05-23 17:47:17

标签: android listview android-arrayadapter android-filter android-filterable

我想根据用户输入搜索自定义列表视图(使用的arrayadaptor)。我已经在自定义列表视图中实现了可过滤的类,但是为了优化高速缓存,我使用了持有者来携带当前行。现在,根据用户输入获取项目变得非常复杂。

任何人都可以帮我解决问题吗?

我的主要课程是,

public class ambulanceActivity extends Activity{
dbhelper db4;
ListView lstview_Items;
int images =  R.drawable.call;
final ArrayList<String> mArrayList = new ArrayList<String>();
final ArrayList<String> mArrayList1 = new ArrayList<String>();
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.ambulance_list);
    EditText inputSearch=(EditText) findViewById(R.id.inputSearch);
    lstview_Items=(ListView) findViewById(R.id.listView1);
    db4 = new dbhelper(getApplicationContext());
    db4.creteDB();
    Cursor checkerA = db4.getDetails();
    checkerA.moveToFirst();
    while (!checkerA.isAfterLast()) {
        mArrayList.add(checkerA.getString(checkerA
                .getColumnIndex("ambname")));
        mArrayList1.add(checkerA.getString(checkerA
                .getColumnIndex("ambnum")));
        checkerA.moveToNext();
    }
    checkerA.close();
    final CustomAdapter adapter=new CustomAdapter(this,mArrayList,images,mArrayList1);
    lstview_Items.setAdapter(adapter);
    lstview_Items.setTextFilterEnabled(true);
    inputSearch.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence cs, int arg1, int arg2,
                                  int arg3) {
            adapter.getFilter().filter(cs);
        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1,
                                      int arg2, int arg3) {
        }
        @Override
        public void afterTextChanged(Editable arg0) {

        }
      });
     }
   }

我的自定义列表视图是,我刚刚在这里实现了可过滤的类

class CustomAdapter extends ArrayAdapter <String> implements Filterable{
Context con;
int images;
ArrayList<String> names;
ArrayList<String> number;

public CustomAdapter(Con c, ArrayList<String> mArrayList,int imgs, ArrayList<String> num) {
    super(c, R.layout.list_entry,R.id.contact,mArrayList);
    this.con=c;
    this.images=imgs;
    this.names=mArrayList;
    this.number=num;
}
class  MyViewHolder extends Activity
{
    ImageView myImage;
    TextView myContactName;
    TextView myContactNumber;

    MyViewHolder(View v)
    {
        myImage=(ImageView) v.findViewById(R.id.police);
        myContactName=(TextView) v.findViewById(R.id.name);
        myContactNumber=(TextView) v.findViewById(R.id.contact);

    }
}
public View getView(final int position, View convertView, ViewGroup parent) {
    View row = convertView;
    MyViewHolder holder=null;
    if (row == null){
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = inflater.inflate(R.layout.list_entry, parent, false);
        holder=new MyViewHolder(row);
        row.setTag(holder);
    }
    else
    {
        holder=(MyViewHolder) row.getTag();
    }
    ImageView call=(ImageView) row.findViewById(R.id.police);
    call.setTag(position);
    call.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent=new Intent("android.intent.action.CALL");
            Uri data = Uri.parse("tel:"+number.get(position).toString());
            intent.setData(data);
            context.startActivity(intent);
        }
    });
    TextView contact=(TextView) row.findViewById(R.id.contact);
    holder.myImage.setImageResource(images);
    holder.myContactName.setText(names.get(position));
    holder.myContactNumber.setText(number.get(position));
    return row;
    }
   }

0 个答案:

没有答案