如何在单次操作中将位于回收组视图中的单选按钮组中的单选按钮中的数据发送到服务器中

时间:2018-08-03 12:36:02

标签: android android-recyclerview

我正在开发一个测验应用程序,因为有5个问题,所以我使用了回收站视图来获取5个问题,因此我需要用户在单击“提交”时在每个5个回收站视图中选择一个选项(单选按钮)。数据库

xml(“回收者”视图的布局):

<RelativeLayout
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:padding="5dp"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardCornerRadius="8dp"
        android:background="#e6e6e6"
        android:elevation="1dp"
        android:padding="5dp"
        >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"

            >
        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="match_parent"
            android:layout_height="260dp"
            android:scaleType="fitXY"
            app:srcCompat="@drawable/img"
            />

            <RadioGroup
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="15dp"
                android:id="@+id/radioGroupImage">


                <RadioButton
                    android:id="@+id/radioButton"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:buttonTint="@color/colorPrimary"
                    android:text="RadioButton" />

                <RadioButton
                    android:id="@+id/radioButton2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:buttonTint="@color/colorPrimary"
                    android:text="RadioButton" />


                <RadioButton
                    android:id="@+id/radioButton3"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:buttonTint="@color/colorPrimary"
                    android:text="RadioButton" />

                <RadioButton
                    android:id="@+id/radioButton4"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:buttonTint="@color/colorPrimary"
                    android:text="RadioButton" />

            </RadioGroup>

        </LinearLayout>

    </android.support.v7.widget.CardView>
</RelativeLayout>

适配器类:

package com.accolade.eventify;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.bumptech.glide.Glide;

import java.util.List;
public class ImageQuizAdapter extends RecyclerView.Adapter<ImageQuizAdapter.ImageQuizViewHolder> {
    private RadioButton lastCheckedRB = null;

    int flag;
    private Context mCtx;
    private List<ImageQuizModel> quizList;
    private RadioGroup lastCheckedRadioGroup = null;

    public ImageQuizAdapter (Context mCtx, List<ImageQuizModel> quizList) {
        this.mCtx = mCtx;
        this.quizList = quizList;
    }

    @Override
    public ImageQuizViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(mCtx);
        View view = inflater.inflate(R.layout.layout_recycler_image_quiz, null);
        return new ImageQuizViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ImageQuizViewHolder holder, final int position) {
        ImageQuizModel imageQuizModel=quizList.get(position);
        Glide.with(mCtx)
                .load(imageQuizModel.getImage())
                .into(holder.imageView);
        holder.r1.setText(imageQuizModel.getOp1());
        holder.r2.setText(imageQuizModel.getOp2());
        holder.r3.setText(imageQuizModel.getOp3());
        holder.r4.setText(imageQuizModel.getOp4());









    }


    @Override
    public int getItemCount() {
        return quizList.size();
    }


    public class ImageQuizViewHolder extends RecyclerView.ViewHolder {
        RadioButton r1,r2,r3,r4;
        ImageView imageView;
        Button button;
        RadioGroup radioGroup;
        public ImageQuizViewHolder(View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.imageView2);
            r1 = itemView.findViewById(R.id.radioButton);
            r2 = itemView.findViewById(R.id.radioButton2);
            r3 = itemView.findViewById(R.id.radioButton2);
            r4 = itemView.findViewById(R.id.radioButton3);
            button=itemView.findViewById(R.id.button3);

            radioGroup = itemView.findViewById(R.id.radioGroupImage);
        }
    }
}

主类:

