2016-01-23 46 views
22

我在實現對話框中的數據綁定時遇到了麻煩。可能嗎?如何在對話框中使用數據綁定?

下面是我的xml。

<data> 

    <variable 
     name="olaBooking" 
     type="com.example.myapp.viewmodels.ViewModel" /> 
</data> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <android.support.v7.widget.CardView 
     android:id="@+id/cv" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="15dp" 
     android:elevation="4dp" 
     android:padding="15dp"> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical"> 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:background="@color/colorPrimary" 
       android:gravity="center" 
       android:padding="15dp" 
       android:text="OLA Cab Booked !" 
       android:textAppearance="@style/TextAppearance.AppCompat.Body1" /> 

      <View 
       android:layout_width="match_parent" 
       android:layout_height="2dp" 
       android:background="@color/colorPrimaryDark" /> 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:gravity="start|center" 
       android:padding="15dp" 
       android:text="Car Details" /> 

      <View 
       android:layout_width="match_parent" 
       android:layout_height="2dp" 
       android:background="@color/colorPrimaryDark" /> 

      <TextView 
       android:id="@+id/driverName" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:padding="5dp" 
       android:text="@{olaBooking.driverName}" /> 

      <TextView 
       android:id="@+id/carModel" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:padding="5dp" 
       android:text="@{olaBooking.getCarName}" /> 

      <TextView 
       android:id="@+id/carNo" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:padding="5dp" 
       android:text="@{olaBooking.getCabNo}" /> 

      <TextView 
       android:id="@+id/eta" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:padding="5dp" 
       android:text="@{olaBooking.getEta}" /> 
     </LinearLayout> 
    </android.support.v7.widget.CardView> 
</LinearLayout> 

我要綁定在對話框上面的佈局。這怎麼可能?下面是我的Java代碼,我試過,但它不工作

 dialog.setContentView(R.layout.dialog_ola_booking_confirmed); 
    DialogOlaBookingConfirmedBinding binding = DataBindingUtil.inflate(
      LayoutInflater.from(dialog.getContext()), 
      R.layout.dialog_ola_booking_confirmed, 
      (ViewGroup) dialog.findViewById(R.id.cv), 
      false); 
    ViewModel viewModel = new ViewModel(this, event.olaBooking); 

回答

26

它可以使用數據綁定在一個對話框中,首先讓你的對話框綁定工作,你應該首先它充氣,並把它傳遞給像這樣的setContentView。

DialogOlaBookingConfirmedBinding binding = DataBindingUtil.inflate(LayoutInflater.from(getContext()), R.layout. dialog_ola_booking_confirmed, null, false); 
setContentView(binding.getRoot()); 

然後,你可以通過視圖模型:

binding.setViewModel(new ViewModel(this, event.olaBooking)); 

現在你可以看到它的工作。

+1

對話框在使用綁定進行設置時沒有正確顯示,它以正常方式工作 –

10
mBinding = DataBindingUtil.inflate(LayoutInflater.from(getContext()), R.layout.dialog_select, null, false); 
    setContentView(mBinding.getRoot()); 
    SelectDialogBean data = new SelectDialogBean(); 
    mBinding.setData(data); 
+1

應用此解決方案時,我注意到建議的'LayoutInflater.from(getContext())'在對話框中給我一個與使用數據綁定之前不同的主題。我最終使用了對話框提供的'LayoutInflater',如下所示:'dialog.getLayoutInflater()'或'LayoutInflater.from(dialog.getContext())' – splatte

7

這裏是一個數據綁定的AlertDialog一個完整的例子:)

import android.app.Dialog; 
import android.databinding.DataBindingUtil; 
import android.os.Bundle; 
import android.support.annotation.NonNull; 
import android.support.annotation.Nullable; 
import android.support.v4.app.DialogFragment; 
import android.support.v4.app.FragmentActivity; 
import android.support.v7.app.AlertDialog; 
import android.view.LayoutInflater; 


public class MyDialog extends DialogFragment { 

    private static final String KEY_MY_INFO = "KEY_MY_INFO"; 

    private String myInfo; 

    public static MyDialog newInstance(String myInfo) { 
     MyDialog dialog = new MyDialog(); 
     Bundle bundle = new Bundle(); 
     bundle.putString(KEY_MY_INFO, myInfo); 
     dialog.setArguments(bundle); 
     return dialog; 
    } 

    @Override 
    public void onCreate(@Nullable Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     myInfo = getArguments().getString(KEY_MY_INFO); 
    } 

    @NonNull 
    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     FragmentActivity activity = getActivity(); 

     MyInfoBinding binding = DataBindingUtil.inflate(LayoutInflater.from(getContext()), 
       R.layout.my_info_dialog_layout, null, false); 

     binding.setMyInfo(myInfo); 

     return new AlertDialog.Builder(activity, R.style.AppCompatAlertDialogStyle) 
       .setView(binding.getRoot()) 
       .create(); 
    } 

} 
+2

有時,人們只是在沒有上下文的情況下發布自己的答案。所以有人(例如我)可能不知道應該在哪裏放置代碼。感謝您發佈完整的解決方案:)。 –

4

你可以做同樣無需調用getRoot(。

View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_delete_confirmation, null, false); 

mBinding = DialogDeleteConfirmationBinding.bind(view); 

mBinding.setViewModel(viewModel); 

builder.setView(view); 

builder.create(); 
相關問題