0

我想實現與處理程序的TextView中的數量的增加和減少,但總是會拋出相同的錯誤善意幫助我瞭解我缺乏獲得預期的結果。綁定按鈕與增量和減量處理程序在Android數據綁定

錯誤:執行任務':app:compileDebugJavaWithJavac'失敗。

java.lang.RuntimeException: Found data binding errors. ****/ data binding error ****msg:Identifiers must have user defined types from the XML file. view is missing it file:C:\Users\Tushar Rai\Desktop\Demo\Demo\app\src\main\res\layout\fragment_list.xml ****\ data binding error ****

<layout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
xmlns:app="http://schemas.android.com/apk/res-auto"> 

<data> 

    <variable 
     name="quantity" 
     type="int"/> 

    <variable 
     name="Handlers" 
     type="com.demo.www.demo.ui.Fragment.Handlers"/> 

    <import type="android.view.View"/> 
</data> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:layout_alignParentEnd="true" 
     android:paddingTop="16dp" 
     android:paddingBottom="16dp"> 


     <Button 
      style="?android:attr/buttonStyleSmall" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/decrease" 
      android:onClick="@{Handlers.decrement(view, 0)}" /> 

     <TextView 
      android:layout_width="32dp" 
      android:layout_height="wrap_content" 
      app:quantity="@{quantity}" 
      android:textAlignment="center" /> 

     <Button 
      style="?android:attr/buttonStyleSmall" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/increase" 
      android:onClick="@{Handlers.increment(view, 10)}" /> 

    </LinearLayout> 

公共類片段擴展片段{

FirebaseRecyclerAdapter adapter; 
Firebase mFirebaseRef = new Firebase("https://demo.firebaseio.com/").child("list"); 

public Fragment() { 
    // Required empty public constructor 
} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    final View rootView = inflater.inflate(R.layout.recycler_view, container, false); 
    final RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view); 

    adapter = new FirebaseRecyclerAdapter<List, ViewHolder>(List.class, R.layout.fragment_list, 
      ViewHolder.class, mFirebaseRef) { 
     @Override 
     protected void populateViewHolder(ViewHolder viewHolder, List list, int i) { 

      FragmentBinding binding = viewHolder.getBinding(); 
      binding.setList(list); 
      Handlers handlers = new Handlers(); 
      binding.setHandlers(handlers); 


     } 
    }; 
    recyclerView.setAdapter(adapter); 
    recyclerView.setHasFixedSize(true); 
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); 
    recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), null)); 
    return rootView; 
} 

public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { 

    public FragmentBinding binding; 

    public ViewHolder(View itemView) { 
     super(itemView); 
     binding = DataBindingUtil.bind(itemView); 
     itemView.setOnClickListener(this); 
    } 

    public FragmentBinding getBinding() { 
     return binding; 
    } 
    @Override 
    public void onClick(View v) { 


    } 
} 
@BindingAdapter("imageUrl") 
public static void setImage(ImageView imageView, String imageUrl) { 
    Picasso.with(imageView.getContext()).load(imageUrl).into(imageView); 
} 
@BindingAdapter("quantity") 
public static void setQuantityText(TextView view, int quantity) { 
    view.setText(String.valueOf(quantity)); 
} 
public class Handlers { 

    public void decrement(View view, int min) { 
     FragmentBinding binding = DataBindingUtil.findBinding(view); 
     binding.setQuantity(Math.min(min, binding.getQuantity() - 1)); 
    } 
    public void increment(View view, int max) { 
     FragmentBinding binding = DataBindingUtil.findBinding(view); 
     binding.setQuantity(Math.max(max, binding.getQuantity() + 1)); 
    } 

} 

}

回答

0

我認爲問題出在你的遞增遞減&方法。你不能重載這些方法參數。

你需要寫,如:

public class Handlers { 

public void decrement(View view) { 
    FragmentBinding binding = DataBindingUtil.findBinding(view); 
    binding.setQuantity(Math.min(min, binding.getQuantity() - 1)); 
} 
public void increment(View view) { 
    FragmentBinding binding = DataBindingUtil.findBinding(view); 
    binding.setQuantity(Math.max(max, binding.getQuantity() + 1)); 
} 

}

以XML:

android:onClick="@{Handlers.increment}" 

看到該鏈接的更多:DataBinding

+0

我做到了,但它也沒有工作,我正在從一個解決方案提到我的另一個問題,看看http://stackoverflow.c om/questions/37066497/layout-binding-integer-value-limit-from-0-to-10-in-android-with-databinding –

+0

你是否試過通過清理你的項目?一些DataBinding錯誤顯示沒有理由,這是通過清理項目自動修復的 – Sayem

+0

是清理過的,甚至使緩存也失效了幾次,但沒有任何反應 –