package com.accolade.eventify;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class ImageQuiz extends AppCompatActivity {
    private static final String URL_PRODUCTS = "http://accoladetest.cf/MyApi/MyApiQuizPic.php";

    RecyclerView recyclerView;
    private Toolbar mTopToolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_quiz);
        mTopToolbar = (Toolbar) findViewById(R.id.my_toolbar1);
        setSupportActionBar(mTopToolbar);
        recyclerView = findViewById(R.id.recylcerViewImage);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        new AsyncFetch().execute();

    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_quiz, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_favorite) {
            Toast.makeText(this, "Add Feature", Toast.LENGTH_LONG).show();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }




    private class AsyncFetch extends AsyncTask<String, String, String> {
        public static final int CONNECTION_TIMEOUT = 10000;
        public static final int READ_TIMEOUT = 15000;
        private RecyclerView mRVFishPrice;
        private ImageQuizAdapter mAdapter;
        ProgressDialog pdLoading = new ProgressDialog(ImageQuiz.this);
        HttpURLConnection conn;
        URL url = null;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pdLoading.setMessage("\tLoading...");
            pdLoading.setCancelable(false);
            pdLoading.show();


        }
        @Override
        protected String doInBackground(String... params) {
            try {
                url = new URL("http://accoladetest.cf/MyApi/MyApiQuizPic.php");
            } catch (MalformedURLException e) {
                e.printStackTrace();
                return  e.toString();
            }
            try {
                conn = (HttpURLConnection) url.openConnection();
                conn.setReadTimeout(READ_TIMEOUT);
                conn.setConnectTimeout(CONNECTION_TIMEOUT);
                conn.setRequestMethod("GET");
                conn.setDoOutput(true);
            } catch (IOException e1) {
                e1.printStackTrace();
                return  e1.toString();
            }
            try {
                int response_code = conn.getResponseCode();
                if (response_code == HttpURLConnection.HTTP_OK) {

                    InputStream input = conn.getInputStream();
                    BufferedReader reader = new BufferedReader(new InputStreamReader(input));
                    StringBuilder result = new StringBuilder();
                    String line;
                    while ((line = reader.readLine()) != null) {
                        result.append(line);
                    }
                    return (result.toString());
                } else {
                    return "Buffer Error";
                }
            } catch (IOException e) {
                e.printStackTrace();
                return  e.toString();
            } finally {
                conn.disconnect();
            }
        }

        @Override
        protected void onPostExecute(String result) {
            pdLoading.dismiss();
            List<ImageQuizModel> data=new ArrayList<>();

            try {
                JSONArray jArray = new JSONArray(result);
                String titleTemp,imageTemp;
                for(int i=0;i<jArray.length();i++){
                    JSONObject json_data = jArray.getJSONObject(i);
                    ImageQuizModel imageQuizModel = new ImageQuizModel();
                    imageQuizModel.id= json_data.getInt("id");
                    imageQuizModel.image= json_data.getString("image");
                    imageQuizModel .op1= json_data.getString("op1");
                    imageQuizModel .op2= json_data.getString("op2");
                    imageQuizModel .op3= json_data.getString("op3");
                    imageQuizModel .op4= json_data.getString("op4");
                    data.add(imageQuizModel);
                }
                mRVFishPrice = (RecyclerView)findViewById(R.id.recylcerViewImage);
                mAdapter = new ImageQuizAdapter(ImageQuiz.this, data);
                mRVFishPrice.setAdapter(mAdapter);
                mRVFishPrice.hasFixedSize();
                mRVFishPrice.setLayoutManager(new LinearLayoutManager(ImageQuiz.this));
            } catch (JSONException e) {
                e.printStackTrace();
                Toast.makeText(ImageQuiz.this,"Network Error", Toast.LENGTH_LONG).show();
            }

        }

    }

}

主要布局:

<RelativeLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="#d1d1d1"
    xmlns:android="http://schemas.android.com/apk/res/android">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <android.support.v7.widget.Toolbar
                android:id="@+id/my_toolbar1"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="@color/colorPrimary"
                android:elevation="8dp"
                app:titleTextColor="#fff"/>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical" >

                    <TextView
                        android:id="@+id/textView13"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="Answer these questions"
                        android:textSize="10dp"
                        android:textAlignment="center"
                        android:padding="5dp"
                        android:textColor="#fff"
                        android:background="#05af43"
                        />

                    <android.support.v7.widget.RecyclerView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/recylcerViewImage"
                        android:paddingBottom="60dp"/>



                </LinearLayout>

        </LinearLayout>
    <Button
        android:id="@+id/button3"

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Submit"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginBottom="5dp"
        android:textColor="#585858"
        android:background="@drawable/rounded_corner"

        android:layout_alignParentBottom="true"
        />
    </RelativeLayout>

和数据库表包含5列 1)名称 2)电话号码 3)q1选项 4)q2选项 5)q3选项 6)q4选项

0 个答案:

没有答案