Android如何将从微控制器接收的数据从数组更改为从对象接收

时间:2015-01-20 20:47:59

标签: java android spinner

我的微调器从strings.xml接收数据,但我想改变接收数据的方式,以便从对象中获取数据。我得到了班级队伍和名字。我想在微调器中显示名称。你能帮助我吗?我的类,对象和微调器的代码:

public class DruzynyStatActivity extends ActionBarActivity {
    public class Team {
        public String name;
        public String shoots;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_druzyny_stat);
        getSupportActionBar().hide();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_druzyny_stat, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


    public void cracovia(View view) {
        Intent intent = new Intent(this, CracoviaActivity.class);
        Team cracovia = new Team();
        cracovia.name = "Cracovia";
        String name = cracovia.name;
        intent.putExtra("teamName", name);
        cracovia.shoots = "STRZAŁY: 10";
        String shoots = cracovia.shoots;
        intent.putExtra("shoots", shoots);
        startActivity(intent);

    }
}

和Spinners代码:

public class PorownanieActivity extends Activity implements AdapterView.OnItemSelectedListener {
    Spinner spinner1;
    Spinner spinner;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_porownanie);
        spinner1 = (Spinner) findViewById(R.id.spinner);
        ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.ZESPOŁY, android.R.layout.simple_list_item_1);
        spinner1.setAdapter(adapter);
        spinner1.setOnItemSelectedListener(this);
        spinner = (Spinner) findViewById(R.id.spinner1);
        ArrayAdapter adapter1 = ArrayAdapter.createFromResource(this, R.array.ZESPOŁY, android.R.layout.simple_list_item_1);
        spinner.setAdapter(adapter1);
        spinner.setOnItemSelectedListener(this);


    }
}

1 个答案:

答案 0 :(得分:0)

您可以从对象创建ArrayAdapter并将其设置为Spinner。 您应该看看适配器构造函数,您可以这样写 -

     String list[] = new String[]{"string1","string2"};
     ArrayAdapter<String> adapter= new ArrayAdapter<String>(this,
                android.R.layout.simple_dropdown_item_1line,list);
     spinner.setAdapter(adapter);

您可以从任何类型的Object创建适配器,您应该查看适配器的构造函数。          对于更多自定义和对象列表,您还可以扩展BaseAapter和ArrayAdapter类。