How To use Different Items Type With Recycler View

时间:2018-12-19 11:12:23

标签: android android-recyclerview

I am Creating A Feedback form using the data from the server.

The server sends different questions types and according to which the type of answer is decided.

When Q_type == "1"

Then I have to use the checkbox

When Q_type == "2"

Then I Have to use the radio buttons

When Q_type == "3"

Then I Have to use text view

When Q_type = 1 recycler view is showing the checkboxes

but when the Q_type=2&3 recycler view is not showing anything

I Am Getting this after the response from the server

V/FA: Inactivity, disconnecting from the service

This Is My Code For OnBindViewHolder in RecyclerAdapter

  @Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
    ViewHolder gv = (ViewHolder) holder;
    if (modelList.get(position).getQuest().size()!=0) {
        //Here you can fill your row view
        if (holder instanceof ViewHolder) {
            final datumnote model = getItem(position);
            String[] answers = model.getAnswerList().toArray(new String[model.getAnswerList().size()]);
            //String [] description = model.getQuest().get(position).getDescription().toArray(new String[model.getAnswerList().size()]);
            Log.i("Question", modelList.get(position).getQuestion());
            Log.i("QType", modelList.get(position).getQType());
            Log.i("Answer", modelList.get(position).getQuest().get(position).getDesc());
            if (model.getQType().equals("1"))
            {
                gv.itemTxtMessage.setVisibility(View.GONE);
                gv.itemTxtTitle.setText(model.getQuestion());
                for (int i = 0; i < answers.length; i++) {
                    CheckBox cb = new CheckBox(mContext);
                    ColorStateList colorStateList = new ColorStateList(
                            new int[][]{
                                    new int[]{android.R.attr.state_enabled} //enabled
                            },
                            new int[] {
                                    Color.WHITE //enabled

                            }
                    );
                    cb.setTextColor(Color.parseColor("#ffffff"));
                    cb.setButtonTintList(colorStateList);
                    //cb.setChecked(true);
                    cb.setText(answers[i]);
                    gv.lv.addView(cb);
                }
            }
            if (model.getQType().equals("2"))
            {
                gv.itemTxtTitle.setText(model.getQuestion());
                RadioGroup ll = new RadioGroup(mContext);
                for (int i = 1; i <= answers.length; i++) {
                    RadioButton rdbtn = new RadioButton(mContext);
                    rdbtn.setId(i + 1000);
                    rdbtn.setText(answers[i]);
                    ll.addView(rdbtn);
                }
                gv.lv.addView(ll);
            }
            if (model.getQType().equals("3")) {
                gv.itemTxtMessage.setVisibility(View.VISIBLE);
                gv.itemTxtTitle.setText(model.getQuestion());
                gv.itemTxtMessage.setText(model.getQuest().get(position).getDesc());
            }
        }
    }
    else
    {
        gv.itemTxtTitle.setText(modelList.get(position).getQuestion());
        gv.itemTxtMessage.setText("No Answer Found");
    }
}

0 个答案:

没有答案