首页 > 解决方案 > ListView过滤器不适用于Android的多列

问题描述

我试图过滤我的 ListView 搜索国家名称,但由于 ListView 有多个列,它不能很好地过滤它。这是我的代码。

public class ShowGlobalStats extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{
    TextView totalCases;
    DBInterface db;
    Context appContext;
    ArrayList<Stat> arrayStats = new ArrayList<Stat>();
    ListView lv;
    SharedPreferences prefs;
    NavigationView navigationView;
    StatsAdapter inflate;

    private DrawerLayout drawer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.all_countries_stats);
        giveValues();
        Spinner spinner = findViewById(R.id.spinnerFilter);
        final EditText countryFilter = findViewById(R.id.searchBar);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.filter,
                android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        Toast.makeText(appContext, "You can click on each country to see detailed info!", Toast.LENGTH_SHORT).show();
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        drawer = findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar,
                R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();
        try {
            db.obre();
            Cursor c = db.obtainAllInformation();
            while (c.moveToNext()) {
                Stat indiStat = new Stat(c.getString(0),
                        c.getString(1),
                        c.getInt(2),
                        c.getInt(3),
                        c.getInt(4));
                arrayStats.add(indiStat);
            }
            c.close();
            inflate(arrayStats);
            selectedCountry();
        } finally {
            retrieveGlobalInfo();
            db.tanca();
        }

        countryFilter.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) {
                lv.setTextFilterEnabled(true);
                inflate.getFilter().filter(s);
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
    }

    private void giveValues()
    {
        db = new DBInterface(this);
        appContext = this;
        totalCases = findViewById(R.id.totalCases);
        prefs = getSharedPreferences("prefs", MODE_PRIVATE);
        navigationView = findViewById(R.id.navView);
        navigationView.setNavigationItemSelectedListener(this);
    }
    private void inflate(ArrayList<Stat> arrayStat) {
        inflate = new StatsAdapter(getApplicationContext(), R.layout.inflate_all_info, arrayStat);
        lv = (ListView) findViewById(R.id.listview);
        lv.setTextFilterEnabled(true);
        lv.setDivider(null);
        lv.setDividerHeight(0);
        lv.setAdapter(inflate);
    }

好像这个过滤方法只支持一列,我的ListView有4列,分别是国家代码,国家名称,病例数和死亡数。

努力在我的APP上工作以实现我的目标!

标签: javaandroidlistview

解决方案


推荐阅读