gpt4 book ai didi

android数据绑定(bind)不起作用

转载 作者:行者123 更新时间:2023-12-03 10:42:59 25 4
gpt4 key购买 nike

我是一个新手程序员。

我正在练习android数据绑定(bind)示例..

它是如何工作的,

单击 imageButton 时,用户类的“喜欢值”会增加。

但它不起作用,我不知道,

  • 原因是为什么?
  • 我该如何解决?

  • 如果将 onClickLike() 方法从主类移动到用户类并修复源,它确实工作得很好。

    User Class


    public class User {
    //
    public ObservableField<String> name = new ObservableField<>();
    public ObservableInt age = new ObservableInt();
    public ObservableInt likes = new ObservableInt();

    public User(String nameString, int ageInt) {
    name.set(nameString);
    age.set(ageInt);
    likes.set(0);
    }

    }

    MainActivity


    public class MainActivity extends AppCompatActivity {
    User user;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.activity_main);
    ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
    user = new User("john", 29);
    binding.setUser(user);

    String date = (String) DateFormat.format("yyyy/MM/dd kk:mm:ss", Calendar.getInstance());
    binding.textTime.setText(date);

    }

    public void onClickLike(View view) { <------------ doesn't work
    user.likes.set(user.likes.get() + 1);
    }

    }

    activity_main.xml



    <?xml version="1.0" encoding="utf-8"?>
    <layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.example.john.bindingexample.MainActivity">
    <data>
    <variable
    name="user"
    type="com.example.john.bindingexample.User"/>
    <variable
    name="main"
    type="com.example.john.bindingexample.MainActivity"/>
    </data>

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

    <TextView
    android:id="@+id/text_time"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="right"/>

    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    android:text="profile"
    android:textAppearance="@style/TextAppearance.AppCompat.Large"/>

    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    android:text="name:"
    android:textAppearance="@style/TextAppearance.AppCompat.Medium"/>

    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    android:text="@{user.name}"
    android:textAppearance="@style/TextAppearance.AppCompat.Medium"/>

    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    android:text="age:"
    android:textAppearance="@style/TextAppearance.AppCompat.Medium"/>

    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    android:text="@{String.valueOf(user.age)}"
    android:textAppearance="@style/TextAppearance.AppCompat.Medium"/>

    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    android:text="LIKE:"
    android:textAppearance="@style/TextAppearance.AppCompat.Medium"/>

    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    android:text="@{String.valueOf(user.likes)}"
    android:textAppearance="@style/TextAppearance.AppCompat.Medium"/>


    <ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="@{main::onClickLike}" <------------ doesn't work
    android:src="@drawable/ic_thumb_up_black_36dp"
    />

    </LinearLayout>

    </layout>

    最佳答案

    我认为您还必须将您的主要绑定(bind)与 onCreate 中的用户相同.

    binding.setMain(this);

    关于android数据绑定(bind)不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46312061/

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