mercredi 18 novembre 2015

Custom spinner doesn't update value

I have an android spinner with a custom layout and a custom adapter.

When I tap on the spinner it shows a list of CheckButtons. I've associated a CheckboxListener to every checkbox so that when the checkbox is selected I want to change the value displayed by the Spinner.

In the CheckboxListener i take a reference to the spinner's TextView (the text displayed by the spinner). When i use the method setText() in the adapter the value displayed change but when i use that method in the CheckBoxListener the setted text isn't displayed. I don't get why.

Here is part of the code:

onCreate:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_filter_apply);

        //get spinner reference
        Spinner spinnerLane = (Spinner) findViewById(R.id.filt_apply_spinner_lane);

        //create adapter
        SpinnerAdapter adapterLane = new SpinnerAdapter(FilterApplyActivity.this,R.layout.spinner_row_layout,R.array.lanes_array,"Lane");

        //assegna gli adapter
        spinnerLane.setAdapter(adapterLane);

    }

Custom adapter:

class SpinnerAdapter extends ArrayAdapter {

    LayoutInflater inflater;
    String[] data;
    CheckBoxListener chkboxListener;
    TextView displayedText;

    //Constructor
    {.....}

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View layout = inflater.inflate(R.layout.spinner_row_layout, parent, false);
        TextView row = (TextView) layout.findViewById(R.id.spinner_row_txt);
        displayedText = row;
        displayedText.setText("This is a text");
        row.setTypeface(custom_font);
        return layout;
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        View layout = inflater.inflate(R.layout.spinner_row_layout_check, parent, false);
        CheckBox row = (CheckBox) layout.findViewById(R.id.spinner_row_chk);
        row.setTag(position);
        chkboxListener = new CheckBoxListener(displayedText);
        row.setOnCheckedChangeListener(chkboxListener);     
        row.setText(data[position]);
        row.setTypeface(custom_font);
        return layout;
    }
}

Custom listener:

public class CheckBoxListener implements CompoundButton.OnCheckedChangeListener{
    String text;
    TextView txtView;

    public CheckBoxListener(TextView txt){
        txtView = txt;
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        text = "";
        System.out.println("row gettext 2: " + txtView.getText()); //when i tap on a chkbox it prints: "This is a text"
        if(isChecked == true){
            text = "bar";
            txtView.setText(text);
            System.out.println("txt gettext: " + txtView.getText()); //it prints: "bar"
            //txtView.invalidate();
        }
    }
}

spinner_row_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/very_dark_gray"
    android:gravity="center"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/spinner_row_txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="1dp"
        android:textSize="13dp"
        android:text="foo"
        android:textColor="@color/orange_words" />

</LinearLayout>

spinner_row_layout_check.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/very_dark_gray"
    android:gravity="center"
    android:orientation="horizontal" >

    <CheckBox
        android:id="@+id/spinner_row_chk"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="1dp"
        android:textColor="@color/orange_words"
        android:textSize="13dp" />

</LinearLayout>




Aucun commentaire:

Enregistrer un commentaire