1

我对安卓很陌生。我正在尝试使用 arrayadapter 在项目列表上开发搜索功能,如下所示。搜索功能应该对模型的名称执行搜索。

    public class InteractiveArrayAdapter extends ArrayAdapter<Model> implements Filterable{

          private final List<Model> list;
          private final Activity context;
          protected Filter filter;
          private ArrayList<Model> arraylist;

          public InteractiveArrayAdapter(Activity context, List<Model> list) {
            super(context, R.layout.mylist, list);
            this.context = context;
            this.list = list;
            this.arraylist = new ArrayList<Model>();
            this.arraylist.addAll(list);
            //filter = new PhysFilter();
          }

          class ViewHolder {
            protected TextView text;
            protected CheckBox checkbox;
            protected ImageView icon_verify, icon_auth;
          }

          @Override
          public View getView(int position, View convertView, ViewGroup parent) {
            View view = null;
            if (convertView == null) {
              LayoutInflater inflator = context.getLayoutInflater();
              view = inflator.inflate(R.layout.mylist, null);
              final ViewHolder viewHolder = new ViewHolder();
              viewHolder.text = (TextView) view.findViewById(R.id.label);
              Typeface face=Typeface.createFromAsset(getAssets(), "fonts/verdana.ttf");
              viewHolder.text.setTypeface(face);

              viewHolder.icon_verify = (ImageView) view.findViewById(R.id.icon_verify);
              if(list_m.get(position).isVerified() == true)
                  viewHolder.icon_verify.setImageResource(R.drawable.verify_icon2);
              else
                  viewHolder.icon_verify.setImageResource(R.drawable.verify_icon);
              viewHolder.icon_auth = (ImageView) view.findViewById(R.id.icon_auth);
              if(list_m.get(position).isAuthorised() == true)
                  viewHolder.icon_auth.setImageResource(R.drawable.auth_icon2);
              else
                  viewHolder.icon_auth.setImageResource(R.drawable.auth_icon);
              viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check);
              viewHolder.checkbox
                  .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                    @Override
                    public void onCheckedChanged(CompoundButton buttonView,
                        boolean isChecked) {
                      Model element = (Model) viewHolder.checkbox.getTag();
                      element.setSelected(buttonView.isChecked());
                      System.out.println("checked change");

                    }
                  });
              view.setTag(viewHolder);
              viewHolder.checkbox.setTag(list.get(position));
            } else {
              view = convertView;
              ((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
            }
            ViewHolder holder = (ViewHolder) view.getTag();
            holder.text.setText(list.get(position).getName());
            holder.checkbox.setChecked(list.get(position).isSelected());
            return view;
          }

        // Filter Class
            public void filter(String charText) {
                charText = charText.toLowerCase(Locale.getDefault());
                list.clear();
                if (charText.length() == 0) {
                    list.addAll(arraylist);
                } else {
                    for (Model wp : arraylist) {
                        if (wp.getName().toLowerCase(Locale.getDefault())
                                .contains(charText)) {
                            list.add(wp);
                        }
                    }
                }
                notifyDataSetChanged();
            }
}

列表.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@+id/label"
        android:layout_marginLeft="70px"
        android:layout_marginTop="10px"
        android:textSize="22px" >
        <requestFocus />
    </TextView>

    <CheckBox
        android:id="@+id/check"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:textSize="0sp"
        android:focusable="false"
        android:layout_marginLeft="10dp"
        android:button="@layout/checkbox" />

    <ImageView
        android:id="@+id/icon_verify"
        android:layout_width="30px"
        android:layout_height="30px"
        android:layout_marginLeft="600px"
        android:layout_marginRight="10px"
        android:layout_marginTop="30px" >
    </ImageView>
    <ImageView
        android:id="@+id/icon_auth"
        android:layout_width="30px"
        android:layout_height="30px"
        android:layout_marginLeft="650px"
        android:layout_marginRight="5px"
        android:layout_marginTop="30px" >
    </ImageView>

</RelativeLayout> 

模型.java

public class Model {
    private int tradeID, pos;
    private String name;
    private boolean selected, verified, authorised;

    public Model(int pos, String name, int tradeID, boolean verified, boolean authorised){
        this.pos = pos;
        this.name = name;
        this.tradeID = tradeID;
        this.verified = verified;
        this.authorised = authorised;
        selected = false;
    }

    public int getPos() {
        return pos;
    }
    public void setPos(int pos) {
        this.pos = pos;
    }
    public int getTradeID() {
        return tradeID;
    }

    public void setTradeID(int tradeID) {
        this.tradeID = tradeID;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isSelected() {
        return selected;
    }

    public void setSelected(boolean selected) {
        this.selected = selected;
    }

    public boolean isVerified() {
        return verified;
    }
    public void setVerified(boolean verified) {
        this.verified = verified;
    }
    public boolean isAuthorised() {
        return authorised;
    }
    public void setAuthorised(boolean authorised) {
        this.authorised = authorised;
    }

}

以下是我目前在编辑文本上所做的,但是当我输入任何字符时,列表显示将为空。谁能建议我应该做什么或我在这里做错了什么?任何帮助将不胜感激。谢谢!

EditText search=(EditText)header.findViewById(R.id.searchbox);

search.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
            // When user changed the Text
            String text = search.getText().toString().toLowerCase(Locale.getDefault());
                    ((InteractiveArrayAdapter) adapter).filter(text);
        }

        @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                          
        }
});
4

0 回答 0