【发布时间】:2016-12-27 14:01:33
【问题描述】:
我正在尝试使用抽屉菜单制作一个应用程序,其中一个选项是使用选项卡打开一个 Activity。在每个片段中,我都试图显示在数据库上运行的列表视图。我在主线程上工作太多有问题。跳过 66 帧或更多帧。我应该在 Async Task 中进行此操作吗?请帮助找到我的解决方案我该怎么办?
public class test1 extends Fragment{
private OpenHelper1 dbHelper;
private SimpleCursorAdapter dataAdapter;
public ListView listView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.trening_activity_main, container, false);
dbHelper = new OpenHelper1(this.getActivity());
dbHelper.open();
//Clean all data
dbHelper.deleteAllCountries();
//Add some data
dbHelper.insertSomeCountries();
//Generate ListView from SQLite Database
displayListView(rootView);
return rootView;
}
private void displayListView(View root) {
Cursor cursor = dbHelper.fetchAllCountries();
// The desired columns to be bound
String[] columns = new String[]{
OpenHelper1.KEY_NAME,
OpenHelper1.KEY_KIND
};
// the XML defined views which the data will be bound to
int[] to = new int[]{
R.id.name,
R.id.kind,
};
// create the adapter using the cursor pointing to the desired data
//as well as the layout information
dataAdapter = new SimpleCursorAdapter(
getActivity(), R.layout.trening_activity1,
cursor,
columns,
to,
0);
listView = (ListView) root.findViewById(R.id.listView1);
// Assign adapter to ListView
listView.setAdapter(dataAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> listView, View view,
int position, long id) {
// Get the cursor, positioned to the corresponding row in the result set
Cursor cursor = (Cursor) listView.getItemAtPosition(position);
// Get the state's capital from this row in the database.
String countryCode =
cursor.getString(cursor.getColumnIndexOrThrow("name"));
Toast.makeText(getActivity().getApplicationContext(),
countryCode, Toast.LENGTH_SHORT).show();
// Launching new Activity on selecting single List Item
Intent i = new Intent(getActivity().getApplicationContext(), SingleListItem.class);
// sending data to new activity
i.putExtra("cwiczenie ", countryCode);
startActivity(i);
}
});
EditText myFilter = (EditText) root.findViewById(R.id.myFilter);
myFilter.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
public void onTextChanged(CharSequence s, int start,
int before, int count) {
dataAdapter.getFilter().filter(s.toString());
}
});
dataAdapter.setFilterQueryProvider(new FilterQueryProvider() {
public Cursor runQuery(CharSequence constraint) {
return dbHelper.fetchCountriesByName(constraint.toString());
}
});}
}
【问题讨论】:
-
是的,数据库操作应该是 AsyncTask(s)。这是一个很好的起点:developer.android.com/reference/android/os/AsyncTask.html
-
此链接将帮助您在 asynctask 中使用数据库。 [stackoverflow.com/questions/16293022/…
-
在 onCreateView() 中对数据(base)做任何事情是个坏主意。将其移至 onViewCreated()
-
谢谢大家,我会检查一切
标签: java android database multithreading listview