【问题标题】:Android: Get values of listview with multiple row layoutAndroid:获取具有多行布局的列表视图的值
【发布时间】:2016-07-17 10:13:11
【问题描述】:

我有一个具有多行布局(单选组、编辑文本、文本视图)的列表视图,具有多项选择模式。我想在提交按钮上检索它的选定数据(用户选择或键入的数据)。

这是我的主文件中的代码:

public class GraduatingSurvey extends Fragment {


    private static final String LOGTAG = "log" ;
    public String stdcode = "024-15-16079";
    Button submit;
    RadioGroup radioGroup;
    RadioButton radioButton;
    EditText comment;

    ArrayList<GraduatingSurveyModel> graduatingModelList;
    ListView listView;
    View view;

    public GraduatingSurvey() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view =  inflater.inflate(R.layout.fragment_graduating_survey, container, false);
        submit = (Button) view.findViewById(R.id.btn_graduate_submit);
        listView = (ListView) view.findViewById(R.id.graduate_list);
        graduatingModelList = new ArrayList<GraduatingSurveyModel>();

        //handle submit form Event.
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final int child=listView.getAdapter().getCount();
                Log.i("radio", "child: " + child);
                SparseBooleanArray checked = listView.getCheckedItemPositions(); //Is returning empty
                Log.i("radio", "sparseArray: " + checked);
                for(int i=0;i<child;i++) {
                    int type = listView.getAdapter().getItemViewType(i);
                    Log.i("radio", "type: " + type);
                    if(type == 2){
                        // Want to retrieve data here

                    }
                    if(type == 0)
                    {

                    }
                    if(type == 1){

                    }

                }

            }
        });

        NetAsync(view);
        return view;
    }

这是适配器:

public class GraduatingSurveyAdapter extends ArrayAdapter<GraduatingSurveyModel> {

    private static final int TYPE_ITEM1 = 0;
    private static final int TYPE_ITEM2 = 1;
    private static final int TYPE_ITEM3 = 2;
    int type;
    ArrayList<GraduatingSurveyModel> ArrayListSurvey;
    int Resource;
    Context context;
    LayoutInflater vi;
    public GraduatingSurveyAdapter(Context context, int resource, ArrayList<GraduatingSurveyModel> objects) {
        super(context, resource, objects);
        ArrayListSurvey = objects;
        Resource = resource;
        this.context = context;
        vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getItemViewType(int position) {

        if (position == 14){
            type = TYPE_ITEM1;
        } else if  (position == 23 || position == 24 || position == 25){
            type = TYPE_ITEM2;
        }
        else
        {
            type= TYPE_ITEM3 ;
        }
        return type;
    }

    @Override
    public int getViewTypeCount() {
        return 3;
    }

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

        ViewHolder holder;
        //first Item or row is being created hence ConvertView is NULL
        if(convertView == null){

            if (type == TYPE_ITEM2) {
                convertView = vi.inflate(R.layout.graduates_survey_comment,null);
                holder = new ViewHolder();
                holder.tv_question = (TextView) convertView.findViewById(R.id.graduate_question);
                holder.tv_comment = (TextView) convertView.findViewById(R.id.graduate_comment);
            }
            else if (type == TYPE_ITEM1) {
                convertView = vi.inflate(R.layout.graduates_survey_subheader,null);
                holder = new ViewHolder();
                holder.tv_question = (TextView) convertView.findViewById(R.id.graduate_question);
            }
           else {
                //infalte layout of normaltype

                //Inflating items only once
                convertView = vi.inflate(Resource,null);
                holder = new ViewHolder();

                holder.tv_question = (TextView) convertView.findViewById(R.id.graduate_question);
                holder.tv_comment = (TextView) convertView.findViewById(R.id.graduate_comment);
                holder.rg = (RadioGroup) convertView.findViewById(R.id.graduate_radioGroup);
                holder.rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(RadioGroup group, int checkedId) {
                       /* RadioButton radioButton = (RadioButton) group.findViewById(checkedId);
                        Log.i("radio", "onCheckedChanged: " + radioButton + " " + checkedId);
                        notifyDataSetChanged();*/
                        Integer pos = (Integer) group.getTag();
                        GraduatingSurveyModel element = ArrayListSurvey.get(pos);
                        switch (checkedId) {
                            case R.id.graduate_sAgree:
                                element.current = GraduatingSurveyModel.ANSWER_ONE_SELECTED;
                                break;
                            case R.id.graduate_agree:
                                element.current = GraduatingSurveyModel.ANSWER_TWO_SELECTED;
                                break;
                            case R.id.graduate_uncertain:
                                element.current = GraduatingSurveyModel.ANSWER_THREE_SELECTED;
                                break;
                            case R.id.graduate_disagree:
                                element.current = GraduatingSurveyModel.ANSWER_FOUR_SELECTED;
                                break;
                            case R.id.graduate_sDisagree:
                                element.current = GraduatingSurveyModel.ANSWER_FIVE_SELECTED;
                                break;
                            default:
                                element.current = GraduatingSurveyModel.NONE; // Something was

                        }


                    }
                });
            }

