使用回调后无法更新Gridview

时间:2016-02-29 16:01:32

标签: android gridview

在我的Android应用程序中,我使用app42后端作为服务。当我使用API​​调用查询数据库时,我在postExecute方法中的异步调用之后设置适配器,但gridview没有更新

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

 rootView = inflater.inflate(R.layout.fragment_primary, null);

 FetchUserList fetchUserList = new FetchUserList();
 fetchUserList.execute();

 return rootView;
}

public class FetchUserList extends AsyncTask < Void, Void, List < CardObject >> {

 private final String LOG_TAG = FetchUserList.class.getSimpleName();

 @Override
 protected List < CardObject > doInBackground(Void...params) {

  mItems = new ArrayList < CardObject > ();
  String dbName = "test";
  String collectionName = "Testing";

  StorageService storageService = App42API.buildStorageService();
  storageService.findAllDocuments(dbName, collectionName, new App42CallBack() {
   public void onSuccess(Object response) {
    Storage storage = (Storage) response;
    System.out.println("dbName is " + storage.getDbName());
    System.out.println("collection Name is " + storage.getCollectionName());
    ArrayList < Storage.JSONDocument > jsonDocList = storage.getJsonDocList();

    for (int i = 0; i < jsonDocList.size(); i++) {

     Stest = jsonDocList.get(i).getJsonDoc();

     if (Stest != null) {
      try {
       JSONObject jsonObj = new JSONObject(Stest);
       Stest_1 = jsonObj.getString("name");
       System.out.println("Complete is " + Stest_1);
       mItems.add(new CardObject(Stest_1, R.drawable.messages, Stest));
      } catch (JSONException e) {
       e.printStackTrace();
      }
     }
    }
   }

   public void onException(Exception ex) {
    System.out.println("Exception Message" + ex.getMessage());
   }
  });
  return mItems;
 }

 @Override
 protected void onPostExecute(List < CardObject > mItems) {
  super.onPostExecute(mItems);
  mAdapter = new GridViewAdapter(getActivity(), mItems);
  GridView gridView = (GridView) rootView.findViewById(R.id.gridview);
  gridView.setAdapter(mAdapter);
 }

}

0 个答案:

没有答案