Android 如何在导航抽屉活动中进行双向数据绑定?
我正在项目中实现双向数据绑定,但我无法在导航抽屉活动中进行数据绑定。我试过了,但出了一个错误。请提供一些相同的示例代码 content\u policy\u info.xmlAndroid 如何在导航抽屉活动中进行双向数据绑定?,android,android-databinding,Android,Android Databinding,我正在项目中实现双向数据绑定,但我无法在导航抽屉活动中进行数据绑定。我试过了,但出了一个错误。请提供一些相同的示例代码 content\u policy\u info.xml <layout xmlns:android="http://schemas.android.com/apk/res/android"> <data> <variable name="policyinfo" type="c
<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);
pd = new ProgressDialog(PolicyInfo.this);
//setContentView(R.layout.activity_policy_info);
pref= getSharedPreferences("LoginDetail", 0);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Poilcy Info");
apiInterface = APIClient.getClient().create(APIInterface.class);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
apiInterface = APIClient.getClient().create(APIInterface.class);
PolicyInfoViewModel viewModel =
ViewModelProviders.of(this,factory).get(PolicyInfoViewModel.class);
**observeViewModel**(viewModel);
pd.setTitle("Loading...");
pd.setMessage("Please wait.");
pd.setCancelable(false);
// 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);
}
/* if (getLastAccessedDetailsResult != null) {
getLastAccessedDetailsRes=getLastAccessedDetailsResult;
viewModel.setLastAccessedDetailsResult(getLastAccessedDetailsResult);
setupViewPager(viewPager);
}*/
}
});
}
public class PolicyInfo扩展了AppCompative活动
实现NavigationView.OnNavigationItemSelectedListener{
api接口api接口;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
binding=DataBindingUtil.setContentView(this,R.layout.activity\u policy\u info);
Intent=getIntent();
字符串sCompanyCode=intent.getStringExtra(“公司代码”);
String sPolicyNumber=intent.getStringExtra(“PolicyNumber”);
String sweepoint=intent.getStringExtra(“航路点”);
Log.e(“onCreate:,sCompanyCode+”+sPolicyNumber);
SimpleDataFormat sdf=新的SimpleDataFormat(“yyyyMMdd”);
字符串currentTime=sdf.format(new Date());
SurrenderQuoteInput SurrenderQuoteInput=新的SurrenderQuoteInput();
提交QuoteInput.setCoderID(“TRN7”);
放弃QuoteInput.setCompanyCode(“01”);
PolicyDetailsReq PolicyDetailsReq=新的PolicyDetailsReq();
保单详情请参见setCoderID(“TRN7”);
getPolicyInfoRequest.setPolicyDetailsReq(policyDetailsReq);
PolicyInfoViewModel.Factory工厂=新的PolicyInfoViewModel.Factory(
getApplication()、getPolicyInfoRequest、SubunderQuoteRequest);
pd=新建进度对话框(PolicyInfo.this);
//setContentView(R.layout.activity\u policy\u info);
pref=getSharedReferences(“LoginDetail”,0);
Toolbar Toolbar=findviewbyd(R.id.Toolbar);
设置支持操作栏(工具栏);
getSupportActionBar().setTitle(“POICY信息”);
apinterface=APIClient.getClient().create(apinterface.class);
FloatingActionButton fab=findViewById(R.id.fab);
fab.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
Snackbar.make(查看“替换为您自己的操作”,Snackbar.LENGTH\u LONG)
.setAction(“Action”,null).show();
}
});
apinterface=APIClient.getClient().create(apinterface.class);
PolicyInfoViewModel视图模型=
ViewModelProviders.of(this,factory.get)(PolicyInfoViewModel.class);
**ObserveView模型**(viewModel);
pd.设置标题(“加载…”);
设置消息(“请稍候”);
pd.可设置可取消(假);
//表现出来
pd.show();
抽屉布局抽屉=findViewById(R.id.抽屉布局);
ActionBarDrawerToggle切换=新建ActionBarDrawerToggle(
这,抽屉,工具栏,R.string.navigation\u drawer\u open,R.string.navigation\u drawer\u close);
抽屉。添加抽屉链接器(切换);
toggle.syncState();
NavigationView NavigationView=findViewById(R.id.nav_视图);
navigationView.setNavigationItemSelectedListener(此);
}
私有void**observeViewModel**(最终保单信息视图模型视图模型){
//数据更改时更新列表
viewModel.GetPolicyInfoResponseToObservable().observe(这是新的观察者(){
@凌驾
更改后的公共void(@Nullable GetPolicyInfoResponse GetPolicyInfoResponse){
if(getPolicyInfoResponse!=null){
setPolicyInfoResult(getPolicyInfoResponse);
binding.setPolicyinfo(getPolicyInfoResponse);
}
/*if(getLastAccessedDetailsResult!=null){
getLastAccessedDetailsResult=getLastAccessedDetailsResult;
viewModel.setLastAccessedDetailsResult(getLastAccessedDetailsResult);
设置viewPager(viewPager);
}*/
}
});
}
上面显示类强制转换异常的错误
无法将android.support.v7.widget.AppCompatTextView强制转换为android.support.design.widget.NavigationView
完整堆栈跟踪
2019-11-05 16:28:27.673 3019-3019/com.exlservice.lifeprov1 E/AndroidRuntime:致命异常:main
进程:com.exlservice.lifeprov1,PID:3019
java.lang.RuntimeException:无法启动活动组件信息{com.exlservice.lifeprov1/com.exlservice.lifeprov1.view.ui.PolicyInfo}:java.lang.ClassCastException:android.support.v7.widget.AppCompatTextView无法转换为android.support.design.widget.NavigationView
在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)上
位于android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
位于android.app.ActivityThread.-wrap11(未知来源:0)
在android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
位于android.os.Handler.dispatchMessage(Handler.java:105)
位于android.os.Looper.loop(Looper.java:164)
位于android.app.ActivityThread.main(ActivityThread.java:6541)
位于java.lang.reflect.Method.invoke(本机方法)
在com.android.internal.os.Zygote$MethodAndArgsCaller.run上(Zygote.java:240)
位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
原因:java.lang.ClassCastException:android.support.v7.widget.AppCompatTextView无法强制转换为android.support.design.widget.NavigationView
位于com.exlservice.lifeprov1.databinding.ActivityPolicyInfoBindingImpl.(ActivityPolicyInfoBindingImpl.java:30)
位于com.exlservice.lifeprov1.databinding.ActivityPolicyInfoBindingImpl.(ActivityPolicyInfoBindingImpl.java:27)
位于com.exlservice.lifeprov1.DataBinderMapperImpl.getDataBinder(DataBinderMapperImpl.java:56)
位于android.databinding.MergedDataBinderMapper.getDataBinder(MergedDataBinderMapper.java:74)
位于android.databinding.databindingtul.bind(databindingtul.java:199)
在android.databinding.DataBindingUtil.bindToAddedViews
NavigationView navigationView = findViewById(R.id.nav_view);