            convertView.setTag(holder);
        }else{
            //reuse code. (Inflate items only once then reuse them.)
            holder = (ViewHolder) convertView.getTag();
        }

        // get info from ArrayList(that came through JSON), and set it in the View.

        if (type == TYPE_ITEM1) {
            holder.tv_question.setText(ArrayListSurvey.get(position).getQuestion());
        }
        else if (type == TYPE_ITEM2) {
            holder.tv_question.setText(ArrayListSurvey.get(position).getQuestion());
        }
        else{
            holder.rg.setTag(new Integer(position));
            holder.tv_question.setText(ArrayListSurvey.get(position).getQuestion());
            if(ArrayListSurvey.get(position).current != GraduatingSurveyModel.NONE){
                RadioButton r = (RadioButton) holder.rg.getChildAt(ArrayListSurvey.get(position).current);
                r.setChecked(true);
            }else{
                holder.rg.clearCheck();
            }
        }
        return convertView;
    }
    static class ViewHolder{
        private TextView tv_question;
        private TextView tv_comment;
        private LinearLayout lv_choices;
        private RadioGroup rg;

    }
}

这是我的模型:

public class GraduatingSurveyModel {

    public String question;
    public String comment;
    public String choices;
    public String subHeading;


    public int current = NONE; // hold the answer picked by the user, initial is NONE(see below)
    public static final int NONE = 1000; // No answer selected
    public static final int ANSWER_ONE_SELECTED = 0; // first answer selected
    public static final int ANSWER_TWO_SELECTED = 1; // second answer selected
    public static final int ANSWER_THREE_SELECTED = 2; // third answer selected
    public static final int ANSWER_FOUR_SELECTED = 3; // forth answer selected
    public static final int ANSWER_FIVE_SELECTED = 4; // forth answer selected


    public GraduatingSurveyModel() {

    }

    public String getQuestion() {
        return question;
    }

    public void setQuestion(String question) {
        this.question = question;
    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    public String getChoices() {
        return choices;
    }

    public void setChoices(String choices) {
        this.choices = choices;
    }

    public String getSubHeading() {
        return subHeading;
    }

    public void setSubHeading(String subHeading) {
        this.subHeading = subHeading;
    }
}

这是主要片段的 XML 布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    android:background="@color/dialog_background"
    android:orientation="vertical"
    tools:context="iulms.iunc.edu.pk.iqraiulms.GraduatingSurvey"
    android:id="@+id/main_layout">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/graduate_list"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_above="@+id/btn_graduate_submit"
        />
    <Button
        android:id="@+id/btn_graduate_submit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="submit"
        android:layout_alignParentBottom="true"
        />
</RelativeLayout>

这些是列表视图中不同类型行的布局

行类型 1:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/dialog_background"
    android:orientation="vertical"
    android:paddingBottom="16dp"
    android:paddingTop="16dp">

