0

I'm a begginer in Android, and I would like to update my main, my principal view after select a value from a spinner.

public class MainActivity extends Activity {
        ArrayAdapter<String> adapter = null;
        Spinner spinnerMois, spinnerAnnee;
        DateAdapter dataAdapterMois , dataAdapterYear;spinnerMois = (Spinner) findViewById(R.id.spinnerMois);
        dataAdapterMois = new DateAdapter(this,
        android.R.layout.simple_spinner_item, loadMonth());
        dataAdapterMois.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerMois.setAdapter(dataAdapterMois);

        spinnerAnnee = (Spinner) findViewById(R.id.spinnerAnnee);
        dataAdapterYear = new DateAdapter(this, android.R.layout.simple_spinner_item, loadYear());
    dataAdapterYear.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnerAnnee.setAdapter(dataAdapterYear);
    values = depenseBDD.getAllDepense();
    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, values);
    //Here the values should be update by my spinners

}

My "Depense" is an objet :

public class Depense {
int id;
String dateDepense;
float montant;
String categ;}

I would like, by the 2spinners who ask the month and year to chose to update my main and diplay only the "depense" with to good month and year... And to be honest, I dont really know how to do. Have u some suggestions to do ?

Saori
  • 3
  • 5

2 Answers2

0

basically, its pretty easy. you could use "onItemSelect" like this /how-can-i-use-onitemselected-in-android

and it will look something like this:

public class spinner extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

Spinner year;
TextView dateView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_spinner);

    dateView = (TextView) findViewById(R.id.date);

    year = (Spinner) findViewById(R.id.anneeSpinner);
    year.setOnItemSelectedListener(this);
}



@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
    dateView.setText(adapterView.getItemAtPosition(i).toString());
}

@Override
public void onNothingSelected(AdapterView<?> adapterView) {

}
}

and for layout:

    <Spinner
    android:id="@+id/anneeSpinner"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:entries="@array/year"/>

<TextView
    android:id="@+id/date"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="helloo"/>

and just add another spinner and textview for the month. and then you could add the values to main

Community
  • 1
  • 1
Yehuda Clinton
  • 375
  • 4
  • 12
0

Add OnItemClickListener on your Spinner.

spinnerMois.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            //write your code here to update view
        }
    });
DevThapa
  • 173
  • 2
  • 12
  • Okay Thx ! There is a difference with ur answer and the one before you ? I mean, one is better than a another or both are good ? – Saori Feb 01 '17 at 18:39