如何使用asynctask执行数据库操作来实现进度条?

时间:2016-10-17 15:58:24

标签: android sqlite android-fragments android-asynctask

我有一个带有listview的片段,并用查询设置数据。 当我在导航抽屉中选择片段时,如何使用asynctask执行数据库操作来选择然后设置数据以实现进度条?

这是我的片段:

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();
}

提前致谢。

1 个答案:

答案 0 :(得分:0)

尝试这样的事情:

public void setData(final String params){
    new AsyncTask<Void, Void, List<MyObject>>() {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // show progress bar on main thread
            showProgressBar();
        }
        @Override
        protected List<Object> doInBackground(Void... params) {
            // fetch data in background
            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 = DBHelper.getInstance(getActivity().getApplicationContext());
            SQLiteDatabase sqLiteDatabase = dbHelper.getReadableDatabase();
            Cursor cursor = sqLiteDatabase.rawQuery(query, null);
            if (cursor.getCount() > 0) {
                if (cursor.moveToFirst()) {
                    do {
                        ...
                        listMyObject.add(new MyObject(...));
                    } while (cursor.moveToNext());
                }
            }
            dbHelper.close();
            return listMyObject;
        }
        @Override
        protected void onPostExecute(List<MyObject> objects) {
            super.onPostExecute(objects);
            // set adapter and hide progress bar on main thread
            arrayAdapter = new MyAdapter(getApplication(), R.layout.listview_row, listMyObject);
            lv.setAdapter(myAdapter);
            hideProgressBar();
        }
    }.execute();
}