gpt4 book ai didi

java - 数据绑定(bind)不是将数据与android中的xml绑定(bind)

转载 作者:行者123 更新时间:2023-12-01 21:57:53 29 4
gpt4 key购买 nike

想要实现数据绑定(bind)。我实现了代码,数据来自 API,但它没有与 xml 绑定(bind),也没有显示任何错误。所以我很困惑问题出在哪里。请帮助我。

我的代码

content_policy_info.xml

<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="policyinfo"
type="com.exlservice.lifeprov1.service.model.GetPolicyInfoResponse" />

<variable
name="surrenderquote"
type="com.exlservice.lifeprov1.service.model.SurrenderQuoteResponse" />
</data>
<android.support.constraint.ConstraintLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:id="@+id/c1"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".view.ui.PolicyInfo"
tools:showIn="@layout/app_bar_policy_info">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:background="#223d50"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/policy_prime_info_bg"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:orientation="horizontal">

<TextView
android:layout_width="0dp"
android:layout_marginLeft="5dp"
android:layout_height="wrap_content"
android:text="@{policyInfo.ProductId}"
android:textColor="#76c6db"
android:layout_weight="1"
android:typeface="serif"
android:textStyle="bold"
android:id="@+id/id_fc_first_key"/>
<TextView
android:layout_width="0dp"
android:textColor="#76c6db"
android:textStyle="bold"
android:typeface="serif"
android:layout_height="wrap_content"
android:text="@{policyInfo.Contract}"
android:layout_weight="1"
android:id="@+id/id_fc_second_key"/>

</LinearLayout>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.constraint.ConstraintLayout>
</layout>

Policyinfo.java

public class PolicyInfo extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
APIInterface apiInterface;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding= DataBindingUtil.setContentView(this,R.layout.activity_policy_info);
Intent intent = getIntent();
String sCompanyCode = intent.getStringExtra("CompanyCode");
String sPolicyNumber = intent.getStringExtra("PolicyNumber");
String sWayPoint = intent.getStringExtra("WayPoint");
Log.e("onCreate: ",sCompanyCode+" "+sPolicyNumber );
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String currentTime = sdf.format(new Date());
SurrenderQuoteInput surrenderQuoteInput = new SurrenderQuoteInput();
surrenderQuoteInput.setCoderID("TRN7");
surrenderQuoteInput.setCompanyCode("01");
PolicyDetailsReq policyDetailsReq= new PolicyDetailsReq();
policyDetailsReq.setCoderID("TRN7");

getPolicyInfoRequest.setPolicyDetailsReq(policyDetailsReq);

PolicyInfoViewModel.Factory factory = new PolicyInfoViewModel.Factory(
**getApplication(),getPolicyInfoRequest,surrenderQuoteRequest);
PolicyInfoViewModel viewModel =
ViewModelProviders.of(this,factory).get(PolicyInfoViewModel.class);
**observeViewModel**(viewModel);**

// show it
pd.show();
DrawerLayout drawer = findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();

NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);

}
private void **observeViewModel**(final PolicyInfoViewModel viewModel) {
// Update the list when the data changes
viewModel.getPolicyInfoResponsetObservable().observe(this, new Observer<GetPolicyInfoResponse>()
{
@Override
public void onChanged(@Nullable GetPolicyInfoResponse getPolicyInfoResponse) {
if(getPolicyInfoResponse!=null) {
viewModel.setPolicyInfoResult(getPolicyInfoResponse);
binding.setPolicyinfo(getPolicyInfoResponse);
}

}

});
}

此代码用于抽屉导航 Activity ,其中我的所有组件都位于 content_policy_info.xml 中。请帮帮我。

最佳答案

数据绑定(bind)是一个非常有用的工具,如果做得正确,可以节省大量工作时间。我真的推荐阅读this guide在尝试自己实现某些事情之前。这里我只是强调数据绑定(bind)设置中的主要步骤

  1. 确保在您的应用中启用数据绑定(bind):在您的应用 gradle 中添加

    dataBinding {
    enabled = true
    }

  2. 创建特定于您的 xml 文件的绑定(bind):

    ContentPolicyInfoBinding binding = DataBindingUtil.setContentView(this,R.layout.content_policy_info);

(也许您犯了一个错误,而是夸大了activity_policy_info?

  • 如果这些参数随时间变化,请将绑定(bind)参数设置为 ObservablFields 一次

    ObservablField<GetPolicyInfoResponse> policyInfoField = new ObservablField();
    ...
    binding.policyinfo = policyInfoField;

  • 更改 xml 以匹配:

    <import type="com.exlservice.lifeprov1.service.model.GetPolicyInfoResponse"/>
    <variable name="policyinfo" type="android.databinding.ObservableField&lt;GetPolicyInfoResponse&gt;"/>

  • 当新的policyInfo到达时更新policyInfoField

    policyInfoField.set(getPolicyInfoResponse)

  • <小时/>

    但更好的是使用 View 模型,将其字段设置为可绑定(bind)。这样就可以通过更改模型的字段来更新 View 。

    关于java - 数据绑定(bind)不是将数据与android中的xml绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58727985/

    29 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com