java - 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上工作以实现我的目标!
解决方案
推荐阅读
- twitter-bootstrap - 之间的区别:输入组前置和附加?(BS4)
- c# - 如何冻结集合以便可以迭代它?
- c - 避免无限的while循环
- mysql - SQL WHERE 过滤
- java - 获取活动 system.out 打印流列表
- angular - 在 Angular 5 中动态添加元描述
- vbscript - 在 vbScript 中安排要在(任何用户)登录时执行的任务
- r - 警告:aggregate.data.frame 中的错误:参数必须具有相同的长度堆栈跟踪(最里面的第一个)
- angularjs - 找不到我的退货被序列化两次的原因
- java - 我的类实现了一个接口,但没有覆盖它的抽象方法。为什么 Eclipse 仍然可以毫无问题地编译它?