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

时间:2014-11-12 18:43:52

标签: java android database listview parse-platform

我想使用parse.com从数据库列中的列表视图中显示特定数据,我只是不知道具体如何做到这一点,以下代码中存在错误但是你可以完全改变它来解决它!

公共类Donar_Acceptor_list扩展了ListActivity {

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

// ProgressDialog mProgressDialog;     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); } }

1 个答案:

答案 0 :(得分:1)

首先,您不需要使用asyncTask'doInBackground来执行查询。您可以使用Parse的findInBackground方法在后台异步查找对象,而不会阻止您的主UI线程。

其次,如果您尝试从表名“Country”获取数据,则不确定为何使用ParseQuery.getQuery("DonAcc");。所以你应该使用ParseQuery<ParseObject> query = ParseQuery.getQuery("Country");

第三,您使用listView作为成员变量,因此您的类不需要扩展listActivity,而应该只扩展常规Activity。所以,你应该做public class Donar_Acceptor_list extends Activity

因此,您可以将代码更改为以下内容:

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

您可以在onCreate()方法中调用此方法。不确定Donar_Acceptor_listR.layout.donar_aceptor_discription实际上是什么,因此上面的代码可能无法编译,但我们的想法是使用Parse的findInBackground()方法而不是asynctask