mardi 17 octobre 2017

TextView not updated

I have a text view that is supposed to change as some checkboxes are clicked. A user has a certain amount of free points with which to purchase attributes. When a checkbox is clicked the points are decreased. When a checkbox is unchecked, the points it cost to check it are supposed to be refunded to the total free points, but that's not the case in the current code that I have. The code for the checkboxes is straightforward:

<RelativeLayout
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:id="@+id/class_container"
   android:layout_below="@id/stats_container"
   android:layout_alignParentLeft="true"
   android:layout_alignParentStart="true">
   <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="@string/class_select"
       android:textSize="24sp"
       android:id="@+id/class_description"
       android:layout_alignParentTop="true"
       android:layout_centerHorizontal="true"/>
   <CheckBox
       android:layout_below="@id/class_description"
       android:id="@+id/checkbox_noble"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="@string/class_noble"
       android:onClick="creationClassClick"/>
   <CheckBox
       android:layout_below="@id/class_description"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:id="@+id/checkbox_warrior"
       android:text="@string/class_warrior"
       android:layout_toEndOf="@+id/checkbox_noble"
       android:layout_toRightOf="@+id/checkbox_noble"
       android:onClick="creationClassClick"/>
   <CheckBox
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:id="@+id/checkbox_wizard"
       android:text="@string/class_wizard"
       android:layout_below="@+id/checkbox_noble"
       android:onClick="creationClassClick"/>
   <CheckBox
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:id="@+id/checkbox_trader"
       android:text="@string/class_trader"
       android:layout_below="@id/checkbox_warrior"
       android:layout_toEndOf="@+id/checkbox_wizard"
       android:layout_toRightOf="@+id/checkbox_wizard"
       android:onClick="creationClassClick"/>
</RelativeLayout>

The relevant methods here are as follows:

public void pointChange(final int amt){
        Runnable updateTextView = new Runnable() {
            @Override
            public void run() {
                TextView tv = findViewById(R.id.free_points);
                tv.setText(String.valueOf(amt));
            }
        };
        runOnUiThread(updateTextView);
    }

    public void creationClassClick(View v){
        int freePoints = Integer.parseInt(freePts.getText().toString());
        if(lastPointsUsed > 0){
            int addedBack = freePoints + lastPointsUsed;
            pointChange(addedBack);
            freePoints = Integer.parseInt(freePts.getText().toString());
            lastPointsUsed = 0;
        }

        String targetFull = A.getResources().getResourceEntryName(v.getId());
        String[] target = targetFull.split("_");

        boolean query;
        int cost = 1;
        int error;
        int cause;

        switch(target[1]){
            case "noble":
                warriorCheck.setChecked(false);
                wizardCheck.setChecked(false);
                traderCheck.setChecked(false);
                cost = 12;
                break;

            case "warrior":
                wizardCheck.setChecked(false);
                nobleCheck.setChecked(false);
                traderCheck.setChecked(false);
                cost = 8;
                break;

            case "wizard":
                warriorCheck.setChecked(false);
                nobleCheck.setChecked(false);
                traderCheck.setChecked(false);
                cost = 9;
                break;

            case "trader":
                warriorCheck.setChecked(false);
                wizardCheck.setChecked(false);
                nobleCheck.setChecked(false);
                cost = 10;
                break;
        }

        lastPointsUsed = cost;
        query = (freePoints - cost < 0);
        error = R.string.creation_no_points;
        cause = freePoints - cost;

        if(query){
            new Modal(app, true, R.string.oops, error, 0, 0);
        }else{
            this.classType = target[1];
            pointChange(cause);
        }
    }

When i use the pointChange() method from any other method in this activity, it works fine. Its only when unchecking a checkbox that it fails. I've searched google and SO for the answer, and the best I could come up with is the runnable set to runonuithread, but it doesn't work either. Any help would be greatly appreciated.




Aucun commentaire:

Enregistrer un commentaire