我正在尝试根据“createdAt”的降序将一堆解析对象提取到Arraylist中。我在我的应用程序中使用片段,其中有一个名为“最近”的片段。
最初,我能够将数据存储在散列映射中,并将它们中的每一个添加到数组列表中,并将它们显示为列表视图。我能够在我的最近标签上看到该列表。
但是,我意识到它不应该在主线程上运行,所以我找到了异步任务,我试图实现相同的。但是我无法在屏幕上查看任何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();
}
}
和RecentsFragment的代码:
/*
* Copyright (C) 2013 Andreas Stuetz <andreas.stuetz@gmail.com>
*
* 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);
}
}
请帮帮我!我无法查看进度对话框或列表视图。 我希望在创建活动时运行异步任务,而不是在任何按钮单击时运行。 我是第一次尝试异步任务。
答案 0 :(得分:0)
我刚刚意识到我不需要为已经在后台处理的任务执行异步任务。我用来从Parse查询数据的findinBackground方法只在后台执行任务。所以,这绝对没有必要。 我需要保持代码与之前相同。