August August - 5 months ago 28
JSON Question

JSONException: Index 3 out of range - disable button before error

tutorial: JSON Get data from MySql

I followed this tutorial and everything went good. Even though the error not thrown, my app doesn't crash. But how to disable button after view the last array in JSON?

public void onClick(View v) {

if (v == nextBtn) {
if(TRACK<marks.length()){
TRACK++;
}
showData();


}
if (v == prevBtn) {
if(TRACK>0){
TRACK--;
}
showData();

}

}

Answer

EDIT : 1

Try below code:

public void onClick(View v) {

if (v == nextBtn) {
    if(TRACK<marks.length()){

        if(TRACK==0)
        {
             //Enable previous button because you are incrementing the count
             prevBtn.setEnabled(true);
        }

        TRACK++;
        if(TRACK==(marks.length()-1))
        {
             //Disable next button
             v.setEnabled(false);
        }
    }
    showData();

}
if (v == prevBtn) {
    if(TRACK>0){

        if(TRACK==(marks.length()-1))
        {
             //Enable next button because you are decrementing the count
             nextBtn.setEnabled(true);
        }

        TRACK--;
        if(TRACK==0)
        {
             //Disable previous button
             v.setEnabled(false);
        }
    }
    showData();

}

}

EDIT 2:

for making prevButton disabled by default do it like this, Where you are doing findViewById()

prevButton = ()findViewById(); // Your findViewById code..!!
prevButton.setEnabled(false);

Hope this helps..!!

Comments