toggle button is checked or not based on info from mysql

94 views Asked by At

so im tring to check data before i press toggle button but i cannot get the right logic to do the process

so i think i should do if checked look at the database set the background resource then i do the if pressed but i doesnt work like it doesnt even change the backgroundResource

i tried to change buttonView to myViewholder.btn.setBackgroundResource but it doesnt work neither

toggle button

       myViewHolder.btn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, final boolean isChecked) {



            if (myViewHolder.btn.isChecked()) {

                sessionManager = new SessionManager(zContext);
                final HashMap<String, String> user = sessionManager.getUserDetail();
                final String user_id2 = user.get(USER_ID);

                String user_id = data.getUser_id();
                final String post_id = data.getPost_id();
                String URL = HOST + "/likes_table.php";

                sessionManager = new SessionManager(zContext);

                AndroidNetworking.get("http://10.0.2.2/Final/gettinglike.php")
                        .setPriority(Priority.LOW)
                        .build()
                        .getAsJSONArray(new JSONArrayRequestListener() {


                            @Override
                            public void onResponse(JSONArray response) {
                                Log.d(TAG, "onResponse: " + response);

                                {

                                    try {
                                        for (int i = 0; i < response.length(); i++) {
                                            final JSONObject data = response.getJSONObject(i);


                                            if (data.getString("post_id").equals(post_id)) {


                                                String like_id = data.getString("like_id");
                                                String user_id = data.getString("user_id").trim();
                                                String post_id = data.getString("post_id");



                                                if (user_id.equals(user_id2)){

                                                    myViewHolder.btn.isChecked();
                                                    buttonView.setBackgroundResource(ic_star_black_24dp_checked);
                                                }
                                                else{
                                                    buttonView.setBackgroundResource(ic_star_border_black_24dp);
                                                }

                                            }


                                        }


                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }
                                }
                            }

                            @Override
                            public void onError(ANError error) {
                                Log.d(TAG, "onError: " + error);
                            }

                        });

                if (myViewHolder.btn.isPressed()){

                }



            }
            else {



            }

        }
    });
1

There are 1 answers

0
Meto Emeto On BEST ANSWER

Finally i solved it correctly

first i needed to create php file where it checks in which state

     $user_id = $_POST['user_id'];
  $post_id = $_POST['post_id'];



 $sql_verify = "SELECT * FROM likes_table WHERE post_id = :POST_ID AND user_id = 
 :USER_ID 
 ";
 $stmt = $PDO->prepare($sql_verify);
 $stmt->bindParam(':POST_ID', $post_id);
 $stmt->bindParam(':USER_ID',$user_id);
 $stmt->execute();

 if ($stmt->rowCount() > 0) {

  $returnApp = array( 'SIGNUP' => 'LIKE_ALREADY_EXIST');
  echo json_encode($returnApp);
  } 

  else {

  $returnApp = array( 'SIGNUP' => 'LIKE_DOESNT_EXIST');
   
  echo json_encode($returnApp);

  }

then i create mothod for calling it with two cases one when toggle button is on and the onther when the toggle button is off

     private void getdata3(final MyViewHolder myViewHolder , final int i){



    final String URL2 = HOST + "/likes_table2.php";
    sessionManager = new SessionManager(zContext);
    final HashMap<String, String> user = sessionManager.getUserDetail();
    final String user_id3 = user.get(USER_ID);
    PostsModelClass data = modelClassList.get(i);

    final String post_id = data.getPost_id();

    Ion.with(zContext)
            .load(URL2)
            .setBodyParameter("user_id",user_id3)
            .setBodyParameter("post_id",post_id)
            .asJsonObject()
            .setCallback(new FutureCallback<JsonObject>() {
                @Override
                public void onCompleted(Exception e, JsonObject result) {

                    try {

                        String RETURN = result.get("SIGNUP").getAsString();

                        switch (RETURN) {


                            case "LIKE_ALREADY_EXIST":
                                final String URL = HOST + "/dislike.php";

                                myViewHolder.btn.setBackgroundResource(ic_star_black_24dp_checked);
                                myViewHolder.btn.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {


                                        Ion.with(zContext)
                                                .load(URL)
                                                .setBodyParameter("post_id", post_id)
                                                .setBodyParameter("user_id",user_id3)
                                                .asJsonObject()
                                                .setCallback(new FutureCallback<JsonObject>() {
                                                    @Override
                                                    public void onCompleted(Exception e, JsonObject result) {

                                                        try {
                                                            String RETURN = result.get("SIGNUP").getAsString();

                                                            switch (RETURN) {
                                                                case "Posted":

                                                                    myViewHolder.btn.setBackgroundResource(ic_star_border_black_24dp);
                                                                    getdata3(myViewHolder,i);
                                                                    modelClassList.clear();
                                                                    newsFeedFragmentUser.getData();



                                                                    break;

                                                                default:

                                                                    break;
                                                            }

                                                        } catch (Exception error) {
                                                        }

                                                    }
                                                });

                                    }
                                });
                                break;

                            case "LIKE_DOESNT_EXIST":

                                myViewHolder.btn.setBackgroundResource(ic_star_border_black_24dp);
                                final String URL3 = HOST + "/likes_table.php";

                                myViewHolder.btn.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {

                                        Ion.with(zContext)
                                                .load(URL3)
                                                .setBodyParameter("post_id", post_id)
                                                .setBodyParameter("user_id",user_id3)
                                                .asJsonObject()
                                                .setCallback(new FutureCallback<JsonObject>() {
                                                    @Override
                                                    public void onCompleted(Exception e, JsonObject result) {

                                                        try {
                                                            String RETURN = result.get("SIGNUP").getAsString();

                                                            switch (RETURN) {
                                                                case "Posted":

                                                                    myViewHolder.btn.setBackgroundResource(ic_star_black_24dp_checked);
                                                                    getdata3(myViewHolder,i);
                                                                    modelClassList.clear();
                                                                    newsFeedFragmentUser.getData();



                                                                    break;

                                                                default:

                                                                    break;
                                                            }

                                                        } catch (Exception error) {
                                                        }

                                                    }
                                                });

                                    }
                                });




                                break;

                            default:
                                Toast.makeText(zContext, "Ops! Error Occurred m", Toast.LENGTH_LONG).show();

                                break;
                        }

                    } catch (Exception error) {
                        Toast.makeText(zContext, "Ops! Error Occured" + error, Toast.LENGTH_LONG).show();

                    }
                }
            });



}

then i call it inside onBindViewHolder

    getdata3(myViewHolder,i);

hope this finds anyone in need of this .. it took my 3 days to figure out the logic