2016-10-17 57 views
0

我有一個帶有listview的片段,並在其中用查詢設置數據。 當我在導航抽屜中選擇片段時,如何使用asynctask執行數據庫操作來選擇並設置數據以實現進度條?如何使用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(); 
} 

在此先感謝。

回答

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