简体   繁体   中英

Update my view after select a value in a spinner

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 ?

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

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
        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM