如何在微调器中显示特定金额?

时间:2018-03-27 14:17:47

标签: android android-studio android-intent spinner android-spinner

我希望在另一个页面上使用putExtra()获取信息,我可以这样做,但我唯一的问题始终是默认值第一个值sample_array但我希望默认情况下classDayIntent值显示在微调器中

提示:putExtra()收到的Spinner就是

第一项活动:

Intent intent = new Intent();

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

intent.setClass(context , AddNewClass.class);

context.startActivity(intent);

第二项活动:

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>

3 个答案:

答案 0 :(得分:1)

您可以使用适配器的add功能。 由于CharSequenceinterfaceString类实现了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);

答案 1 :(得分:0)

您可以获取xml arraylist然后添加到:

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

答案 2 :(得分:0)

我找到了正确答案:

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) {


        }
    });