ListviewAdapter在Android

时间:2015-07-01 14:37:54

标签: android getview listview-adapter

我正在尝试在listview中对每一行进行不同的布局,但是使用ListviewAdapter getView方法为每一行编写相同的内容。

我的_data1如下:

_data1.get(0)= {gunesli}; // answer1 for question1

_data1.get(1)= {yagmurlu}; // answer2 for question1

_data1.get(2)= {evet}; // answer3 for question2

_data1.get(3)= {hayir}; // answer4 for question2

_soru数据如下:

_soru.get(0)= {“Hava Nasil?” }; // question1

_soru.get(1)= {“Kola Sevimlimi?” }; // question2

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) _context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        for (int j = 0; j < _type.size(); j++) {
            if (_type.get(j).equals("1")) {
                convertView = inflater.inflate(R.layout.list_item_widget1, parent, false);
                radioGroup1 = (RadioGroup) convertView.findViewById(R.id.radioGroup1);
                soru = (TextView) convertView.findViewById(R.id.soru);
                soru.setTextSize(20);
                soru.setText(_soru.get(j));
                soru.setGravity(Gravity.CENTER);
                final RadioButton[] radioButton1 = new RadioButton[10];
                for (int i = 0; i < _data1.get(0).size(); i++) {
                    String[] temp1 = _data1.get(0).get(i).split(",");
                    if (temp1[2].equals(_pollId.get(j))) {
                        radioButton1[i] = new RadioButton(_context);
                        radioGroup1.addView(radioButton1[i]);
                        radioButton1[i].setText(temp1[1]);
                        String[] temp2 = _data2.get(0).get(i).split(",");
                        if (temp2[2].equals(temp1[0])) {
                            if (temp2[1].equals("1")) {
                                radioButton1[i].setChecked(true);
                            }
                        }
                    }
                }
            } else if (_type.get(j).equals("2")) {
                convertView = inflater.inflate(R.layout.list_item_widget2, parent, false);
                radioGroup2 = (RadioGroup) convertView.findViewById(R.id.radioGroup2);

                soru2 = (TextView) convertView.findViewById(R.id.soru2);
                soru2.setTextSize(20);
                soru2.setText(_soru.get(j));
                soru2.setGravity(Gravity.CENTER);
                final RadioButton[] radioButton2 = new RadioButton[10];
                for (int i = 0; i < _data1.get(0).size(); i++) {
                    String[] temp1 = _data1.get(0).get(i).split(",");
                    if (temp1[2].equals(_pollId.get(j))) {
                        radioButton2[i] = new RadioButton(_context);
                        radioGroup2.addView(radioButton2[i]);
                        radioButton2[i].setText(temp1[1]);
                        String[] temp2 = _data2.get(0).get(i).split(",");
                        if (temp2[2].equals(temp1[0])) {
                            if (temp2[1].equals("1")) {
                                radioButton2[i].setChecked(true);
                            }
                        }
                    }
                }
            }
        }
    }
    return convertView;
}

然后如下所示:

enter image description here

0 个答案:

没有答案