listview不存储我从AlertDialog微调器中带来的项目

时间:2014-10-23 14:11:33

标签: android listview android-listview spinner alertdialog

请帮助我。我有一个带有spinner的alertDialog。我在那里选择项目。此项目发布到listView(它正常工作)。当我把buton,listView项目删除。我如何保存所有物品?

这是我的代码:

    listView = (ListView) findViewById(R.id.listView1);

    // Создаём пустой массив для хранения имен котов


    RadioGroup radiogroup = (RadioGroup) findViewById(R.id.radioGroup1);
    seekbar2 = (SeekBar)findViewById(R.id.seekBar2);
    seekbar2.setOnSeekBarChangeListener(this);

    mTextValue2 = (TextView)findViewById(R.id.textView2);
    mTextValue2.setText("0");

    but1 = (Button)findViewById(R.id.button1);

    radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            // TODO Auto-generated method stub
            switch (checkedId) {
            case R.id.radio1:
                vidmark = 7;
                break;
            case R.id.radio2:
                vidmark = 6;
                break;

            default:
                break;
            }
        }
    });

}

public void onMyDialogClick(View view)  
{  

    final ArrayList<String> drinks = new ArrayList<String>();

    // Создаём адаптер ArrayAdapter, чтобы привязать массив к ListView
    final ArrayAdapter<String> adapterList;
    adapterList = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, drinks);
    // Привяжем массив через адаптер к ListView
    listView.setAdapter(adapterList);

    AlertDialog.Builder Dialog = new AlertDialog.Builder(this);
     Dialog.setTitle("Добавить порцию");
     LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     View dialogView = li.inflate(R.layout.dialog, null);
     Dialog.setView(dialogView);
     final TextView txt = (TextView)dialogView.findViewById(R.id.textDialog1);
     txt.setText("Кол-во: 0г");
     SeekBar seek1 = (SeekBar) dialogView.findViewById(R.id.seekBarDialog1);
     seek1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
     public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser){
         if (progress % 10 == 0) {
             txt.setText("Кол-во: " + progress + "г");
         }
     }

     public void onStartTrackingTouch(SeekBar arg0) {

     }

     public void onStopTrackingTouch(SeekBar seekBar) {

     }

     });


      Dialog.setPositiveButton("Добавить",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        Toast.makeText(getApplicationContext(), selItem, Toast.LENGTH_SHORT).show();
                        drinks.add(0, selItem);
                        adapterList.notifyDataSetChanged();
                    }
                });

      Dialog.setNegativeButton("Закрыть",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {

                    }
                });
        Dialog.show();
        Spinner spinnercategory = (Spinner) dialogView
                .findViewById(R.id.spinnerDialog);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                this, R.array.alco_array, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnercategory.setAdapter(adapter);

        spinnercategory.setOnItemSelectedListener(new OnItemSelectedListener() {

            public void onItemSelected(AdapterView<?> parent, View arg1,
                    int arg2, long arg3) {
                selItem = parent.getSelectedItem().toString();

            }

            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub
            }
        });

    }

1 个答案:

答案 0 :(得分:0)

您可以使用adapterList.add(sellItem)

如果您想将项目插入列表顶部,遗憾的是您必须扩展ArrayAdapter并在那里存储您自己的列表。