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

Posted

技术标签:

【中文标题】如何使用 asynctask 执行数据库操作来实现进度条?【英文标题】:How I can perform database operations using asynctask to implement a progress bar? 【发布时间】:2017-02-26 16:48:07 【问题描述】:

我有一个带有列表视图的片段,并通过查询在其中设置数据。 当我在导航抽屉中选择片段时,如何执行数据库操作以选择然后使用 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】:

试试这样的:

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

【讨论】:

以上是关于如何使用 asynctask 执行数据库操作来实现进度条?的主要内容,如果未能解决你的问题,请参考以下文章

安卓Android开发:使用Executor线程池代替AsyncTask进行异步操作

安卓Android开发:使用Executor线程池代替AsyncTask进行异步操作

等待多个AsyncTask完成

使用 AsyncTask 从 JSON 中获取数据

android asynctask怎么用

AsyncTask和Handler两种异步方式的实现和区别比较