如何使用警告对话框更改微调器值? Android的

时间:2015-06-23 08:52:45

标签: android spinner

有人可以告诉我如何在调用警告对话框后更改微调器值?单击“其他...”时,我正在调用警报对话框。我不知道如何用我在警告对话框中写的新值替换“Other ...”值。

Resources00.cpp

3 个答案:

答案 0 :(得分:0)

首先,您需要替换ListAdapter中的项目。 您可以删除然后插入一个新值,如下所示:

adapterLessonDuration.setNotifyOnChange(false);
adapterLessonDuration.remove(adapterLessonDuration.getItem(position)); 
adapterLessonDuration.insert(userInput, position);
adapterLessonDuration.notifyDataSetChanged();

制作位置参数最终

注意:项目值更改后,您的条件"其他......" .equals(...)将不再有效。

另一种方法 - 将ArrayAdapter重写为您自己的,或者将自定义类的值放入其中,并在项值更改后调用notifyDataSetChanged()。

答案 1 :(得分:0)

.xml file

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.testing.MainActivity" >

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />

</RelativeLayout>

.java file

public class MainActivity extends Activity {

    Spinner spinner;
    String[] name = { "Prakash", "Bhavin", "Chirag", "Kishan", "Sandy" };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

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

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                getApplicationContext(),
                android.R.layout.simple_dropdown_item_1line, name);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);


        spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                    int position, long id) {
                // TODO Auto-generated method stub

                AlertDialog.Builder builder = new AlertDialog.Builder(
                        MainActivity.this);
                builder.setTitle("This is Selected Value " + name[position]);
                builder.setPositiveButton("Yes",
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                // TODO Auto-generated method stub
                                Toast.makeText(getApplicationContext(),
                                        "Yes Button clicked...",
                                        Toast.LENGTH_SHORT).show();

                            }
                        });
                builder.setNegativeButton("No",
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                // TODO Auto-generated method stub
                                Toast.makeText(getApplicationContext(),
                                        "No Button clicked...",
                                        Toast.LENGTH_SHORT).show();

                            }
                        });
                builder.setNeutralButton("Help",
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                // TODO Auto-generated method stub
                                Toast.makeText(getApplicationContext(),
                                        "Help Button clicked...",
                                        Toast.LENGTH_SHORT).show();

                            }
                        });
                builder.show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // TODO Auto-generated method stub

            }
        });

    }


}

答案 2 :(得分:0)

固定它!

问题是来自R.array的arrayList是不可变的:)

    ArrayList<CharSequence> testArrayList = new ArrayList<CharSequence>();
    CharSequence[] strings = getResources().getTextArray(R.array.lessonDuration);
    Collections.addAll(testArrayList, strings);

    final Spinner spinnerLessonDuration = (Spinner) findViewById(R.id.spinnerLessonDuration);
    final ArrayAdapter<CharSequence> adapterLessonDuration = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item, testArrayList);