0

i have a spinner with 4 value

String[] petak = { "Petak 1", "Petak 2", "Petak 3", "Petak 4" };

Image 1

First i choose the value "Petak 1"

then i choose "foto dan save" to next step

and the value now is

Image 2

because value "Petak 1" is already choosen before, so the value "Petak 1" is dissapear.

now i choose "foto dan save" again

and the value is

Image 3

because value "Petak 1" and "Petak 2" is already choosen before, so the value "Petak 1" and "Petak 2" is dissapear.

The question is how to manipulate value spinner when i choose the value Random , Example : i choose "Petak 2" First ? so the value is show "Petak 1", "Petak 3", "Petak 4"

here is my code

            View spinner1 = (Spinner) findViewById(R.id.namapetak);
            String spinnerPetak=((Spinner) spinner1).getSelectedItem().toString();
            if (spinnerPetak == "Petak 1"){
                String[] petak1 = { "Petak 2", "Petak 3", "Petak 4" };
                // buat spinner manual manufaktur
                Spinner spin3 = (Spinner) findViewById(R.id.namapetak);
                //spin3.setOnItemSelectedListener(this);
                ArrayAdapter<String> aa = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, petak1);
                aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                spin3.setAdapter(aa);
                //update = (Button) findViewById(R.id.update);

                spin3 = (Spinner) findViewById(R.id.namapetak);
                spin3.setOnItemSelectedListener(new OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
                        //Toast.makeText(CPupukApp.this, "Anda Memilih: " + manufaktur[position],Toast.LENGTH_LONG).show();
                    }

                    public void onNothingSelected(AdapterView<?> parent) {

                    }


                    });
                View b = findViewById(R.id.btnPupukQuit);
                b.setVisibility(View.GONE);
                onBackPressed();
                onAttachedToWindow();

            }

            if (spinnerPetak == "Petak 2"){
                String[] petak1 = { "Petak 3", "Petak 4" };
                // buat spinner manual manufaktur
                Spinner spin3 = (Spinner) findViewById(R.id.namapetak);
                //spin3.setOnItemSelectedListener(this);
                ArrayAdapter<String> aa = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, petak1);
                aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                spin3.setAdapter(aa);
                //update = (Button) findViewById(R.id.update);

                spin3 = (Spinner) findViewById(R.id.namapetak);
                spin3.setOnItemSelectedListener(new OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
                        //Toast.makeText(CPupukApp.this, "Anda Memilih: " + manufaktur[position],Toast.LENGTH_LONG).show();
                    }

                    public void onNothingSelected(AdapterView<?> parent) {

                    }


                    });
                View b = findViewById(R.id.btnPupukQuit);
                b.setVisibility(View.GONE);
                onBackPressed();
                onAttachedToWindow();

            }

            if (spinnerPetak == "Petak 3"){
                String[] petak1 = { "Petak 4" };
                // buat spinner manual manufaktur
                Spinner spin3 = (Spinner) findViewById(R.id.namapetak);
                //spin3.setOnItemSelectedListener(this);
                ArrayAdapter<String> aa = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, petak1);
                aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                spin3.setAdapter(aa);
                //update = (Button) findViewById(R.id.update);

                spin3 = (Spinner) findViewById(R.id.namapetak);
                spin3.setOnItemSelectedListener(new OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
                        //Toast.makeText(CPupukApp.this, "Anda Memilih: " + manufaktur[position],Toast.LENGTH_LONG).show();
                    }

                    public void onNothingSelected(AdapterView<?> parent) {

                    }


                    });
                View b = findViewById(R.id.btnPupukQuit);
                b.setVisibility(View.GONE);
                onBackPressed();
                onAttachedToWindow();

            }

Anyone help me?

EDIT

this code work when i put this on button onClick "Foto dan Save", but error when i put to onActivityResult , line switch (v.getId()) can switch add in onActivityResult?

String myData = toList.getSelectedItem().toString();
              int position = dataAdapter.getPosition(myData);

              switch (View.getId()) {



              case R.id.btnPetakFoto:

               if(position >= 0){
                dataAdapter.remove(myData);
                dataAdapter.notifyDataSetChanged();
               }
               else {
                Toast.makeText(getBaseContext(), myData + " not in Spinner", Toast.LENGTH_LONG).show();
               }
               break;
               // More buttons go here (if any) ...

              }

            View b = findViewById(R.id.btnPupukQuit);
            b.setVisibility(View.GONE);
            onBackPressed();
            onAttachedToWindow();
4

2 回答 2

2

使用 ArrayList 来解决您的问题,您还应该考虑使用 switch 而不是一堆 if。

祝你好运

于 2013-03-05T19:26:29.453 回答
1

对,您要做的是使用 anArrayList来保存您的基础数据,然后在您单击Foto dan Save按钮时更新它。

您将如何执行此操作的一个小示例如下:

public class MainActivity extends Activity {
ArrayAdapter<String> spinnerAdapter;
Spinner spinner1;
ArrayList<String> petak;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    petak = new ArrayList<String>();
    petak.add("Petak 1");
    petak.add("Petak 2");
    petak.add("Petak 3");
    petak.add("Petak 4");

    spinner1 = (Spinner) findViewById(R.id.spn_petak);
    spinnerAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, petak);

    spinner1.setAdapter(spinnerAdapter);

    Button foto = (Button)findViewById(R.id.foto);
    foto.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            petak.remove(spinner1.getSelectedItem().toString());
            spinnerAdapter.notifyDataSetChanged();

        }
    });

}

这是自包含代码,因此您可以将其复制到新项目中,它会按预期工作,或者您可以获取重要部分并将它们应用到您的代码中。

基本上我们正在做的是创建一个新的ArrayList并将您的四个Petak值添加到它。然后我们创建一个新SpinnerAdapter的,ArrayList作为参考。

当用户点击fotoString按钮时,我们从底层证券中移除选定的项目ArrayList,然后调用notifyDataSetChanged我们SpinnerAdapter的以更新显示的项目列表。

于 2013-03-06T11:04:34.820 回答