如何将RecyclerView中的mysql数据库中的数据检索到RadioButton中

时间:2018-04-12 17:54:06

标签: android mysql android-recyclerview

我使用jdbc连接和mysql数据库创建RecyclerView。在RecyclerView内部,我将数据库中的数据上传到一个TextView和三个radioButtons。一切正常,但我的问题是它只显示第一个RadioButton,第二个和第三个没有任何东西。

这是我的java代码:

public class GererVoteInvite extends AppCompatActivity {
    private ArrayList<VoteItem> itemArrayList;  //List items Array
    private MyAppAdapter myAppAdapter; //Array Adapter
    private RecyclerView recyclerView; //RecyclerView
    private RecyclerView.LayoutManager mLayoutManager;
    private boolean success = false; // boolean

    private static final String DB_URL = "jdbc:mysql://10.0.2.2/mvoting_db_1.0.0.0";
    //    private static final String DB_URL = "jdbc:mysql://172.16.24.25/mvoting_db_1.0.0.0"; //"jdbc:mysql://DATABASE_IP/DATABASE_NAME";
    private static final String USER = "mootaz";
    private static final String PASS = "mootaz";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gerer_vote_invite);

        recyclerView = (RecyclerView) findViewById(R.id.recyclerView); //REcyclerview Declaration
        recyclerView.setHasFixedSize(true);
        // use a linear layout manager
        mLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(mLayoutManager);

        itemArrayList = new ArrayList<VoteItem>(); // Arraylist Initialization

        // Calling Async Task
        SyncData orderData = new SyncData();
        orderData.execute("");
    }

    // Async Task has three overrided methods,
    private class SyncData extends AsyncTask<String, String, String> {
        String msg = "Internet/DB_Credentials/Windows_FireWall_TurnOn Error, See Android Monitor in the bottom For details!";
        ProgressDialog progress;

        //Starts the progress dailog
        @Override
        protected void onPreExecute() {
            progress = ProgressDialog.show(GererVoteInvite.this, "Synchronising",
                    "RecyclerView Loading! Please Wait...", true);
        }

        // Connect to the database, write query and add items to array list
        @Override
        protected String doInBackground(String... strings) {
            try {
                Class.forName("com.mysql.jdbc.Driver");

                Connection conn = DriverManager.getConnection(DB_URL, USER, PASS); //Connection Object

                if (conn == null) {
                    success = false;
                } else {
                    // Change below query according to your own database.
                    String query = "SELECT lib_probleme , lib_solution_1 , lib_solution_2 , lib_solution_3  FROM reunion JOIN probleme JOIN solution WHERE reunion.id_reunion=probleme.id_reunion and probleme.id_probleme=solution.id_probleme ;";
                    Statement stmt = conn.createStatement();
                    java.sql.ResultSet rs = stmt.executeQuery(query);

                    // if resultset not null, I add items to itemArraylist using class created
                    if (rs != null) {
                        while (rs.next()) {
                            try {
                                itemArrayList.add(new VoteItem(rs.getString("lib_probleme"), rs.getString("lib_solution_1"), rs.getString("lib_solution_2"), rs.getString("lib_solution_3")));
                            } catch (Exception ex) {
                                ex.printStackTrace();
                            }
                        }
                        msg = "Found";
                        success = true;
                    } else {
                        msg = "No Data found!";
                        success = false;
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                Writer writer = new StringWriter();
                e.printStackTrace(new PrintWriter(writer));
                msg = writer.toString();
                success = false;
            }
            return msg;
        }

        // disimissing progress dialoge, showing error and setting up my gridview
        @Override
        protected void onPostExecute(String msg) {
            progress.dismiss();
            Toast.makeText(GererVoteInvite.this, msg + "", Toast.LENGTH_LONG).show();
            if (success == false) {
            } else {
                try {
                    myAppAdapter = new MyAppAdapter(itemArrayList, GererVoteInvite.this);
                    recyclerView.setAdapter(myAppAdapter);
                } catch (Exception ex) {

                }

            }
        }
    }

    public class MyAppAdapter extends RecyclerView.Adapter<MyAppAdapter.ViewHolder> {
        private List<VoteItem> values;
        public Context context;

        public class ViewHolder extends RecyclerView.ViewHolder {

            public TextView txtProbleme;
            public RadioGroup group;
            public RadioButton txtSol1;
            public RadioButton txtSol2;
            public RadioButton txtSol3;
            public View layout;

            public ViewHolder(View v) {
                super(v);
                layout = v;
                group = (RadioGroup) v.findViewById(R.id.radio_group);
                txtProbleme = (TextView) v.findViewById(R.id.tv_probleme);
                txtSol1 = (RadioButton) v.findViewById(R.id.rb_1);
                txtSol2 = (RadioButton) v.findViewById(R.id.rb_2);
                txtSol3 = (RadioButton) v.findViewById(R.id.rb_3);
            }
        }

        // Constructor
        public MyAppAdapter(List<VoteItem> myDataset, Context context) {
            values = myDataset;
            this.context = context;
        }

        // Create new views (invoked by the layout manager) and inflates
        @Override
        public MyAppAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            // create a new view
            LayoutInflater inflater = LayoutInflater.from(parent.getContext());
            View v = inflater.inflate(R.layout.vote_view, parent, false);
            ViewHolder vh = new ViewHolder(v);
            return vh;
        }

        // Binding items to the view
        @Override
        public void onBindViewHolder(ViewHolder holder, final int position) {

            final VoteItem voteItem = values.get(position);
            holder.txtProbleme.setText(voteItem.getProbleme());
            holder.group.setTag(position);
            holder.txtSol1.setText(voteItem.getSol1());
            holder.txtSol2.setText(voteItem.getSol2());
            holder.txtSol3.setText(voteItem.getSol3());

        }

        // get item count returns the list item count
        @Override
        public int getItemCount() {
            return values.size();
        }
    }
}

这是我的layout.xml,其中包含RecyclerView

的项目
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/ac"
    android:layout_width="match_parent"
    android:layout_height="205dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:padding="5dp">

    <android.support.v7.widget.CardView
        android:id="@+id/vote_card"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="7dp"
        android:layout_marginRight="7dp"
        android:layout_marginTop="10dp"
        card_view:cardBackgroundColor="@color/white"
        card_view:cardCornerRadius="10dp"
        card_view:cardElevation="5dp">


        <TextView
            android:id="@+id/tv_probleme"
            android:layout_width="fill_parent"
            android:layout_height="40dp"
            android:layout_marginTop="30dp"
            android:paddingLeft="15dp"
            android:paddingRight="15dp"
            android:text="Probleme :"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textSize="16dp" />

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

            <RadioButton
                android:id="@+id/rb_1"
                android:layout_width="fill_parent"
                android:layout_height="40dp"
                android:layout_marginTop="60dp"
                android:text="Solution 1" />

            <RadioButton
                android:id="@+id/rb_2"
                android:layout_width="fill_parent"
                android:layout_height="40dp"
                android:text="Solution 2" />

            <RadioButton
                android:id="@+id/rb_3"
                android:layout_width="fill_parent"
                android:layout_height="40dp"
                android:text="Solution 3" />
        </RadioGroup>
    </android.support.v7.widget.CardView>
</RelativeLayout>

以下是我的应用

的屏幕截图

[1]: https://i.stack.imgur.com/p7tIl.png

0 个答案:

没有答案