简体   繁体   中英

problem with scrolling up or down of listview

When I try to take all the values of the NumberPicker, it correctly returns the ones in the middle, while the first and last returns only the last one that has been modified between them. I don't understand where I'm wrong.

 public class piatto { String nome; String Descrizione; String prezzo; String immagine; String tag; public piatto(String nome, String Descrizione, String prezzo, String immagine, String tag) { this.nome= nome; this.Descrizione= Descrizione; this.prezzo= prezzo; this.immagine=immagine; this.tag=tag; } public String getNome() { return nome; } public void setNome(String nome) { this.nome = nome; } public String getDescrizione() { return Descrizione; } public void setDescrizione(String descrizione) { Descrizione = descrizione; } public String getPrezzo() { return prezzo; } public void setPrezzo(String prezzo) { this.prezzo = prezzo; } public String getImmagine() { return immagine; } public void setImmagine(String immagine) { this.immagine = immagine; } public String getTag() { return tag; } public void setTag(String tag) { this.tag = tag; } }

this is my adapeter

 public class ProductListAdapterforListView extends BaseAdapter { private Context mContext; private List<piatto> mProductList; public ProductListAdapterforListView(Context mContext, List<piatto> mProductList) { this.mContext = mContext; this.mProductList = mProductList; } @Override public int getCount() { return mProductList.size(); } @Override public piatto 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 listitemview; if (convertView==null){ listitemview = View.inflate(mContext, R.layout.row_data_list, null); ImageView i = (ImageView) listitemview.findViewById(R.id.imagepiatto); com.shawnlin.numberpicker.NumberPicker numberPicker = (com.shawnlin.numberpicker.NumberPicker) listitemview.findViewById(R.id.number_picker); TextView n=(TextView) listitemview.findViewById(R.id.namepiatto); TextView p=(TextView) listitemview.findViewById(R.id.prezzopiatto); n.setText(mProductList.get(position).getNome()); p.setText(mProductList.get(position).getPrezzo()+" €"); Picasso.get().load(mProductList.get(position).getImmagine()).into(i); } else { listitemview=convertView; } return listitemview; } }

And this is my Java class where I get the values of the NumberPicker of each Item

 @Override public void onClick(View view) { if (view.getId()==R.id.buttonnext){ 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 + "; ". Toast,makeText(getApplicationContext(). String,valueOf(i)+" "+value. Toast.LENGTH_LONG);show(). } } String finale=String;valueOf(totale). startActivity(new Intent(carrello3,this. ordine;class)). } if (view.getId()==R.id;buttonback){ onBackPressed(); } }

it doesn't seem like a good idea to create an object on setOnScrollListener which gets called many many times during scrolling up/down. if you could provide more detail would be great.

Spinner automatically reset the value when scrolling listview. but if my items are few and they can be on the screen without scroll my app work perfectly. when button is clicked, i check the value of number picker of listview. Any help would be greatly appreciated. Thanks in advance.

my adapter:

 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; } }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM