使用解析对象填充列表视图

时间:2014-08-17 05:49:34

标签: android listview parse-platform

我的主要活动有一个按钮,可以重定向到ResterauntList活动。我想从我的Parse Cloud中获取几个对象,并且只想将名称添加到ListView。这个代码到目前为止

package com.example.gastronomaapp;

import java.util.List;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;

import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.ParseQueryAdapter;
import com.parse.ParseQueryAdapter.OnQueryLoadListener;

public class ResterauntList extends ActionBarActivity {
    String mValue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_resteraunt_list);

        Bundle bdl = getIntent().getExtras();
        mValue = bdl.getString("Value");
        setContentView(R.layout.activity_resteraunt_list);

        populateList(mValue);
    }

    private void populateList(String Value) {
        ParseQueryAdapter.QueryFactory<ParseObject> factory = new ParseQueryAdapter.QueryFactory<ParseObject>() {

            @SuppressWarnings({ "unchecked", "rawtypes" })
            public ParseQuery create() {
                ParseQuery query = new ParseQuery("Restraunt");
                query.whereEqualTo("Location", Value);
                return query;
            }
        };
        ParseQueryAdapter<ParseObject> adapter = new ParseQueryAdapter<ParseObject>(
                this, factory);
        adapter.setTextKey("name");
        adapter.addOnQueryLoadListener(new OnQueryLoadListener<ParseObject>() {
            public void onLoading() {
                // Trigger any "loading" UI
            }

            @Override
            public void onLoaded(List<ParseObject> objects, Exception e) {
                // TODO Auto-generated method stub

            }
        });

        // Attach it to your ListView, as in the example above
        ListView listView = (ListView) findViewById(R.id.restListView);
        listView.setAdapter(adapter);
    }

}

不确定什么是错的,但ListView永远不会填充。我的Parse数据浏览器声称已收到请求。检查了Logcat,它声称应用程序可能做了太多工作。不确定什么是错的。 (编辑)根据评论中的建议进行了更改。但现在列表视图有2个条目但是空。我知道有2个条目,因为它们是可点击的。完全混淆了什么是错的。编辑了代码! This is my emulator, as you can see the line there are list view entries

1 个答案:

答案 0 :(得分:0)

您忘记调用adapter.notifyDatasetChanged()方法。如果我没错,Parse查询将在后台执行,对吧?如果是这样,那么在后台线程完成时需要调用此方法。

另外,作为建议,您可以将代码简化为:

ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("ClassName");
query.whereEqualTo("KEY","VALUE");
query.findInBackground(new FindCallback<ParseObject>(){
  @Override
  public void done(List<ParseObject> dataFromServer, ParseException e){
      if( e == null ) { /** DO SOMETHING */ }
      else { /** DO SOMETHING ELSE */ }
  }
});