您好,我试图让 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;
}
}