我想使用PutExtra从项目传递字符串以编辑活动,但是出了点问题。我在日志中看到字符串,但是一个EditText和两个TextView是空的...在其他活动中,我可以在TextView中进行所需的设置,但是我不知道发生了什么。
EditTaskActivity:
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EditTaskActivityBinding editTaskActivityBinding = DataBindingUtil.setContentView(this, R.layout.edit_task_activity);
EditTaskViewModel editTaskViewModel = ViewModelProviders.of(this).get(EditTaskViewModel.class);
editTaskActivityBinding.setEditTaskViewModel(editTaskViewModel);
Intent intent = getIntent();
editTaskActivityBinding.editDescriptionText.setText(intent.getStringExtra(EXTRA_DESCRIPTION));
editTaskActivityBinding.editDateText.setText(intent.getStringExtra(EXTRA_DATE));
editTaskActivityBinding.editTimeText.setText(intent.getStringExtra(EXTRA_TIME));
Log.d(TAG, "INTENT " + intent.getStringExtra(EXTRA_DESCRIPTION) + " " + intent.getStringExtra(EXTRA_DATE) + " " +
intent.getStringExtra(EXTRA_TIME));
}
MainActivity和OnItemClickListener:
recyclerViewAdapter.setOnItemClickListener(new RecyclerViewAdapter.OnItemClickListener() {
@Override
public void onItemClick(Task task) {
Intent intent = new Intent(MainActivity.this, EditTaskActivity.class);
intent.putExtra(EditTaskActivity.EXTRA_DESCRIPTION, task.getDescription());
intent.putExtra(EditTaskActivity.EXTRA_DATE, task.getDate());
intent.putExtra(EditTaskActivity.EXTRA_TIME, task.getTime());
startActivity(intent);
}
});
和EditTaskActivity的XML文件:
<?xml version="1.0" encoding="utf-8"?>
<data>
<variable
name="editTaskViewModel"
type="com.example.daniellachacz.taskmvvm.viewmodel.EditTaskViewModel">
</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_marginLeft="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>