Search code examples
androidlistviewparse-platformlistview-adapter

Populating ListView with data from Parse.com failed


I used AndroidBegin guide to populate my ListView with data from Parse.com table (http://www.androidbegin.com/tutorial/android-parse-com-simple-listview-tutorial/) and it shows an empty 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);
            }
        });
    }
}


Later, I used ParseQueryAdapter tutorial, but still, it doesn't work.
Someone knows how to do it?


Solution

  • make a parse query factory

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

    make a parse query adapter

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

    done, now objects are in a list

    make sure that you have a sperate xml file for each parseobject, here i have it labaled as (R.id.post_item)