我是Android新手,我正在寻找在ListView中加载大量数据然后使用SearchView过滤它们的最佳方法。 我从数据库中获取数据,查询返回大约25000条记录。 这是我能够实现的解决方案,但不确定它是最好的(当然不是最快的!):
这是片段:
public class FragmentOne extends Fragment {
ListView lv;
MyAdapter myAdapter;
SearchView sv;
ArrayList<MyObject> listMyObject;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_one, container, false);
return view;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
sv = (SearchView) getView().findViewById(R.id.searchView);
lv = (ListView) getView().findViewById(R.id.listView);
setData("");
sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
setData(newText);
return false;
}
});
}
public void setData(String params) {
String query = "select *, count(*) as num from table where field_1 like '" + params + "%' group by field_1 order by field_1";
listMyObject= new ArrayList<>();
DBHelper dbHelper;
SQLiteDatabase sqLiteDatabase;
String ... ;
int ...;
dbHelper = DBHelper.getInstance(getActivity().getApplicationContext());
sqLiteDatabase = dbHelper.getReadableDatabase();
Cursor cursor = sqLiteDatabase.rawQuery(query, null);
if (cursor.getCount() > 0) {
if (cursor.moveToFirst()) {
do {
...
listMyObject.add(new MyObject(...));
} while (cursor.moveToNext());
}
}
arrayAdapter = new MyAdapter(getActivity(), R.layout.listview_row, listMyObject);
lv.setAdapter(myAdapter);
dbHelper.close();
}
因为这个解决方案有点'慢(拥有大量数据),我该如何加速片段的加载?
答案 0 :(得分:0)
首先检查可用的SQLite / ORM库b'z他们已经实现了许多功能,如缓存db或反应数据库等,并根据您的要求选择。然后尝试在SQL查询中使用分页加载数据。