Android-ListView與EditText的 關鍵字搜尋 KEYWORD SEARCH

關鍵字搜尋-Android
想要做出像Google一樣的關鍵字搜尋的話
這樣就是要在 EditText 這個元件做一些機關拉
跟先前教的一樣,只要在 TextWatcher 的監聽方法做一些判別…


假若今天你有三種資料算一組如下圖:

關鍵字搜尋

資料分別為:用戶名,用戶類型,餘額,程式代碼為 mPlay_name,mPlay_groupname,mPlay_balance

當然也需要一組一樣的資料當作緩存區 mPlay_name_tem,mPlay_groupname_tem,mPlay_balance_tem

之後用下面我寫的方法:

int textlength=0;
    /**
     * 監聽輸入筐
     *
     * @param _search
     */
    public void CheckAndReload(final EditText _search) {
        _search.addTextChangedListener(new TextWatcher()
        {
            public void afterTextChanged(Editable s)
            {
                  // Abstract Method of TextWatcher Interface.
            }
            public void beforeTextChanged(CharSequence s,
                    int start, int count, int after)
            {
                // Abstract Method of TextWatcher Interface.
            }
            public void onTextChanged(CharSequence s,
                    int start, int before, int count)
            {
                textlength = _search.getText().length();
                mPlay_name_tem.clear();
                mPlay_groupname_tem.clear();
                mPlay_balance_tem.clear();
                for (int i = 0; i < mPlay_name.size(); i++){
                    if (textlength <= mPlay_name.get(i).length()){
                        if(mPlay_name.get(i).toLowerCase().contains(
                                _search.getText().toString().toLowerCase().trim()))
                        {
                            mPlay_name_tem.add(mPlay_name.get(i));
                            mPlay_groupname_tem.add(mPlay_groupname.get(i));
                            mPlay_balance_tem.add(mPlay_balance.get(i));
                        }
                      }
                }
                lv.notifyDataSetChanged();
                }
            });
    }

P.S. lv = BaseAdapter;

我自己客製化的 lv = new AccountListView(getBaseContext(), mPlay_name_tem, mPlay_groupname_tem, mPlay_balance_tem);

/**
     * 客製化ListView
     *
     * @author joey
     *
     */
    class AccountListView extends BaseAdapter {
        LayoutInflater mInflater;
        List<String> Tx1, Tx2, Tx3;
        public AccountListView(Context ctx, List<String> mPlay_type, List<String> mPlay_range,
                List<String> mPlay_price) {
            super();
            mInflater = (LayoutInflater) ctx.getSystemService(ctx.LAYOUT_INFLATER_SERVICE);
            Tx1 = mPlay_type;
            Tx2 = mPlay_range;
            Tx3 = mPlay_price;
        }
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return Tx1.size();
        }
        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return Tx1.get(position);
        }
        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final ViewHolder holder;
            OnClickToGo onClickGo = new OnClickToGo(position);
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.account_list, null);
                holder = new ViewHolder((TextView) convertView.findViewById(R.id.user_name),
                        (TextView) convertView.findViewById(R.id.user_type),
                        (TextView) convertView.findViewById(R.id.user_balance));
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.TX_1.setText(Tx1.get(position));
            holder.TX_2.setText(Tx2.get(position));
            holder.TX_3.setText(Tx3.get(position));
            holder.TX_1.setOnClickListener(onClickGo);
            return convertView;
        }
        public class ViewHolder {
            TextView TX_1, TX_2, TX_3;
            public ViewHolder(TextView TX_1, TextView TX_2, TextView TX_3) {
                this.TX_1 = TX_1;
                this.TX_2 = TX_2;
                this.TX_3 = TX_3;
            }
        }
        class OnClickToGo implements OnClickListener {
            int select = 0;
            public OnClickToGo(int sel) {
                select = sel;
            }
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                case R.id.user_name:
                    getUserCustoms(String.valueOf(mPlay_name_id.get(select)));
                    break;
                case R.id.account_2:
                    break;
                case R.id.account_3:
                    break;
                default:
                    break;
                }
            }
        }
    }

這樣就大功告成拉~
來看看結果
關鍵字搜尋
是不是很酷呢 ~

Facebook Comments

1 reply to Android-ListView與EditText的 關鍵字搜尋 KEYWORD SEARCH

  1. 寫得不錯

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *