利用Adapter动态刷新数据实现联想词提示

最近要实现一个需求:根据edittext输入框内的关键词,动态的在listpopwindow中显示联想词,利用listpopwindow实现下拉框提示,可以参考我的另一篇文章listpopwindow实现EditText下拉框显示列表。
先上效果图

adapter动态刷新数据

博主的思路是为edittext设置文字变化监听addTextChangedListener,在onTextChanged方法,实现关键词判断(判断出应该显示哪些提示词),然后将新的提示词放进matchList集合中,通知adapter刷新view。
关键代码:

 @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (inputCompany.getText().toString().length() > 0) {
                    setCompany.setBackgroundColor(getColor(R.color.dark_blue));

                    //这是博主自己写的工具类,用来判断包含用户输入的关键词的提示词
                    KeyWordMatch keyWordMatch = new KeyWordMatch();

                    List ml = keyWordMatch.getMatch(list,s.toString());//新的提示词集合
                    matchList.clear();//清除原有matchList集合
                    matchList.addAll(ml);//将新的提示词赋值给matchList
                    adapter.notifyDataSetChanged();//通知view数据源改变,刷新biew
                    listPopupWindow.show();//显示listpopwindow
                }
            }

下面贴出实现这个功能所需的代码,包括一个主activity:CompanySet.java;主activity的布局文件:companyset.xml;一个listpopwinodw的item的布局文件:companyitem.xml;一个工具类,用来判断应显示的提示词:KeyWordMatch.java

CompanySet.java

package com.example.zyk97.infotest.Activity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListPopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zyk97.infotest.R;
import com.example.zyk97.infotest.Utils.KeyWordMatch;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class CompanySet extends AppCompatActivity {


    @BindView(R.id.back)
    Button back;
    @BindView(R.id.confirm)
    Button confirm;
    @BindView(R.id.my_company)
    TextView myCompany;
    @BindView(R.id.input)
    EditText inputCompany;
    @BindView(R.id.set)
    Button setCompany;
    private List list=new ArrayList<>();
   // private static String[] matchList = {"格力", "海尔", "美的", "小米", "讯飞"};
    private List matchList=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.companyset);
        ButterKnife.bind(this);
        list.add("海尔");
        list.add("格力");
        list.add("美的");
        list.add("讯飞");
        list.add("大疆");
        list.add("奎格");
        showListPopWindow();
    }

    @OnClick(R.id.back)
    public void setBack(View view) {
        finish();
    }

    @OnClick(R.id.confirm)
    public void setConfirm(View view) {
        finish();
    }

    @OnClick(R.id.my_company)
    public void resetMyCompany() {
        myCompany.setText("我的公司:未设置");
    }

    //显示提示词
    public void showListPopWindow() {
        final ListPopupWindow listPopupWindow = new ListPopupWindow(this);
        final ArrayAdapter adapter = new ArrayAdapter<>(this, R.layout.companyitem, matchList);
        listPopupWindow.setAdapter(adapter);//设置适配器,使用默认的适配器
        listPopupWindow.setAnchorView(inputCompany);//设置listpopowindow和哪个控件相邻
        listPopupWindow.setModal(true);//指定listpopwindow是否阻止在显示的时候将内容输入其他窗口


        listPopupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView parent, View view, int position, long id) {
                inputCompany.setText(matchList.get(position));
                listPopupWindow.dismiss();//如果已经选择,隐藏listpopwindow
            }
        });
        inputCompany.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (inputCompany.getText().toString().length() > 0) {
                    setCompany.setBackgroundColor(getColor(R.color.dark_blue));
                    KeyWordMatch keyWordMatch = new KeyWordMatch();

                    List ml = keyWordMatch.getMatch(list,s.toString());
                    matchList.clear();
                    matchList.addAll(ml);
                    adapter.notifyDataSetChanged();
                    listPopupWindow.show();
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (inputCompany.getText().toString().length() == 0) {
                    setCompany.setBackgroundColor(getColor(R.color.gray2));
                }
            }
        });
    }

    @OnClick(R.id.set)
    public void setMyCompany(View view) {
        myCompany.setText(inputCompany.getText());
    }

    @OnClick(R.id.business1)
    public void showBusiness1(View view) {
        AlertDialog.Builder builder = new AlertDialog.Builder(CompanySet.this);
        builder.setTitle("家电行业公司");
        final String[] cities = {"青岛海尔", "格力电器", "美的集团"};
        builder.setItems(cities, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                myCompany.setText(cities[which]);
            }
        });
        builder.show();
    }

    @OnClick(R.id.business2)
    public void showBusiness2(View view) {
        Toast.makeText(CompanySet.this, "行业待开通!", Toast.LENGTH_SHORT).show();
    }

    @OnClick(R.id.business3)
    public void showBusiness3(View view) {
        Toast.makeText(CompanySet.this, "行业待开通!", Toast.LENGTH_SHORT).show();
    }

    @OnClick(R.id.business4)
    public void showBusiness4(View view) {
        Toast.makeText(CompanySet.this, "行业待开通!", Toast.LENGTH_SHORT).show();
    }

}

companyset.xml



    

        

companyitem.xml




KeyWordMatch.java

package com.example.zyk97.infotest.Utils;

import java.util.ArrayList;
import java.util.List;

public class KeyWordMatch {
    public void KeyEordMatch() {
    }

    ;

    /**
     * @param list 主串数组list
     * @param sub  字串
     * @return 返回一个匹配完成的数组list
     */
    public List getMatch(List list, String sub) {
        List matchList = new ArrayList<>();
        for (int i = 0; i 

本文样例完整代码InfoTest

你可能感兴趣的:(利用Adapter动态刷新数据实现联想词提示)