【发布时间】:2016-04-18 23:27:46
【问题描述】:
我正在制作一个程序,我想在其中保存哪些球员正在训练,这只是教练的日记。所以我想在 recyclerview 的每一行都有三个单选按钮(一个用于玩家在场时,一个在他不在时,一个在他有理由不在那里时)。
我想将 PersonItem 中的状态值更改为状态 1,2 或 3,以便稍后在程序中使用它来将其保存到数据库中。
我遇到的问题是 recyclerview 没有正确显示,例如当我有两个 PersonItems 显示时。
/////////////////////////////////////// ////////////////////////////////.
PersonItem1.
(单选按钮)存在。
/////////////////////////////////////// /////////////////////////////////////////////。
PersonItem1.
(单选按钮)存在,(单选按钮)不存在。
/////////////////////////////////////// /////////////////////////////////////////。
单选按钮也有奇怪的行为。
代码如下:
public NewTrainingAdapter(Context context, ArrayList<PersonItem> feedItemList) {
this.feedItemList = feedItemList;
this.mContext = context;
}
@Override
public CustomViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.vrstica_new_training_row, null);
view.setClickable(true);
CustomViewHolder viewHolder = new CustomViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(final CustomViewHolder customViewHolder,final int i) {
PersonItem feedItem = feedItemList.get(i);
customViewHolder.name.setText(feedItem.getName().toString() + " " + feedItem.getLastName().toString());
setRadio(customViewHolder, 1);
customViewHolder.present.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
feedItemList.get(i).setState(0);
setRadio(customViewHolder, feedItemList.get(i).getState());
}
});
customViewHolder.absent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
feedItemList.get(i).setState(0);
setRadio(customViewHolder, feedItemList.get(i).getState());
}
});
customViewHolder.forgiven.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
feedItemList.get(i).setState(0);
setRadio(customViewHolder, feedItemList.get(i).getState());
}
});
}
@Override
public int getItemCount() {
return (null != feedItemList ? feedItemList.size() : 0);
}
public interface OnItemClickListener {
public void onItemClick(View view, int position);
}
public void setOnItemClickListener(final OnItemClickListener itemClickListener) {
this.itemClickListener = itemClickListener;
}
private void setRadio(final CustomViewHolder holder, int selection) {
RadioButton b1 = holder.present;
RadioButton b2 = holder.absent;
RadioButton b3 = holder.forgiven;
if (selection == 0) b1.setChecked(true);
if (selection == 1) b2.setChecked(true);
if (selection == 2) b3.setChecked(true);
}
public class CustomViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
protected TextView name;
protected RadioButton present,absent,forgiven;
protected RadioGroup presence;
public CustomViewHolder(View view) {
super(view);
this.name = (TextView) view.findViewById(R.id.textViewPlayerName);
this.presence = (RadioGroup) view.findViewById(R.id.radioGroupPlayerInTraining);
this.present = (RadioButton) view.findViewById(R.id.radioButtonPresent);
this.absent = (RadioButton) view.findViewById(R.id.radioButtonAbsent);
this.forgiven = (RadioButton) view.findViewById(R.id.radioButtonForgiven);
view.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (itemClickListener != null) {
itemClickListener.onItemClick(view, getAdapterPosition());
}
}
}
vrstica_new_training_row 中的代码也是:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/textViewPlayerName" />
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/radioGroupPlayerInTraining">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Prisoten"
android:id="@+id/radioButtonPresent"
android:checked="true" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Odsoten"
android:id="@+id/radioButtonAbsent"
android:checked="false" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Opravičen"
android:id="@+id/radioButtonForgiven"
android:checked="false" />
</RadioGroup>
</LinearLayout>
有没有人有任何建议我需要更改以使我的代码按我想要的方式工作。 感谢您提前回答。 :)
【问题讨论】:
标签: android radio-button android-recyclerview