在TextView中显示解析对象数据

时间:2014-09-01 15:10:23

标签: android parse-platform textview

obj是一个字符串,其中包含我的解析数据库中特定对象的Id。我想获取单个行值并在文本视图中显示它们。以下代码就是我所拥有的。不知道为什么,但查询alwasy似乎返回空。因此我的字符串restName,restCuisine等都只有它们的初始化值,即它们的值因为我的查询而没有改变。任何帮助将不胜感激

public class SingleRestraunt extends ActionBarActivity {
    private GoogleMap map;
    TextView resteName, resteCuisine, resteLocation, resteAddress;
    String restName = "nothing", obj, restCuisine = "nothing",
            restLocation = "nothing", restAddress = "nothing";
    Double Lang = 19.144378, Long = 72.837135;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_single_restraunt);
        resteName = (TextView) findViewById(R.id.restrauntName);
        resteCuisine = (TextView) findViewById(R.id.restrauntCuisine);
        resteLocation = (TextView) findViewById(R.id.restrauntLocation);
        resteAddress = (TextView) findViewById(R.id.restrauntAddress);
        Intent i = getIntent();
        obj = i.getStringExtra("restId");
        getDetails(obj);

    }

    private void getDetails(final String obj) {

        ParseQuery<ParseObject> query = ParseQuery.getQuery("resdb");
        query.getInBackground(obj, new GetCallback<ParseObject>() {
            @Override
            public void done(ParseObject object, ParseException e) {
                if (e == null) {
                    restName = object.getString("name");
                    restCuisine = object.getString("cuisine");
                    restLocation = object.getString("location");
                    restAddress = object.getString("address");
                } else {
                    e.printStackTrace();
                }
            }
        });
        prepareMap(Lang, Long);
        addData();

    }

    public void addData() {

        resteName.setText(restName);
        resteCuisine.setText(restCuisine);
        resteLocation.setText(restLocation);
        resteAddress.setText(restAddress);
    }

    public void prepareMap(Double Lang, Double Long) {
        final LatLng REST = new LatLng(Lang, Long);
        map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
                .getMap();
        @SuppressWarnings("unused")
        Marker hamburg = map.addMarker(new MarkerOptions().position(REST)
                .title("Here"));
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(REST, 15));
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

1 个答案:

答案 0 :(得分:0)

我认为在电话prepareMap(Lang, Long)之后拨打addData()query.getInBackground(...)是不正确的,因为&#34;在后台&#34;意味着在不同的线程中。你应该重新排序你的电话:

ParseQuery<ParseObject> query = ParseQuery.getQuery("resdb");
    query.getInBackground(obj, new GetCallback<ParseObject>() {
        @Override
        public void done(ParseObject object, ParseException e) {
            if (e == null) {
                restName = object.getString("name");
                restCuisine = object.getString("cuisine");
                restLocation = object.getString("location");
                restAddress = object.getString("address");
                // Update your info after to get the rest info
                prepareMap(Lang, Long);
                addData();
            } else {
                e.printStackTrace();
            }
        }
    });

我希望这能帮到你!