使用来自Parse.com的数据填充ListView失败

时间:2015-03-16 10:16:24

标签: android listview parse-platform listview-adapter

我使用AndroidBegin指南使用来自Parse.com表(http://www.androidbegin.com/tutorial/android-parse-com-simple-listview-tutorial/)的数据填充我的ListView,它显示一个空的ListView。

// RemoteDataTask AsyncTask
private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Create a progressdialog
        mProgressDialog = new ProgressDialog(QuestionsList.this);
        // Set progressdialog title
        mProgressDialog.setTitle("Parse.com Simple ListView Tutorial");
        // Set progressdialog message
        mProgressDialog.setMessage("Loading...");
        mProgressDialog.setIndeterminate(false);
        // Show progressdialog
        mProgressDialog.show();
    }

    @Override
    protected Void doInBackground(Void... params) {
        // Locate the class table named "Country" in Parse.com
        if (AnswerActivity.friend.isEmpty()) {
            if (AnswerActivity.wantedTop == AnswerActivity.all) {
                // Locate the class table named "Info" in Parse.com
                ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Info");

                // Locate the column named "Views" in Parse.com and order list by ascending
                query.orderByDescending("Views");
                try {
                    ob = query.find();
                } catch (ParseException e) {
                    Log.e("Error", e.getMessage());
                    e.printStackTrace();
                }
                return null;
            } else {
                // Locate the class table named "Info" in Parse.com
                ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Info");

                // Search for the wanted topic
                query.whereContains("User_Topic", AnswerActivity.wantedTop);

                // Locate the column named "Views" in Parse.com and order list by ascending
                query.orderByDescending("Views");
                try {
                    ob = query.find();
                } catch (ParseException e) {
                    Log.e("Error", e.getMessage());
                    e.printStackTrace();
                }
                return null;
            }
        } else {
            if (AnswerActivity.wantedTop == AnswerActivity.all) {
                // Locate the class table named "Info" in Parse.com
                ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Info");

                // Search for the wanted topic
                query.whereContains("User_Name", AnswerActivity.friend);

                // Locate the column named "Views" in Parse.com and order list by ascending
                query.orderByDescending("Views");
                try {
                    ob = query.find();
                } catch (ParseException e) {
                    Log.e("Error", e.getMessage());
                    e.printStackTrace();
                }
                return null;
            } else {
                // Locate the class table named "Info" in Parse.com
                ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Info");

                // Search for the wanted topic
                query.whereContains("User_Topic", AnswerActivity.wantedTop);
                query.whereContains("User_Name", AnswerActivity.friend);

                // Locate the column named "Views" in Parse.com and order list by ascending
                query.orderByDescending("Views");
                try {
                    ob = query.find();
                } catch (ParseException e) {
                    Log.e("Error", e.getMessage());
                    e.printStackTrace();
                }
                return null;
             }
        }
    }

    @Override
    protected void onPostExecute(Void result) {
        // Locate the listview in listview_main.xml
        listview = (ListView) findViewById(R.id.listview);
        // Pass the results into an ArrayAdapter
        adapter = new ArrayAdapter<String>(QuestionsList.this,
                R.layout.listview_item);
        // Retrieve object "name" from Parse.com database
        for (ParseObject country : ob) {
            adapter.add((String) country.get("name"));
        }
        // Binds the Adapter to the ListView
        listview.setAdapter(adapter);
        // Close the progressdialog
        mProgressDialog.dismiss();
        // Capture button clicks on ListView items
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                // Send single item click data to SingleItemView Class
                Intent i = new Intent(QuestionsList.this,
                        SingleItemView.class);
                // Pass data "name" followed by the position
                i.putExtra("name", ob.get(position).getString("name")
                        .toString());
                // Open SingleItemView.java Activity
                startActivity(i);
            }
        });
    }
}


后来,我使用了ParseQueryAdapter教程,但仍然没有用。
有人知道怎么做吗?

2 个答案:

答案 0 :(得分:0)

你真的不需要AsyncTask ... 只需创建您的查询,调用findInBackGround()即可为您创建异步,并在回调中填充您的列表视图。

 query.findInBackground(new FindCallback<ParseObject>() {
            @Override
            public void done(List<ParseObject> results, ParseException e) {
                if (results != null && results.size() > 0) {

                    YourAdapter adapter = new YourAdapter(context, results);
                    list.setAdapter(adapter);

或者,如果你想使用ParseQueryAdapter,它甚至更简单:

// create your query before, but do no call findInBackground. 
YourParseQueryAdapter adapter = new YourParseQueryAdapter(context, new ParseQueryAdapter.QueryFactory<ParseObject>() {
            public ParseQuery<ParseObject> create() {
                return query;
            }

        });
list.setAdapter(adapter);

如有任何问题,请再次阅读文档

希望这有帮助!

答案 1 :(得分:0)

制作一个解析查询工厂

ParseQueryAdapter.QueryFactory<Post> factory;
factory = new ParseQueryAdapter.QueryFactory<Post>() {
                @Override
                public ParseQuery<Post> create() {
                    ParseQuery query = new ParseQuery("YourParseObject");
                    return query;
                }
            };

制作解析查询适配器

private ParseQueryAdapter<Post> adapter;
adapter = new ParseQueryAdapter<Post>(this, factory) {
        @Override
        public View getItemView(final Post object, View v, ViewGroup parent) {
            if (v == null) {
                v = View.inflate(getContext(), R.layout.post_item, null);
            }
            return v;
        }
    };
    adapter.setPaginationEnabled(true);
    adapter.setTextKey("title");
    adapter.setImageKey("Image");
    adapter.loadObjects();
    ListView your_list_view = (ListView) findViewById(R.id.list);
    your_list_view.setAdapter(adapter);

完成,现在对象在列表中

确保每个parseobject都有一个sperate xml文件,在这里我把它标记为(R.id.post_item)