使用AsyncTask将Parse对象提取到Arraylist中

香卡南

我试图基于“ createdAt”的降序将一堆解析对象提取到一个Arraylist中。我在我的应用程序中使用片段,该片段具有一个称为“ Recents”的片段。

最初,我能够将数据存储在哈希图中,并将每个数据添加到数组列表中,并将其显示为列表视图。我可以在“我的近期”标签上看到该列表。

但是,我意识到它不应该在主线程上运行,所以我找到了异步任务,并尝试实现相同的任务。但是我无法在屏幕上查看任何Listview。

AsyncTask的代码:

    package com.astuetz.viewpager.extensions.sample;

/**
 * Created by Shankar S on 10-06-15.
 */

import android.os.AsyncTask;

import com.parse.FindCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class getBooks extends AsyncTask<String, Void, ArrayList<HashMap<String, String>>> {

    RecentsCardFragment container;
    public getBooks(RecentsCardFragment f){
        this.container = f;

}

    @Override
    protected ArrayList<HashMap<String, String>> doInBackground(String... params) {

        final ArrayList<HashMap<String, String>> book_items = new ArrayList<>();

        try {

            ParseQuery<ParseObject> query = ParseQuery.getQuery(params[0]);//params[0] refers to the class name(a String) from which
            query.orderByDescending("createdAt");                       //parse object is to be queried. In our case, it is "Posted"

            query.findInBackground(new FindCallback<ParseObject>() {
                @Override
                public void done(List<ParseObject> parseObjects, ParseException e) {

                    if (e == null) {
                        for (ParseObject book : parseObjects) {

                            HashMap<String, String> test = new HashMap<>();

                            String dept = book.getString("Department");
                            String title = book.getString("Title");
                            String author = book.getString("Author");
                            Number price_num = book.getNumber("Price");
                            String price = String.valueOf(price_num);
                            String place = book.getString("Place");
                            String desp = book.getString("Description");

                            test.put("dept", dept);
                            test.put("title", title);
                            test.put("author", author);
                            test.put("price", price);
                            test.put("place", place);
                            test.put("description", desp);

                            book_items.add(test);


                        }

                    }


                }
            });


        } catch (Exception ex) {
        }

        return book_items;
    }

    @Override
    protected void onPreExecute(){
        super.onPreExecute();

        container.showProgress();
    }


    @Override
    protected void onPostExecute(ArrayList<HashMap<String, String>> books) {
        super.onPreExecute();

      if(RecentsCardFragment.items.size()>0)
      {
          RecentsCardFragment.items.clear();
      }

     RecentsCardFragment.items.addAll(books);

        container.hideProgress();
    }


}

以及最近的片段的代码:

/*
 * Copyright (C) 2013 Andreas Stuetz <[email protected]>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.astuetz.viewpager.extensions.sample;


import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewCompat;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.FrameLayout.LayoutParams;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.ListView;
import android.os.AsyncTask;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


public class RecentsCardFragment extends Fragment
{

    ListView recentsList;
    getBooks task;
    private ProgressDialog pdia;


    /*

    array list of hashmaps that's going to hold all the data
    fetch the data from parse and load it onto this

    Each hashmap represents one post.

    */

    static ArrayList<HashMap<String,String>> items = new ArrayList<>();

   protected void startBookfetch()
   {
       task = new getBooks(this);
       task.execute("Posted");
   }





    //sample hashmaps
    HashMap<String,String> test1 = new HashMap<>();
    HashMap<String,String> test2 = new HashMap<>();

    public void showProgress()
    {

        pdia = new ProgressDialog(getActivity());
        pdia.setMessage("Loading...");
        pdia.show();


     }

    public void hideProgress(){
        pdia.dismiss();
    }

    public static RecentsCardFragment newInstance() {
        RecentsCardFragment f = new RecentsCardFragment();
        return f;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.recents_card,container,false);
        ViewCompat.setElevation(rootView,50);
        return rootView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState)
    {
        super.onActivityCreated(savedInstanceState);
        recentsList = (ListView)getActivity().findViewById(R.id.recents_list);

        startBookfetch();//Calls the async task getBooks to download books from Parse



                  RecentsAdapter adapter = new RecentsAdapter(getActivity().getApplicationContext(), items);
                  recentsList.setAdapter(adapter);


              }
    }

请帮帮我!我无法查看进度对话框或列表视图。我希望异步任务在创建活动时运行,而不是在单击任何按钮时运行。我是第一次尝试异步任务。

香卡南

我只是意识到,对于已经在后台处理的任务,我不需要异步任务。我用于从Parse中查询数据的findinBackground方法仅在后台执行该任务。因此,这绝对没有必要。我需要保持代码与以前相同。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

将Blob提取到linq对象中

来自分类Dev

将网格的记录提取到对象中

来自分类Dev

将Blob提取到linq对象中

来自分类Dev

将字典对象提取到列表中

来自分类Dev

使用 Javascript 将 JSON 对象属性提取到新数组中

来自分类Dev

如何将Firebase函数提取到Swift中的对象?

来自分类Dev

将 DataSet 的内容提取到一个对象中

来自分类Dev

使用Bash将文件内容提取到数组中

来自分类Dev

使用R将数据提取到新列中

来自分类Dev

使用VBA将文本提取到Excel表中

来自分类Dev

使用Gradle将所需的库提取到生成的Jar中

来自分类Dev

无法使用ffmpeg将字幕提取到txt文件中

来自分类Dev

使用R将数据提取到新列中

来自分类Dev

使用webpack 2将.scss文件提取到.css中

来自分类Dev

如何使用 Selenium 将数据行提取到 DataFrame 中

来自分类Dev

将对象值提取到数组中

来自分类Dev

将JSON对象中的信息提取到Javascript变量中

来自分类Dev

将JSON对象中的信息提取到Javascript变量中

来自分类Dev

使用scala play-json 2.4.x,如何将json对象的名称提取到另一个对象中?

来自分类Dev

bash:将值提取到表中

来自分类Dev

将XML数据提取到MySQL中

来自分类Dev

将游标提取到数组中

来自分类Dev

从foreach将数据提取到数组中

来自分类Dev

将多个表提取到.csv中

来自分类Dev

Perl将参数提取到hashref中

来自分类Dev

将XML提取到Excel中

来自分类Dev

将HTML表提取到R中

来自分类Dev

将JSON提取到Javascript变量中

来自分类Dev

将层次结构提取到json中