0

您好,我试图让 stackmob 向列表视图查询项目,除了出现在列表视图中的结果列表之外,一切正常,我尝试在整个互联网上进行搜索,但没有发现任何有效的结果。这是我使用 Log.i("Tagg", result.get(0).getAddress()); 时的代码 它完美地工作并显示地址,所以我不知道为什么它不会出现在列表视图中?

结果Activity.java

public class ResultsActivity extends ListActivity {

            double lon;
            double lat;
            ListAdapter adapter;
            List<RRData> RestR;
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                //setListAdapter(new ListAdapter(this, R.layout.resultslist, null));
                setListAdapter(adapter);
                Bundle extras = getIntent().getExtras();
                lon = extras.getDouble("Longitude");
                lat = extras.getDouble("Latitude");
                StackMobGeoPoint sarea = new StackMobGeoPoint(lon, lat);
                StackMobQuery q = new StackMobQuery().fieldIsNearWithinMi("location", sarea, 1110.0); //(lon, lat), distance (miles)
                RRData.query(RRData.class, q, new StackMobQueryCallback<RRData>() {
                      @Override
                      public void success(List<RRData> result) {
                          Log.i("Tagg", result.get(0).getAddress());
                          //adapter = new ListAdapter(ResultsActivity.this, R.layout.resultslist, result);
                          RestR = result;
                          ListAdapter adapter = new ListAdapter(ResultsActivity.this, R.layout.resultslist, RestR);

                        setListAdapter(adapter);
                        adapter.notifyDataSetChanged();
                      }

                      @Override
                      public void failure(StackMobException e) {
                          Log.i("TagError", e.getLocalizedMessage());
                      }
                  });
                setContentView(R.layout.activity_results);
            }

            @Override
            public boolean onCreateOptionsMenu(Menu menu) {
                // Inflate the menu; this adds items to the action bar if it is present.
                getMenuInflater().inflate(R.menu.activity_results, menu);
                return true;
            }

        }

RRData.java

public class RRData extends StackMobModel {

    private String name;
    private StackMobGeoPoint location;
    private String Address;
    private String UserName;

    public RRData(String name, StackMobGeoPoint  location,String Address) {
        super(RRData.class);
        this.name = name;
        this.location = location;
        this.Address = Address;
    }
    public String getName() {
        return name;
    }
    public String getAddress(){
        return Address;
    }
    public void setName(String name){
        this.name = name;
    }
    public void setAddress(String address){
        this.Address = address;
    }
    public void setLocation(StackMobGeoPoint location){
        this.location = location;
    }
    public StackMobGeoPoint getLocation() {
        return location;
    }
}

ListAdapter.java 公共类 ListAdapter 扩展 ArrayAdapter {

public ListAdapter(Context context, int textViewResourceId, List<RRData> tasks) {
    super(context, textViewResourceId, tasks);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    if (v == null) {
        LayoutInflater vi = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.resultslist, null);
    }
    RRData task = getItem(position);
    if (task != null) {
        ((TextView) v.findViewById(R.id.title)).setText(task.getName());
        ((TextView) v.findViewById(R.id.address)).setText(task.getAddress());
        ((TextView) v.findViewById(R.id.distance)).setText(task.getLocation().getQueryDistanceRadians().toString());
        this.notifyDataSetChanged();
    }
    return v;
}

}

4

1 回答 1

1

我为 StackMob 工作。

您的活动是 ListActivity 的子类,这要求您的布局包含一个 ID 为 @android:id/list 的 ListView。看起来您的 ListView 被称为结果列表;尝试更改名称。

有关更多信息,请查看有关 ListViews 的本教程。ListActivity 在第 3 节中介绍。

干杯,

卡尔

于 2013-04-29T18:51:25.233 回答