如何在选择微调器时更改对象的值?

时间:2013-04-19 00:20:50

标签: android object spinner

我有一个对象:

PanggilOlahraga( nama, met, waktu, kalori )

我有一个微调器,其中包含'waktu'值。

我想在选择微调器值时更改'waktu'值。

@Override
        public View getView(int position, View convertView, ViewGroup parent) {

            ViewHolder holder = null;

            Log.d("ConvertView", String.valueOf(position));

            PanggilOlahraga olahraga = PanggilOlahragaList.get(position);
            if (convertView == null) {

                LayoutInflater vi = (LayoutInflater)getSystemService(
                    Context.LAYOUT_INFLATER_SERVICE);
                    convertView = vi.inflate(R.layout.olahragainet_info, null);

                    holder          = new ViewHolder();
                    holder.nama     = (TextView) convertView.findViewById(R.id.tvNamaOlgaInet);
                    holder.checkbox = (CheckBox) convertView.findViewById(R.id.cbOlga);
                    holder.sp       = (Spinner) convertView.findViewById(R.id.spWaktu);


                    ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(
                                getApplicationContext(), R.layout.my_spinner_textview, items1); 

                    adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                    holder.sp.setAdapter(adapter1);     
                    holder.sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

                        public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {

                            Object item     = parent.getItemAtPosition(pos);
                            txtWaktu        = item.toString();
                            double dwaktu   = Double.parseDouble(txtWaktu);

                            olahraga.setWaktu(dwaktu);

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

                        }
                   });
                    convertView.setTag(holder);

                    // ACTION CLICKED ON CHECK BOX
                    holder.checkbox.setOnClickListener( new View.OnClickListener() {  
                        public void onClick(View v) {  
                            CheckBox cb = (CheckBox) v ;  
                            PanggilOlahraga olahraga = (PanggilOlahraga) cb.getTag();  
                            olahraga.setSelected(cb.isChecked());
                        }  
                    });
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            kalori = olahraga.getMet() * olahraga.getWaktu() / 60.0 * berat;
            holder.nama.setText(olahraga.getNama());
            holder.checkbox.setChecked(olahraga.isSelected());
            holder.checkbox.setTag(olahraga);
            olahraga.setKalori(kalori);

            return convertView;

        }

但是,我在 olahraga.setWaktu(dwaktu)中犯了一个错误。

它说“不能引用在不同方法中定义的内部类中的非变量olahraga”

任何人都可以帮助我?


1 个答案:

答案 0 :(得分:0)

更改

PanggilOlahraga olahraga = PanggilOlahragaList.get(position);

final PanggilOlahraga olahraga = PanggilOlahragaList.get(position);

你可以在你的匿名课程中引用它。