我尝试使用listview应用程序来使用mvvm模式。
但我在android studio中有错误。
我不知道为什么这个项目不起作用。
错误:
错误:(18,49)错误:包com.example.jhseo.flight_mvvm.databinding不存在
错误:(30,13)错误:找不到符号类FlightActivityBinding
错误:任务':app:compileDebugJavaWithJavac'。
执行失败java.lang.RuntimeException:发现数据绑定错误。 **** /数据绑定错误****消息:标识符必须具有XML文件中的用户定义类型。 viewModel缺少它 文件:E:\ workspace_mobile \测试\飞行-MVVM \程序\ SRC \主\水库\布局\ flight_item.xml 当地时间:15:27 - 15:35 **** \数据绑定错误****
FlightActivity
public class FlightActivity extends AppCompatActivity implements FlightViewModel.DataListener{
private FlightActivityBinding binding;
private FlightViewModel flightViewModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.flight_activity);
flightViewModel = new FlightViewModel(this, this);
binding.setViewModel(flightViewModel);
setupListVIew(binding.flightListView);
@Override
protected void onDestroy() {
super.onDestroy();
}
@Override
public void onListChanged(ArrayList<Flight> flights) {
FlightListAdapter adapter =(FlightListAdapter) binding.flightListView.getAdapter();
adapter.setFlights(flights);
adapter.notifyDataSetChanged();
}
private void setupListVIew(ListView flightListView) {
FlightListAdapter adapter = new FlightListAdapter(this);
flightListView.setAdapter(adapter);
}
}
FlightViewModel
public class FlightViewModel {
FlightFactory flightFactory;
...
public ObservableArrayList<Flight> flightList;
private Context context;
private DataListener dataListener;
private Realm realm;
public FlightViewModel(Context context, DataListener dataListener) {
this.context = context;
this.dataListener = dataListener;
init();
loadData();
}
public void setDataListener(DataListener dataListener) { this.dataListener = dataListener; }
...
public void searchByConditionInListAndSet() {
RealmQuery<Flight> query = realm.where(Flight.class);
if (!"".equals(terminal.get())) {
query = query.equalTo("terminal", terminal.get());
}
flightList.addAll(query.findAll().subList(0, (int) query.count()));
if (dataListener != null) {
dataListener.onListChanged(flightList);
}
...
}
public interface DataListener {
void onListChanged(ArrayList<Flight> flights);
}
}
ItemViewModel
public class ItemViewModel extends BaseObservable {
private Flight flight;
private Context context;
public ItemViewModel(Flight flight, Context context) {
this.flight = flight;
this.context = context;
}
...
public void onItemClick(View view) {
//TODO: Detail Page
//context.startActivity();
}
public void setFlight(Flight flight) {
this.flight = flight;
notifyChange();
}
}
flight_activity.xml
flight_item.xml
<data>
<variable
name="itemModel"
type="com.example.jhseo.flight_mvvm.viewModel.ItemViewModel"/>
</data>
<LinearLayout android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:onClick="@{viewModel::onItemClick}">
<LinearLayout
android:layout_width="50dp"
android:layout_height="wrap_content">
<TextView
android:id="@+id/timeTextView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="@{itemModel.time}" />
</LinearLayout>
<LinearLayout
android:layout_width="100dp"
android:layout_height="wrap_content">
<TextView
android:id="@+id/flightStateTextView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="@{itemModel.flightState}" />
</LinearLayout>
<LinearLayout
android:id="@+id/destinationi"
android:layout_width="80dp"
android:layout_height="wrap_content">
<TextView
android:id="@+id/destinationTextView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="@{itemModel.destination}" />
</LinearLayout>
<LinearLayout
android:id="@+id/counterAndGatei"
android:layout_width="80dp"
android:layout_height="wrap_content">
<TextView
android:id="@+id/counterAndGateTextView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="@{itemModel.counterAndGate}" />
</LinearLayout>
<LinearLayout
android:id="@+id/origini"
android:layout_width="70dp"
android:layout_height="wrap_content">
<TextView
android:id="@+id/originTextView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="@{itemModel.origin}" />
</LinearLayout>
<LinearLayout
android:id="@+id/carouseli"
android:layout_width="40dp"
android:layout_height="wrap_content">
<TextView
android:id="@+id/carouselTextView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="@{itemModel.carousel}" />
</LinearLayout>
<LinearLayout
android:id="@+id/exitDoori"
android:layout_width="70dp"
android:layout_height="wrap_content">
<TextView
android:id="@+id/exitDoorTextView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="@{itemModel.exitDoor}" />
</LinearLayout>
</LinearLayout>
</layout>
答案 0 :(得分:1)
您得到的错误非常明确:viewModel
中没有定义标识符flight_item.xml
。
更改
android:onClick="@{viewModel::onItemClick}"
到
android:onClick="@{itemModel::onItemClick}"
在flight_item.xml
档案中。