数据绑定活动中的null editexts

时间:2018-11-16 12:09:37

标签: android android-databinding

如何使用Viewmodel中的数据绑定在editexts中设置数据? 我将参数从RecyclerView项传递到OnItemClick中的ViewModel,并在日志中一切正常,但是Edittexts仍然为空。当我尝试设置在ViewModel中创建的String时,它是相同的。 EditTexts仍然为空。我不知道哪里是错误。

ViewModel:

public class MainViewModel extends AndroidViewModel implements RecyclerViewAdapter.OnItemClickListener {

private TaskRepository taskRepository;
private LiveData<List<Task>> allTasks;
public ObservableField<String> description = new ObservableField<>();
public ObservableField<String> date = new ObservableField<>();
public ObservableField<String> time = new ObservableField<>();
String TAG;


public MainViewModel(@NonNull Application application) {
    super(application);
    taskRepository = new TaskRepository(application);
    allTasks = taskRepository.getAllTasks();
}

public LiveData<List<Task>> getAllTasks() {
    return allTasks;
}

public void deleteTask(Task task) {
    taskRepository.delete(task);
}

@Override
public void onItemClick(View view, Task item) {
    Log.d(TAG, "MVM " + view + " / " + item.getDescription() + " / " + item.getDate() + " / " + item.getTime());
    description.set(item.getDescription());
    date.set(item.getDate());
    time.set(item.getTime());
}

XML文件:

<layout xmlns:android="http://schemas.android.com/apk/res/android">

<data>

    <variable
        name="editTaskViewModel"
        type="com.example.daniellachacz.taskmvvm.viewmodel.MainViewModel">
    </variable>

    <variable
        name="editTaskInterface"
        type="com.example.daniellachacz.taskmvvm.view.OnEditTaskClickInterface">
    </variable>

</data>

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#FFF">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:background="@color/colorPrimaryDark">

            <TextView
                android:id="@+id/title"
                android:layout_width="100dp"
                android:layout_height="40dp"
                android:layout_alignParentStart="true"
                android:layout_alignParentTop="true"
                android:layout_marginStart="30dp"
                android:layout_marginTop="30dp"
                android:clickable="false"
                android:focusable="false"
                android:text="Edit task"
                android:textAlignment="center"
                android:textColor="#FFF"
                android:textSize="25sp" />

            <EditText
                android:id="@+id/edit_description_text"
                android:layout_width="320dp"
                android:layout_height="40dp"
                android:layout_below="@+id/title"
                android:layout_alignStart="@+id/title"
                android:backgroundTint="#FFF"
                android:text="@={editTaskViewModel.description}"
                android:textColor="#FFF"
                android:textColorHint="#949393"
                android:textSize="18sp" />

        </RelativeLayout>

        <TextView
            android:id="@+id/edit_date_text"
            android:layout_width="130dp"
            android:layout_height="40dp"
            android:layout_alignTop="@+id/edit_time_text"
            android:layout_marginStart="40dp"
            android:clickable="true"
            android:focusable="true"
            android:gravity="center"
            android:hint="Select date"
            android:onClick="@{()-> editTaskInterface.onEditDateClick()}"
            android:text="@={editTaskViewModel.date}"
            android:textSize="18sp" />

        <View
            android:id="@+id/line"
            android:layout_width="320dp"
            android:layout_height="1dp"
            android:layout_below="@+id/date_text"
            android:layout_centerHorizontal="true"
            android:background="@color/colorPrimaryDark" />

        <TextView
            android:id="@+id/edit_time_text"
            android:layout_width="132dp"
            android:layout_height="40dp"
            android:layout_alignParentTop="true"
            android:layout_alignParentEnd="true"
            android:layout_marginLeft="40dp"
            android:layout_marginTop="231dp"
            android:layout_marginEnd="40dp"
            android:clickable="true"
            android:focusable="true"
            android:gravity="center"
            android:hint="Select time"
            android:onClick="@{()-> editTaskInterface.onEditTimeClick()}"
            android:text="@={editTaskViewModel.time}"
            android:textAlignment="center"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/and"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_alignParentTop="true"
            android:layout_centerInParent="true"
            android:layout_marginTop="230dp"
            android:gravity="center"
            android:text="and"
            android:textColor="@color/colorPrimaryDark"
            android:textSize="18sp" />

        <Button
            android:id="@+id/edit_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="115dp"
            android:onClick="@{()-> editTaskInterface.onEditClick()}"
            android:text="Edit" />

    </RelativeLayout>


</android.support.constraint.ConstraintLayout>

EditTaskActivity:

public class EditTaskActivity extends AppCompatActivity implements RecyclerViewAdapter.OnItemClickListener {

private String TAG;
public EditTaskActivityBinding editTaskActivityBinding;
private MainViewModel mainViewModel;
private DatePickerDialog.OnDateSetListener dateSetListener;
private TimePickerDialog.OnTimeSetListener timeSetListener;


@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    editTaskActivityBinding = DataBindingUtil.setContentView(this, R.layout.edit_task_activity);
    mainViewModel = ViewModelProviders.of(this).get(MainViewModel.class);
    editTaskActivityBinding.setEditTaskViewModel(mainViewModel);

    dateSetListener = (view, year, month, dayOfMonth) -> {
        month = month + 1;
        Log.d(TAG, "onDataSet: dd/mm/yyyy: " + year + "/" + month + "/" + dayOfMonth);
        String date = dayOfMonth + "/" + month + "/" + year;
        editTaskActivityBinding.editDateText.setText(date);
    };

    timeSetListener = (view, hourOfDay, minute) -> {
        Log.d(TAG, "onTimeSet: hh/mm: " + hourOfDay + "/" + minute);
        String time = hourOfDay + ":" + minute;
        editTaskActivityBinding.editTimeText.setText(time);
    };

    editTaskActivityBinding.setEditTaskInterface(new OnEditTaskClickInterface() {
        @Override
        public void onEditClick() {

        }

        @Override
        public void onEditDateClick() {
            showDatePicker();
        }

        @Override
        public void onEditTimeClick() {
            showTimePicker();
        }
    });
}

public void showDatePicker() {
    Calendar calendar = Calendar.getInstance();
    int year = calendar.get(Calendar.YEAR);
    int month = calendar.get(Calendar.MONTH);
    int day = calendar.get(Calendar.DAY_OF_MONTH);

    DatePickerDialog dialog = new DatePickerDialog(this, android.R.style.Theme_Holo_Light_Dialog_MinWidth, dateSetListener,
            year, month, day);
    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    dialog.show();
}

public void showTimePicker() {
    Calendar calendar = Calendar.getInstance();
    int hourOfDay = calendar.get(Calendar.HOUR_OF_DAY);
    int minute = calendar.get(Calendar.MINUTE);

    TimePickerDialog dialog = new TimePickerDialog(this, android.R.style.Theme_Holo_Light_Dialog_MinWidth, timeSetListener,
            hourOfDay, minute, DateFormat.is24HourFormat(this));
    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    dialog.show();
}

0 个答案:

没有答案