从可扩展列表视图获取所有复选框,单选按钮,评级:android

时间:2014-05-20 21:08:56

标签: android android-layout android-listview

我有一个可扩展的列表视图,其中包含我们拥有的每条记录的简短调查(用户插入数据的3-4个字段)。然后我们有一个按钮,他们可以点击将数据提交给我们的服务器。当他们按下按钮时,我需要它来获取调查中的所有数据并将其发送到我们的网站。我发现了它的发送部分;我正在努力的是抓住调查中的所有数据。

我查看了许多相关帖子/ google结果,并尝试实施和调整他们的listviews解决方案到扩展列表视图,但我无法让他们中的任何一个工作。我花了几个小时试图找到一些可行的解决方案,但唉,我无法弄明白。

我认为我的问题的部分原因是我不确定儿童观点如何为可扩展列表视图发挥作用。无论如何,下面是我的代码,希望比我聪明的人可以帮助我解决这个问题。

所以我有我的ExpandableListView fragment_survey.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    tools:context="edu.ucr.aum.fragments.SurveyFragment$PlaceholderFragment">

    <ExpandableListView
        android:id="@+id/list_view_survey_list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/rowPaddingMedium"
        android:layout_marginRight="@dimen/rowPaddingMedium"
        android:divider="@null"
        android:dividerHeight="0dp"
        android:clipToPadding="false"
        android:scrollbarStyle="outsideOverlay" />

    <TextView
        android:id="@+id/tvResponseCode"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/list_view_survey_list"
        android:text="Response Code: "/>

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Submit Survey"
        android:layout_below="@+id/tvResponseCode"
        android:id="@+id/btnSubmitSurvey" />

</RelativeLayout>

这是我的行xml list_view_survey_row.xml:

    <TextView
        android:id="@+id/tvSurveyTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/surveyTitle" />

    <TextView
        android:id="@+id/tvSurveyShared"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:text="@string/surveyShared"
        android:layout_below="@+id/tvSurveyTitle"
        />

    <RadioGroup
        android:id="@+id/radioShared"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tvSurveyShared">

    <RadioButton
            android:id="@+id/radioButtonYes"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/yes"
            android:focusable="true"/>

        <RadioButton
            android:id="@+id/radioButtonNo"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/no"
            android:focusable="true"/>

    </RadioGroup>

<TextView
        android:id="@+id/tvSurveyReason"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/surveyReason"
        android:layout_below="@+id/radioShared" />

    <TextView
        android:id="@+id/tvSurveyRating"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:text="@string/surveyRating"
        android:layout_below="@+id/tvSurveyReason" />

    <CheckBox
        android:id="@+id/checkBoxInterest"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/surveyInterestPositive"
        android:layout_below="@+id/tvSurveyRating"
        android:focusable="true" />

    <CheckBox
        android:id="@+id/checkBoxPrivacy"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/surveyShareLevelPositive"
        android:layout_below="@+id/checkBoxInterest"
        android:focusable="true" />

    <RatingBar
        android:id="@+id/ratingBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/checkBoxPrivacy"
        android:focusable="true" />

    <View android:layout_width="fill_parent"
        android:layout_height="2dip"
        android:background="#FF00FF00"
        android:layout_below="@+id/ratingBar" />

然后我有一个片段(fragment_survey.java),它将一个onclicklistener添加到fragment_survey.xml文件中的按钮(btnSubmitSurvey)。 onClickListener()函数调用两个方法(一个用于获取数据,一个用于发送该数据)。

    this.buttonSubmit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            getData(v);submitSurvey();
        }
    });


public void getData(View view) {
//get data here
}

public void submitSurvey() {
    // Create a new HttpClient and Post Header
    String url;
    if(Singletons.Debug.debug) {
        url = "url for postback";
    } else {
        url = "url for postback";
    }

    NetworkAsyncTask nat = new NetworkAsyncTask();
    try {
        String response = nat.execute(url).get();
        //Log.d(TAG, "Response: " + response);
    } catch(ExecutionException e) {

    } catch(InterruptedException e) {

    }
}

2 个答案:

答案 0 :(得分:0)

你应该创建一个像数组一样的数据结构来存储调查数据,我会按照组和/或子位置对数据进行索引。然后你应该为每个问题输入添加监听器,当问题回答是,在适当的位置将答案添加到该数据结构中。然后,当您按下提交按钮时,它可以迭代该数据结构,提示未答复的问题或填写默认值,将它们全部收集起来然后提交。

基本上没有简单/内置的方法来访问我能够找到的动态/可重用行的各行。当哪一行的上下文很重要时,我发现最好的方法是拥有自己的数据结构,使用组/子位置来查找正在按下的按钮,因为每行都有一个具有相同id的按钮。我还在创建时使用setHint将组位置/索引存储在按钮中,以便在侦听器的上下文中我可以确定位置。

答案 1 :(得分:0)

我必须通过一个可怕的黑客工作来做到这一点,但它确实有效。我为每一行添加了一个按钮,允许用户“保存”#34;排。当他们保存行时,数据将保存到我的数据结构中。不幸的是为了让这个工作正常,我不得不创建一个自定义的OnClickListener,它将视图作为参数,从那里我可以从视图中删除元素。

它不是一个优雅或好的解决方案;但它确实有效。

相关问题