English 中文(简体)
ArrayAdapter 过滤器返回错误的项目
原标题:ArrayAdapter filter return wrong items
  • 时间:2012-05-22 14:24:12
  •  标签:
  • android

我对自定义对象的过滤数组适配器有问题。 我对 String () 使用 owerride 自定义对象, 所以它返回了名称, 我想要过滤。 问题在于它的文件员更正了项目数, 但错误的项目数。 例如, 我有一个列表中的项目 a1, a2, b1, b1, b2 。 当我输入 b 过滤器, 我看到 a1, a2 时, 我有一个调适器代码。 我想问题在于 :

class CustomerAdapter extends ArrayAdapter<Customer> {

private LayoutInflater mInflater;
private List<Customer> customers;

public CustomerAdapter(Context context, int textViewResourceId,
        List<Customer> customers) {
    super(context, textViewResourceId, customers);
    mInflater = LayoutInflater.from(context);
    this.customers = customers;
时 时

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    ViewHolder holder;

    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.customers_list_item, null);

        holder = new ViewHolder();
        holder.text = (TextView) convertView
                .findViewById(R.id.textViewCustomerName);
        holder.icon = (LinearLayout) convertView
                .findViewById(R.id.linearLayoutCustomersListEdit);

        convertView.setTag(holder);
    时 时 else {
        holder = (ViewHolder) convertView.getTag();
    时 时

    // Bind the data efficiently with the holder.
    holder.text.setText(customers.get(position).getCustomerName());

    return convertView;
时 时

static class ViewHolder {
    TextView text;
    LinearLayout icon;
时 时

时 时

以下是过滤设置和呼叫 :

customerAdapter = new CustomerAdapter(this,
                R.layout.customers_list_item, repository.getCustomers());
setListAdapter(customerAdapter);

etSearch = (EditText) findViewById(R.id.editText_customers_search);
        etSearch.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int arg1, int arg2,
                int arg3) {
            customerAdapter.getFilter().filter(s.toString());
        时 时

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1,
                int arg2, int arg3) {
        时 时

        @Override
        public void afterTextChanged(Editable arg0) {
        时 时
    时 时);

    getListView().setTextFilterEnabled(true);
最佳回答
List<Customer> customers = new List<Customer>();
List<Customer> tempcustomers = new List<Customer>();
customers = repository.getCustomers();
etSearch .addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub
                System.out.println("onTextChanged Key presed..."+s);


            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub
                System.out.println("beforeTextChanged Key presed..."+filterText.getText());

            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub
                System.out.println("afterTextChanged Key presed..."+filterText.getText());
                if (!etSearch .getText().toString().equalsIgnoreCase("")){
                    tempcustomers = new List<Customer>();
                    String text = etSearch .getText().toString();

                    for(int i=0 ;i< customers .size();i++){

                        if(customers .get(i).toUpperCase().toString().contains(text.toUpperCase())){
                            tempcustomers .add(customers .get(i));

                        }
                    }

                        }
                    customerAdapter = new CustomerAdapter(this,
                R.layout.customers_list_item,tempcustomers  );
setListAdapter(customerAdapter);

                }else{
                    customerAdapter = new CustomerAdapter(this,
                R.layout.customers_list_item,customers  );
setListAdapter(customerAdapter);

                }
            }
        });
    }
问题回答

暂无回答




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签