    <TextView
        android:id="@+id/graduate_question"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="15dp"
        android:text="1. Here comes questions?"
        android:textSize="13dp" />

   <!-- <EditText
        android:id="@+id/graduate_comment"
        style="@style/GraduateTextBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:background="@drawable/textbox"
        android:ems="10"
        android:hint="Comment"
        android:inputType="textMultiLine"
        android:lines="3" />-->

    <LinearLayout
        android:id="@+id/graduate_choices"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <RadioGroup
            android:id="@+id/graduate_radioGroup"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <RadioButton
                android:id="@+id/graduate_sAgree"
                style="@style/GraduateRadioButton"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/checkbox_selector"
                android:button="@android:color/transparent"
                android:checked="false"
                android:text="Strongly Agree" />

            <RadioButton
                android:id="@+id/graduate_agree"
                style="@style/GraduateRadioButton"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/checkbox_selector"
                android:button="@android:color/transparent"
                android:checked="false"
                android:text="Agree" />

            <RadioButton
                android:id="@+id/graduate_uncertain"
                style="@style/GraduateRadioButton"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/checkbox_selector"
                android:button="@android:color/transparent"
                android:checked="false"
                android:text="Uncertain"
                />

            <RadioButton
                android:id="@+id/graduate_disagree"
                style="@style/GraduateRadioButton"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/checkbox_selector"
                android:button="@android:color/transparent"
                android:checked="false"
                android:text="Disagree" />

            <RadioButton
                android:id="@+id/graduate_sDisagree"
                style="@style/GraduateRadioButton"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/checkbox_selector"
                android:button="@android:color/transparent"
                android:checked="false"
                android:text="Strongly Disagree" />
        </RadioGroup>
    </LinearLayout>
</LinearLayout>

行类型 2

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/dialog_background"
    android:orientation="vertical"
    android:paddingBottom="16dp"
    android:paddingTop="16dp">

    <TextView
        android:id="@+id/graduate_question"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="15dp"
        android:text="1. Here comes questions?"
        android:textSize="13dp" />

    <EditText
        android:id="@+id/graduate_comment"
        style="@style/GraduateTextBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:background="@drawable/textbox"
        android:ems="10"
        android:hint="Comment"
        android:inputType="textMultiLine"
        android:lines="3" />
    </LinearLayout>

行类型 3:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/dialog_background"
    android:orientation="vertical"
    android:paddingBottom="16dp"
    android:paddingTop="16dp">

    <TextView
        android:id="@+id/graduate_question"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="15dp"
        android:text="1. Here comes questions?"
        android:textSize="15dp"
        android:textColor="@color/primary_dark"/>
</LinearLayout>

如何检索主片段中提交的数据?

谢谢

【问题讨论】:

    标签: java android listview android-fragments


    【解决方案1】:

    为此,您可以创建一个具有行值的自定义类,例如,

    Arraylist<CustomClass> mnewList = new ArrayList();
    Class CustomClass
    {
        private String edValues,
        private int rowId,
    }
    

    等等

    然后在将当前选定项目添加/删除到该自定义列表中的单选按钮中实现更改侦听器。

    【讨论】:

    • 谢谢。你能解释一下吗?我是android开发的新手。我正在进行一项调查,其中提出了不同的问题。其中一些问题可以通过选择单选按钮来回答,而另一些问题可以通过制作 cmets 来回答。我创建的。现在我想要的是,当用户按下提交按钮时,我想收集所有选中的单选按钮和 cmets 并将它们发布到服务器。我怎么能在提交点击 .listView.getCheckedItemPositions() 返回一个空数组。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-22
    • 1970-01-01
    • 2016-05-19
    • 2011-09-16
    • 1970-01-01
    相关资源
    最近更新 更多