简体   繁体   English

使用Parse.com从数据库中获取数据以显示在列表视图中

[英]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 ! 我想使用parse.com在数据库列的列表视图中显示特定数据,我只是不知道具体如何执行,下面的代码在这里有错误,但是您可以完全更改它来解决它!

public class Donar_Acceptor_list extends ListActivity { 公共类Donar_Acceptor_list扩展了ListActivity {

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

// ProgressDialog mProgressDialog; // ProgressDialog mProgressDialog; ArrayAdapter adapter; ArrayAdapter适配器;

@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. 首先,您不需要使用asyncTask的doInBackground执行查询。 You can use Parse's findInBackground method to find objects asynchronously in the background without blocking your main UI thread. 您可以使用Parse的findInBackground方法在后台异步查找对象,而不会阻塞您的主UI线程。

Second, not sure why you use ParseQuery.getQuery("DonAcc"); 其次,不确定为什么要使用ParseQuery.getQuery("DonAcc"); , if you try to get the data from the table name "Country". ,如果您尝试从表名“ Country”中获取数据。 So you should use ParseQuery<ParseObject> query = ParseQuery.getQuery("Country"); 因此,您应该使用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. 第三,您将listView用作成员变量,因此您的类不需要扩展listActivity,而应该扩展常规的Activity。 So, you should do public class Donar_Acceptor_list extends Activity 所以,你应该做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. 您可能可以在onCreate()方法中调用此方法。 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 . 不确定您的Donar_Acceptor_listR.layout.donar_aceptor_discription实际是什么,因此上面的代码可能无法编译,但是其想法是使用Parse的findInBackground()方法而不是使用asynctask

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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