私はドロワーメニューでアプリケーションを作成しようとしています。オプションの1つは、タブでアクティビティーを開くことです。どのフラグメントでも、データベース上でどのoparateのリストビューを表示しようとしています。私はメインスレッドであまりにも多くの作業に問題があります。 66フレーム以上はスキップされます。私は非同期タスクでこの操作を行う必要がありますか?私の解決策を見つけるのを手伝ってください。どうすればいいですか?Andoidメインスレッドでアプリケーションが多すぎる可能性があります
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)でなければなりません。 https://developer.android.com/reference/android/os/AsyncTask.html – Flummox
このリンクは、asynctaskでデータベースを使用するのに役立ちます。 [http://stackoverflow.com/questions/16293022/how-to-perform-database-operations-using-async-task][1] –
onCreateView()でデータを使って何かを行うのは悪い考えです。 onViewCreated()に移動してください – slanecek