Android spinner populating using arraylist

前端 未结 2 913
温柔的废话
温柔的废话 2021-01-22 17:51

im to populate spinner using arraylist , and that arraylist actully holds data from the file having android application data stored already for t=my application, im pasting the

相关标签:
2条回答
  • 2021-01-22 18:43

    Try to initialize your spinner adapter with array in constructor.

    String[] wee = list2.toArray(new String[list2.size()]);
    final String[] str={"Report 1","Report 2","Report 3","Report 4","Report 5"};
    ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(
             this, android.R.layout.simple_spinner_item, wee);
    spinnerArrayAdapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
    
    // Spinner spinYear = (Spinner)findViewById(R.id.spin);
    spin.setAdapter(spinnerArrayAdapter);
    
    0 讨论(0)
  • 2021-01-22 18:52
    private void setSpinner() {
        spinner = (Spinner) findViewById(R.id.spinner_size);
    
        Intent intent = getIntent();
    
        ArrayList<String> sizes = null;
    
        if (intent != null) {
          // Receiving ArrayList in Another Activity where "sizeList" is the Key
            sizes = intent.getStringArrayListExtra("SizeList");
        }
    
        if (sizes != null) {
    
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, sizes);
    
            spinner.setAdapter(adapter);
    
            // Spinner click listener
            spinner.setOnItemSelectedListener(this);
        }
    
    
    }
    

    Call setSpinner() method from OnCreate() Method. You can directly pass ArrayList as a parameter, you don't need to convert into String[].

    0 讨论(0)
提交回复
热议问题