2017-10-16 80 views
0

我試過使用mvvm模式的listview應用程序。數據綁定錯誤:數據綁定不存在

但我在android工作室中有錯誤。

我不知道爲什麼這個項目不起作用。

structure image

錯誤:

  1. 錯誤:(18,49)誤差:包com.example.jhseo.flight_mvvm.databinding不存在

  2. 錯誤:(30, 13)錯誤:找不到符號類FlightActivityBinding

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

    java.lang.RuntimeException: Found data binding errors. ****/ data binding error ****msg:Identifiers must have user defined types from the XML file. viewModel is missing it file:E:\workspace_mobile\test\Flight-Mvvm\app\src\main\res\layout\flight_item.xml loc:15:27 - 15:35 ****\ data binding error ****

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

您得到的錯誤非常明顯:在flight_item.xml中沒有定義的標識符viewModel

變化

android:onClick="@{viewModel::onItemClick}" 

android:onClick="@{itemModel::onItemClick}" 

flight_item.xml文件。

+0

哦,我的上帝,我的錯誤,謝謝先生。 – Jays