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 {
}
}
});
Finally i solved it correctly
first i needed to create php file where it checks in which state
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
then i call it inside onBindViewHolder
hope this finds anyone in need of this .. it took my 3 days to figure out the logic