我见过以下链接,仍无法找到我做错的事。
参考文献:
情境:
我是用于数据绑定的buildng演示,在textview中使用简单文本成功完成,现在我想使用picasso将url传递给imageview。
Student.java
package model;
import android.databinding.Bindable;
import android.databinding.BaseObservable;
import android.databinding.BindingAdapter;
import android.widget.ImageView;
import com.example.test.example_databinding.R;
import com.squareup.picasso.Picasso;
/**
* Created by test on 07/20/16.
*/
public class Student extends BaseObservable{
String name;
String surname;
String id;
String profile_pic;
boolean is_online;
public Student(String name, String surname, String id, String profile_pic, boolean is_online) {
this.name = name;
this.surname = surname;
this.id = id;
this.profile_pic = profile_pic;
this.is_online = is_online;
}
@Bindable
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
notifyPropertyChanged(com.example.test.example_databinding.BR.name);
}
@Bindable
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
notifyPropertyChanged(com.example.test.example_databinding.BR.surname);
}
@Bindable
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
notifyPropertyChanged(com.example.test.example_databinding.BR.id);
}
@Bindable
public boolean getis_online() {
return is_online;
}
public void setIs_online(boolean is_online) {
this.is_online = is_online;
notifyPropertyChanged(com.example.test.example_databinding.BR.is_online);
}
@Bindable
public String getProfile_pic() {
return profile_pic;
}
public void setProfile_pic(String profile_pic) {
this.profile_pic = profile_pic;
notifyPropertyChanged(com.example.test.example_databinding.BR.profile_pic);
}
}
activity_demo_1.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".DemoActivity">
<data class="DemoActivitynew">
<variable
name="student"
type="model.Student" />
</data>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<ImageView
android:id="@+id/imageview"
android:layout_width="200dp"
android:layout_height="200dp"
app:profile_pic="@{student.profile_pic}"/>
<TextView
android:id="@+id/txt_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/imageview"
android:layout_alignParentTop="true"
android:text="@{student.id}" />
<TextView
android:id="@+id/txt_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/txt_id"
android:text="@{student.name}" />
<TextView
android:id="@+id/txt_surname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/txt_name"
android:text="@{student.surname}" />
<Button
android:id="@+id/btn_update_data"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Update data"
android:layout_above="@+id/btn_open_recyclerview"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:id="@+id/btn_open_recyclerview"
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Open recyclerview"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
BindingAdapters.java
public class BindingAdapters {
@BindingAdapter("app:profile_pic")
public static void loadImage(ImageView imageView, String profile_pic) {
Picasso.with(imageView.getContext()).load(profile_pic).into(imageView);
}
}
DemoActivity.java
public class DemoActivity extends Activity {
Student student;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DemoActivitynew binding = DataBindingUtil.setContentView(this, R.layout.activity_demo_1);
student = new Student("Smit","satodia","1","",false);
binding.setStudent(student);
binding.btnUpdateData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
student.setName("Android_smit");
student.setSurname("Android_satodia");
student.setProfile_pic("http://fillmurray.com/400/400");
}
});
}
}
注意:由于其他部分正在运作,请仅关注imagview部分,我发布完整的代码以供参考。
错误:错误:任务执行失败':app:compileDebugJavaWithJavac'。 java.lang.RuntimeException:发现数据绑定错误。 **** /数据绑定错误****消息:在android.widget.ImageView上找不到参数类型为java.lang.String的属性'app:profile_pic'的setter。 文件:E:\ Android的\ WTS_adroid_project_stuido \ Example_databinding \程序\ SRC \主\水库\布局\ activity_demo_1.xml 当地时间:25:31 - 25:49 **** \数据绑定错误****