我有使用RecyclerView的活动。 RecyclerView行布局代码如下。在行布局中我有editTexts。当用户填写edittexts时,我想从RecyclerView中的这些edittext获取值。我无法得到正确的价值观。它看起来像适配器返回坏的值或我不知道问题在哪里。知道我做得不好吗?
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/cv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground"
card_view:cardBackgroundColor="#fff"
card_view:cardCornerRadius="2dp"
card_view:cardElevation="2sp"
android:layout_marginBottom="6dp"
android:orientation="horizontal"
android:padding="10dp"
android:weightSum="5"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp"
android:weightSum="5">
<TextView
android:id="@+id/exercise_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:gravity="center_vertical"
android:layout_gravity="center_vertical"
android:textColor="#ab000000"
android:textSize="16sp" />
<EditText
android:id="@+id/et_repeats"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="number"
android:gravity="center"
android:layout_gravity="center"
android:textColor="@color/float_label"
android:textSize="18sp" />
<EditText
android:id="@+id/et_weight"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:layout_gravity="center"
android:inputType="number"
android:textColor="@color/float_label"
android:textSize="18sp" />
</LinearLayout>
public class SeriesAdapter extends RecyclerView.Adapter<SeriesAdapter.ViewHolder> {
private ArrayList<Series> series = null;
private Context context = null;
public SeriesAdapter(ArrayList<Series> series, Context context) {
this.series = series;
this.context = context;
}
@Override
public SeriesAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.series_scr_row, parent, false);
SeriesAdapter.ViewHolder viewHolder = new ViewHolder(view, new SeriesAdapter.ViewHolder.IMyViewHolderClicks() {
@Override
public void onRowClick(View caller, int position) {
long id = series.get(position).idSeries;
doAction(id);
}
}, new ViewHolder.IMyViewHolderClicksLong() {
@Override
public void onRowClickLong(View caller, int position) {
long id = series.get(position).idSeries;
doActionLong(id);
}
});
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.exercise_name.setText(++position + context.getResources().getString(R.string.series_label));
holder.et_repeats.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable ss) {
}
});
}
@Override
public long getItemId(int position) {
return series.get(position).idSeries;
}
@Override
public int getItemCount() {
return series.size();
}
private void doAction(long id) {
HashMap<String, Object> hashMap = new HashMap<>();
hashMap.put("id", id);
startMyActivity(context, SeriesActivity.class, hashMap, false);
}
private void doActionLong(long id) {
}
public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
public TextView exercise_name;
public EditText et_repeats;
public EditText et_weight;
public IMyViewHolderClicks mListener;
public IMyViewHolderClicksLong mListenerLong;
public ViewHolder(View itemLayoutView, IMyViewHolderClicks listener, IMyViewHolderClicksLong listenerLong) {
super(itemLayoutView);
mListener = listener;
mListenerLong = listenerLong;
exercise_name = (TextView) itemLayoutView.findViewById(R.id.exercise_name);
et_repeats = (EditText) itemLayoutView.findViewById(R.id.et_repeats);
et_weight = (EditText) itemLayoutView.findViewById(R.id.et_weight);
itemLayoutView.setOnClickListener(this);
itemLayoutView.setOnLongClickListener(this);
}
@Override
public void onClick(View v) {
mListener.onRowClick(v, getAdapterPosition());
}
@Override
public boolean onLongClick(View v) {
mListenerLong.onRowClickLong(v, getAdapterPosition());
return true;
}
public interface IMyViewHolderClicks {
public void onRowClick(View caller, int position);
}
public interface IMyViewHolderClicksLong {
public void onRowClickLong(View caller, int position);
}
}
}
在“活动”中,我尝试获取以下值:
for (int i = 0; i < rv_series_list.getAdapter().getItemCount(); i++) {
CardView cardView = (CardView) rv_series_list.getChildAt(i);
LinearLayout linearLayout = (LinearLayout) cardView.getChildAt(0);
EditText etWeight = (EditText)linearLayout.getChildAt(1);
EditText etRepeat = (EditText)linearLayout.getChildAt(2);
db.addRecord( new Records(-1,++i,
Integer.parseInt(etWeight.getText().toString()) ,
Integer.parseInt(etRepeat.getText().toString()), idOfExercise, -1));
}