我在使用Integer数据类型实现双向绑定时遇到了一些问题。我有回收视图用户必须插入程度,当他点击按钮的数据改造将发送到服务器。但是当用户在学位字段中插入数字时,它将返回0
如何修复它请。
模型类:
public class QuModel extends AppCompatActivity {
public String Qu;
public String answer1;
public String answer2;
public String answer3;
public String answer4;
public int degree ;
public QuModel() {
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name = "qumodel"
type = "com.hota.exam.model.QuModel"/>
</data>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="4dp"
tools:ignore="ExtraText">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="4dp">
<EditText
android:id="@+id/editTextQuestion"
android:layout_width="322dp"
android:layout_height="84dp"
android:layout_marginTop="64dp"
android:ems="10"
android:hint="Question"
android:inputType="textPersonName"
android:padding="4dp"
android:text="@={qumodel.Qu}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.539"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/QuNumber"
android:layout_width="231dp"
android:layout_height="41dp"
android:layout_marginTop="24dp"
android:padding="4dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.266"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/editTextAnswer1"
android:layout_width="183dp"
android:layout_height="48dp"
android:layout_marginTop="180dp"
android:hint="Answer 1"
android:inputType="textPersonName"
android:padding="4dp"
android:text="@={qumodel.answer1}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.07"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/editTextAnswer2"
android:layout_width="183dp"
android:layout_height="48dp"
android:layout_marginTop="232dp"
android:hint="Answer 2"
android:inputType="textPersonName"
android:padding="4dp"
android:text="@={qumodel.answer2}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.07"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/editTextAnswer4"
android:layout_width="183dp"
android:layout_height="48dp"
android:layout_marginTop="328dp"
android:hint="Answer 4"
android:inputType="textPersonName"
android:padding="4dp"
android:text="@={qumodel.answer4}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.07"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/editTextAnswer3"
android:layout_width="183dp"
android:layout_height="48dp"
android:layout_marginTop="276dp"
android:hint="Answer 3"
android:inputType="textPersonName"
android:padding="4dp"
android:text="@={qumodel.answer3}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.07"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/degree"
android:layout_width="98dp"
android:layout_height="45dp"
android:layout_marginTop="192dp"
android:hint="Degree"
android:inputType="number"
android:padding="4dp"
android:text="@{String.valueOf(qumodel.degree)}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</layout>
适配器:
public class QuAdapter extends RecyclerView.Adapter<QuAdapter.QuViewHolder> {
int id;
private ArrayList<QuModel> dataModelList;
private static final String TAG = "QuAdapter";
public QuAdapter(ArrayList<QuModel> dataModelList) {
this.dataModelList = dataModelList;
}
@NonNull
@Override
public QuViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
ListItemBinding Binding = DataBindingUtil.inflate(
LayoutInflater.from(parent.getContext()),
R.layout.list_item, parent, false);
return new QuViewHolder(Binding);
}
@SuppressLint("SetTextI18n")
@Override
public void onBindViewHolder(@NonNull QuViewHolder holder, int position) {
QuModel quModel = dataModelList.get(position);
holder.bind(quModel);
int Counter = position + 1 ;
holder.listItemBinding.QuNumber.setText("Qu no : " + Counter);
}
@Override
public int getItemCount() {
return dataModelList.size();
}
public static class QuViewHolder extends RecyclerView.ViewHolder {
public ListItemBinding listItemBinding;
public QuViewHolder(ListItemBinding listItemBinding) {
super(listItemBinding.getRoot());
this.listItemBinding = listItemBinding;
}
public void bind(Object obj) {
listItemBinding.setVariable(BR.qumodel, obj);
listItemBinding.executePendingBindings();
}
}
}
1- Make degree String and Make inputType number .
2-在适配器中添加textwhatcher来处理文本的变化。
3-使用Integer转换类型。