上下滚动listview时出现问题

yws3nbqq  于 2021-07-12  发布在  Java
关注(0)|答案(2)|浏览(274)

微调器在滚动listview时自动重置值。但如果我的项目很少,他们可以在屏幕上没有滚动我的应用程序完美地工作。单击按钮时,我检查listview的数字选择器的值。任何帮助都将不胜感激。提前谢谢。
我的适配器:

public class ProductListAdapterforListView extends BaseAdapter {
    private Context mContext;
    private List<piatto> mProductList;
    Vibrator vibe;
    MediaPlayer mp ;
    //Constructor

    public ProductListAdapterforListView(Context mContext, List<piatto> mProductList) {
        this.mContext = mContext;
        this.mProductList = mProductList;
    }
    @Override
    public int getViewTypeCount() {

        return getCount();
    }

    @Override
    public int getItemViewType(int position) {

        return position;
    }

    @Override
    public int getCount() {
        return mProductList.size();
    }

    @Override
    public Object getItem(int position) {
        return mProductList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = View.inflate(mContext, R.layout.row_data_list, null);

        TextView n = (TextView)v.findViewById(R.id.namepiatto);
        TextView p = (TextView)v.findViewById(R.id.prezzopiatto);
        ImageView i = (ImageView) v.findViewById(R.id.imagepiatto);
        com.shawnlin.numberpicker.NumberPicker numberPicker =v.findViewById(R.id.number_picker);

        n.setText(mProductList.get(position).getNome());
        p.setText(mProductList.get(position).getPrezzo()+" €");
        Picasso.get().load(mProductList.get(position).getImmagine()).into(i);

        numberPicker.setOnScrollListener(new NumberPicker.OnScrollListener() {
            @Override
            public void onScrollStateChange(NumberPicker view1, int scrollState) {
                Vibrator vi = (Vibrator) view1.getContext().getSystemService(Context.VIBRATOR_SERVICE);
                vi.vibrate(12);
                mp = MediaPlayer.create(view1.getContext(),R.raw.sound);
                mp.start();
            }
        });

        return v;
    }

}
ha5z0ras

ha5z0ras1#

在setonscrolllistener上创建一个对象似乎不是一个好主意,它在上下滚动期间被多次调用。如果你能提供更多的细节,那就太好了。

3okqufwl

3okqufwl2#

我试图通过以下方式获取单击按钮后每个项目的数字选择器值:

@Override
    public void onClick(View view) {
        if (view.getId()==R.id.buttonnext){
            save("", "totaledolci");
            save("", "listadolci");

            Float totale = (float) 0.0;
            String listapiatti="";

            for (int i = 0; i < listView.getChildCount(); i++) {
                view = listView.getChildAt(i);

                TextView n = view.findViewById(R.id.namepiatto);
                String nome = n.getText().toString();

                TextView p = view.findViewById(R.id.prezzopiatto);
                String pricestr = p.getText().toString();
                String[] prezzo = pricestr.split(" ");
                Float price = Float.valueOf(prezzo[0]);

                com.shawnlin.numberpicker.NumberPicker numberPicker = (com.shawnlin.numberpicker.NumberPicker) view.findViewById(R.id.number_picker);
                int value = numberPicker.getValue();

                if (value != 0) {
                    totale = totale + (price * value);
                    listapiatti = listapiatti + nome + ": " + value + ", ";
                }
            }
            String finale=String.valueOf(totale);

            save(finale, "totaledolci");
            save(listapiatti, "listadolci");

            startActivity(new Intent(carrello3.this, ordine.class));
        }

相关问题