单击按钮时,使用不同的布局保存Recycerview的Edittext值

时间:2017-05-22 22:26:25

标签: android android-recyclerview adapter

我必须重新制作一份包含说明,问题和回复字段的调查问卷。要重现布局,我使用textview显示不同的视图类型:textinputlayout用于说明,recyclerview用提示作为实际问题。
<?xml version="1.0" encoding="utf-8"?> <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/cardm2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="12dp" card_view:cardCornerRadius="4dp"> <android.support.design.widget.TextInputLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/hint"> <EditText android:id="@+id/risposta" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:inputType="text" android:paddingTop="15dp" android:textAlignment="center"/> </android.support.design.widget.TextInputLayout> 下方有一个按钮,单击该按钮时,应将所有edittext值保存在db表中。现在我的问题是:
1.我如何迭代所有edittexts以获取值和
2.在保存数据库之前保存数据的最佳结构是什么?

以下是单一问题布局的代码和Example of layout

public class QuestionAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

private List<Domande> mList;

public QuestionAdapter(List<Domande> list) {
    this.mList = list;
}

@Override
public int getItemViewType(int position) {
    if (mList != null) {
        Domande object = mList.get(position);
        if (object != null) {
            return object.getType();
        }
    }
    return 0;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View view;

    switch (viewType) {
        case ISTRUZIONI:
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.istruzione_single, parent, false);
            return new IstruzioniHolder(view);
        case DOMANDA:
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.domanda_single, parent, false);
            return new DomandaHolder(view);
        case ISTRUZIONI_BOLD:
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.istruzione_single_bold, parent, false);
            return new IstruzioniBoldHolder(view);
    }
    return null;
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    Domande object = mList.get(position);

    if (object != null) {
        switch (object.getType()) {
            case ISTRUZIONI:
                ((IstruzioniHolder) holder).mIstruz.setText(object.getIstruzione());
                break;
            case ISTRUZIONI_BOLD:
                ((IstruzioniBoldHolder) holder).mIstruzBold.setText(object.getIstruzione());
                break;
            case DOMANDA:
                ((DomandaHolder) holder).mDomanda.setHint(object.getDomanda());
                break;
        }
    }

}

public static class IstruzioniHolder extends RecyclerView.ViewHolder {
    private TextView mIstruz;
    public IstruzioniHolder(View itemView) {
        super(itemView);
        mIstruz = (TextView) itemView.findViewById(R.id.istruz);
    }
}

public static class IstruzioniBoldHolder extends RecyclerView.ViewHolder {

    private TextView mIstruzBold;

    public IstruzioniBoldHolder(View itemView) {
        super(itemView);
        mIstruzBold = (TextView) itemView.findViewById(R.id.istruz);
    }
}


public static class DomandaHolder extends RecyclerView.ViewHolder {

    private TextInputLayout mDomanda;

    public DomandaHolder(View itemView) {
        super(itemView);
        mDomanda = (TextInputLayout) itemView.findViewById(R.id.hint);
    }
}

@Override
public int getItemCount() {
    if (mList == null)
        return 0;
    return mList.size();
}
}

适配器:

public final class DummyData {

public static List<Domande> getData() {

    List<Domande> list = new ArrayList<>();
    list.add(new Domande("Adesso le farò alcune domande. In genere le domande sono facili. Ha 10 secondi per ogni risposta.", null, Domande.ISTRUZIONI_BOLD));
    list.add(new Domande(null, "In che anno siamo?", Domande.DOMANDA));
    list.add(new Domande(null, "In che stagione siamo?", Domande.DOMANDA));
    ...

    return list;
}
}

保存指令和问题的dummyData类是这样的

public class Pag1 extends Fragment {
public Pag1() {
}

private RecyclerView recyclerView;
private QuestionAdapter mAdapter;

private Button nxt;

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.pag1, container, false);

    recyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);

    nxt = (Button) rootView.findViewById(R.id.next);

    mAdapter = new QuestionAdapter(DummyData.getData());

    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
    recyclerView.setLayoutManager(mLayoutManager);
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    recyclerView.setAdapter(mAdapter);
    recyclerView.setNestedScrollingEnabled(false);

    nxt.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {


        }
    });


    return rootView;

}

}

最后是片段类

android:text="@+id/superHerosTV"

1 个答案:

答案 0 :(得分:0)

  1. 如何迭代所有edittexts以获取值

    nxt.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View view) {
    
    ArrayList<String> answers = new ArrayList<String>();
    for (int i = 0; i < myList.getCount(); i++) {
        View v = recycleView.findViewHolderForItemId(mAdapter.getItemId(i)).itemView;
        EditText editText= (EditText) searchResultFragmentRecycleView.findViewById(i);
        answers .add(et.getText().toString());
    
        }
    }
    
  2. 这是保存数据库之前保存数据的最佳结构吗?

  3. https://developer.android.com/training/basics/data-storage/databases.html

    修改

    final ArrayList<Integer> myList = new ArrayList<>();
        myList.add(R.id.editText1);
        myList.add(R.id.editText2);
        myList.add(R.id.editText3);
    
        nxt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
    
                ArrayList<String> answers = new ArrayList<String>();
                for (int i = 0; i < myList.size(); i++) {
                    View v = recyclerView.findViewHolderForItemId(mAdapter.getItemId(i)).itemView;
                    EditText editText = (EditText) v.findViewById(myList.get(i));
                    answers.add(editText.getText().toString());
    
                }
            }
        });