gpt4 book ai didi

Android DataBinding - 将 Spinner 与自定义适配器绑定(bind)

转载 作者:太空狗 更新时间:2023-10-29 15:25:26 35 4
gpt4 key购买 nike

我想将我的 Spinner 与使用的自定义适配器绑定(bind)。我需要双向绑定(bind)。我不确定如何使用自定义适配器做到这一点?

我的微调器是在 xml 中定义的:

 <Spinner
android:id="@+id/add_event_category_spinner"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:prompt="@string/spinner_category_title"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/TextView"/>

自定义适配器:

public class EventCategoryAdapter extends ArrayAdapter<Category>
{
private Context context;
private List<Category> values;

public EventCategoryAdapter (@NonNull Context context, int textViewResourceId, @NonNull List<Category> values)
{
super(context, textViewResourceId, values);
this.context = context;
this.values = values;
}

public int getCount()
{
return values.size();
}

public Category getItem(int position)
{
return values.get(position);
}

public long getItemId(int position)
{
return position;
}


@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
TextView label = new TextView(context);
label.setText(values.get(position).getName());

return label;
}

@NonNull
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent)
{
LayoutInflater vi = LayoutInflater.from(getContext());
convertView = vi.inflate(android.R.layout.simple_spinner_dropdown_item, parent, false);
CheckedTextView label = (CheckedTextView) convertView.findViewById(android.R.id.text1);

label.setText(values.get(position).getName());


return label;
}
}

因此,如您所见,适配器列表集合用于填充项目。 OnClick 监听器选择了我的 Category 对象,Spinner 仅将 Category.Name 字段显示为标签。现在我需要将微调器与数据绑定(bind)绑定(bind)以避免使用 onItemSelected 事件。相反,所选项目应该绑定(bind)并自动填充到我的 View 模型。我需要双向绑定(bind),因此更改 View 模型也应该触发微调器的集合选择。

我知道如何绑定(bind) TextView 、标签和回收器 View (用于列表),但我不确定如何为我的微调器绑定(bind)适配器。有什么建议吗?

我的尝试:

<android.support.v7.widget.AppCompatSpinner
android:id="@+id/spinner"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="7dp"
app:layout_constraintTop_toBottomOf="@+id/editText4"
android:layout_marginRight="8dp"
android:layout_marginLeft="8dp"
android:entries="@{user.categories}"
android:selectedItemPosition="@={user.selectedCategoryPosition}"/>

用户类:

@InverseBindingMethods({@InverseBindingMethod(type = AppCompatSpinner.class, attribute = "android:selectedItemPosition")})
public class User extends BaseObservable
{
public List<Category> categories;

Category category = null;

@Bindable
public Category getCategory()
{
return category;
}

public void setCategory(Category category)
{
this.category = category;
notifyPropertyChanged(BR.category);
}

Integer selectedCategoryPosition = 0;


@BindingAdapter("selectedItemPositionAttrChanged")
void setSelectedItemPositionListener(AppCompatSpinner view, final InverseBindingListener selectedItemPositionChange)
{
if (selectedItemPositionChange == null)
{
view.setOnItemSelectedListener(null);
}
else
{
view.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
selectedItemPositionChange.onChange();
}

@Override
public void onNothingSelected(AdapterView<?> parent)
{

}
});
}
}

@InverseBindingAdapter(attribute = "selectedItemPosition")
Integer getSelectedItemPosition(AppCompatSpinner spinner)
{
return spinner.getSelectedItemPosition();
}


@Bindable
public Integer getSelectedCategoryPosition()
{
return selectedCategoryPosition;
}

public void setSelectedCategoryPosition(Integer selectedCategoryPosition)
{
this.selectedCategoryPosition = selectedCategoryPosition;
notifyPropertyChanged(BR.selectedCategoryPosition);
category = categories.get(selectedCategoryPosition);
}
}

在主要 Activity 中:

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

activityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
User user = new User();
activityMainBinding.setUser(user);

现在我收到错误:

Error:Execution failed for task ':app:transformJackWithJackForDebug'.

com.android.jack.ir.JNodeInternalError: java.lang.Exception: java.lang.RuntimeException: failure, see logs for details.
@BindingAdapter on invalid element: void setSelectedItemPositionListener(android.support.v7.widget.AppCompatSpinner, android.databinding.InverseBindingListener)

最佳答案

将此添加到您的代码中

@BindingAdapter("selectedItemPosition")
void setSelectedItemPosition(AppCompatSpinner spinner,int position)
{
if(spinner.getSelectedItemPosition()!=position)
spinner.setSelection(position);
}

关于Android DataBinding - 将 Spinner 与自定义适配器绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45562170/

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