【问题标题】:Changes in design not reflected on activity [duplicate]设计变更未反映在活动中[重复]
【发布时间】:2018-05-14 22:35:55
【问题描述】:

我正在尝试在我的 Activity 上显示来自我的 DataObject 的数据。一切正常,不会在任何地方崩溃,但我的视图没有随信息更新。 我是android的初学者,我知道这个...... 请问有人可以帮我吗?谢谢

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);

        LayoutInflater inflater = LayoutInflater.from(DetailActivity.this);
        vp = (RelativeLayout)inflater.inflate(R.layout.activity_detail, null);

        String object_id = getIntent().getStringExtra("getIn"); // Get object_id from Intent

        DataQuery query = DataQuery.get("Id");
        query.getInBackground(object_id, new GetCallback<DataObject>() {
            @Override
            public void done(DataObject object, DataException e) {
                if (e == null) {
                        TextView price = (TextView)vp.findViewById(R.id.priceD);
                        price.setText((String) object.get("price"));


                        TextView productD = (TextView)vp.findViewById(R.id.productD);
                        productD.setText((String) object.get("product"));

                        ImageView  thumbnail=  (ImageView)vp.findViewById(R.id.thumbnail2);
                        thumbnail.setImageBitmap((Bitmap) object.get("image"));

                        TextView descriptionD = (TextView)vp.findViewById(R.id.description );
                        descriptionD.setText((String) object.get("description"));


                  //  }

                } else {
                    // Error

                }
            }
        });

【问题讨论】:

  • 你确定,e 不为空并且调用了 done 方法吗?尝试使用 logcat 获取结果。
  • 我确定,因为我调试应用程序,我看到每个变量的数据,但我看到,在方法集文本中,但活动不刷新...跨度>

标签: android android-activity background


【解决方案1】:

当您将 setContent 设置为 Activity 时,为什么还要再次膨胀相同的 XML? 删除这些行:

 LayoutInflater inflater = LayoutInflater.from(DetailActivity.this);
    vp = (RelativeLayout)inflater.inflate(R.layout.activity_detail, null);

在查找视图时也删除它的引用。只需使用:

TextView price = (TextView)findViewById(R.id.priceD);

目前,您在后台方法中的更改会更改膨胀视图内的视图,而不是 Activity 的视图,因此更改不可见。去掉上面的代码就可以正常工作了。

【讨论】:

  • 非常感谢,这是我在android中的第一个应用......
  • 嘿 Miki,我看到您从问题中编辑并删除了您的代码。但是我提供的解决方案不正确吗?
  • 是的,但是堆栈溢出的经理阻止了我,因为我的问题......
【解决方案2】:

你可以简单地使用:

finish();
startActivity(getIntent());

从内部刷新Activity

【讨论】:

  • 它finist但是当我startactivt它再次启动并进入一个循环
  • 最简单的方法是调用onCreate(null);你的活动就会像新的一样。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-10-13
  • 1970-01-01
  • 1970-01-01
  • 2014-04-27
  • 2021-06-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多