【问题标题】:Android The application may be doing too much work on its main threadAndroid 应用程序可能在其主线程上做了太多工作
【发布时间】: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());
        }
    });}

}

【问题讨论】:

标签: java android database multithreading listview


【解决方案1】:

您不能在主线程(或 UI 线程)中进行数据库操作。您必须使用 AsyncTask 在另一个线程中执行此操作。所以你自己已经有了答案。

【讨论】:

    【解决方案2】:

    确切地说,您应该使用LoaderCursorLoader。这将在后台线程中从数据库中获取数据。回调方法 onLoadFinished 将提供光标,然后您可以使用该光标来创建/更新您的 SimpleCursorAdapter

    查看example on the Developer site

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多