简体   繁体   中英

Fetch data from database to display in a listview using Parse.com

I want to display specific data in a listview from a database's column using parse.com, i just don't know how to specifically do that,following is the code there is an error here but you can change it completely to solve it !

public class Donar_Acceptor_list extends ListActivity {

// Declare Variables
ListView listview;
List<ParseObject> ob;

// ProgressDialog mProgressDialog; ArrayAdapter adapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Get the view from listview_main.xml
    setContentView(R.layout.donar_acceptor_list);
    // Execute RemoteDataTask AsyncTask
   // new RemoteDataTask().execute();
}

protected Void doInBackground(Void... params) throws com.parse.ParseException {
        // Locate the class table named "Country" in Parse.com
        ParseQuery<ParseObject> query = ParseQuery.getQuery("DonAcc");
        query.whereEqualTo("DonAcc", "donar");
       // ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
        //        "username");
        query.orderByDescending("_created_at");
        try {
            ob = query.find();
        } catch (ParseException e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return null;
    }
    protected void onPostExecute(Void result) {
        // Locate the listview in listview_main.xml
        listview = (ListView) findViewById(R.id.list);
        // Pass the results into an ArrayAdapter
        adapter = new ArrayAdapter<String>(Donar_Acceptor_list.this,
                R.layout.donar_aceptor_discription);
        // Retrieve object "name" from Parse.com database
        for (ParseObject status : ob) {
            adapter.add((String) status.get("name"));
        } 
       listview.setAdapter(adapter); } }

First you dont need to use asyncTask' doInBackground to execute your query. You can use Parse's findInBackground method to find objects asynchronously in the background without blocking your main UI thread.

Second, not sure why you use ParseQuery.getQuery("DonAcc"); , if you try to get the data from the table name "Country". So you should use ParseQuery<ParseObject> query = ParseQuery.getQuery("Country");

Third, you are using the listView as a member variable, so your class doesnt need to extends the listActivity, instead, it should just extends the regular Activity. So, you should do public class Donar_Acceptor_list extends Activity

So you can change your code to the following:

private void findName() {
       listview = (ListView) findViewById(R.id.list);

        ParseQuery<ParseObject> query = ParseQuery.getQuery("Country");
        query.whereEqualTo("DonAcc", "donar"); //assume you have a DonAcc column in your Country table
        query.orderByDescending("createdAt"); //Parse has a built createAt
        query.findInBackground(new FindCallback<ParseObject>() {
        public void done(List<ParseObject> nameList, ParseException e) {
            if (e == null) {
                ArrayList<String> mylist = new ArrayList<String>();

                for (ParseObject object: nameList) {
                    String name = object.getString("name"); //assume you have a name column in your Country table
                    myList.add(name);
                }
                adapter = new ArrayAdapter<String>(Donar_Acceptor_list.this,
                    R.layout.donar_aceptor_discription, myList);
                listview.setAdapter(adapter);
            } else {
                Log.d("error", "Error: " + e.getMessage());
            }
        }
    });

You can probably call this method in your onCreate() method. Not sure what your Donar_Acceptor_list and R.layout.donar_aceptor_discription actually are, so the above code might not compile, but the idea is to use Parse's findInBackground() method instead of using asynctask .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM