如何在 Spinner 中显示具体金额?

How To Display Specific Amount in Spinner?

我想在另一个页面上使用 putExtra() 获取信息,我可以做到,但我唯一的问题始终是默认值第一个值 sample_array 但我想默认使用 classDayIntent 值显示在旋转器中

提示:在 Spinner 中收到的 putExtra() 在那里

第一个Activity:

Intent intent = new Intent();

intent.putExtra("className" , className);
intent.putExtra("uniName" , uniName);
intent.putExtra("classDay" , classDay);

intent.setClass(context , AddNewClass.class);

context.startActivity(intent);

第二个Activity:

Spinner spinner = (Spinner)findViewById(R.id.spinnerDay);

String className = getIntent().getStringExtra("className");
String uniName = getIntent().getStringExtra("uniName");
String classDayInent = getIntent().getStringExtra("classDay"); // How To Display classDayInent In Spinner....

className_EditeText.setText(className);
uniName_EditeText.setText(uniName);

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this , R.array.sample_array , android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        String classDay = parent.getSelectedItem().toString();
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
    }
});

字符串:

<resources>
<string name="app_name">ClassManager</string>
<string-array name="sample_array">
    <item>شنبه</item>
    <item>یکشنبه</item>
    <item>دوشنبه</item>
    <item>سه شنبه</item>
    <item>چهارشنبه</item>
    <item>پنجشنبه</item>
    <item>جمعه</item>
</string-array>

可以使用适配器的add功能。 由于 CharSequence 是一个 interface,而 String class 实现了 CharSequence,因此您可以直接将字符串添加到适配器

adapter.add(classDayInent);
spinner.setAdapter(adapter);

另一个问题是,createFromResources 创建了一个不可变列表,这意味着它无法添加更多项目。

解决方案:使用可变列表创建您自己的适配器

List<String> list = new ArrayList<>(Arrays.asList(getResources().getStringArray(R.array.sample_array)));

ArrayAdapter<String> adapter = new ArrayAdapter<>(this , android.R.layout.simple_spinner_item,list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
list.add(0,classDayIntent);// to add received element at first position to display
spinner.setAdapter(adapter);

您可以获得 xml 数组列表,然后添加到其中:

List<String> ar = new LinkedList<String>(Arrays.asList(getResources().getStringArray(R.array.sample_array)));
    ar.add("test");

我找到了正确答案:

String classDayInent = getIntent().getStringExtra("classDay"); 

 List<String> list = new ArrayList<String>(Arrays.asList(getResources().getStringArray(R.array.sample_array)));

    ArrayAdapter<String> adapter = new ArrayAdapter<>(this , android.R.layout.simple_spinner_item,list);

    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        //find classDayIntent from List For get Position To Display

        for (int i = 0; i < list.size(); i++)
        {
            if (classDayIntent.equals(list.get(i)))
            {
                spinner.setSelection(i);
            }
        }

    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
        {
            classDay = parent.getSelectedItem().toString();
        }

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


        }
    });