android listviewはedittextデータの乱れ問題を含む

5516 ワード

プロジェクトのlistviewの各itemにedittextがある場合、1つ入力すると下にスクロールし、listviewの多重化メカニズムによりデータが錯乱する現象が発生します.主にedittextのtagを設定することで、データ入力が完了したときのpositionを記録し、配列からpositionを取り出し、次のコードに値を付け直します.
	//    
	public class SellHedgeAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			if (sellMaps == null) {
				return 0;
			}
			return sellMaps.size();
		}

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

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

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder viewHolder = null;
			if (convertView == null) {
				viewHolder = new ViewHolder();
				convertView = initViewHolder(viewHolder);
				convertView.setTag(viewHolder);
			} else {

				viewHolder = (ViewHolder) convertView.getTag();
				//   edit      ,        ,    
				viewHolder.listview_item_edit.setTag(position);
			}
			refreshValue(position, viewHolder);
			return convertView;
		}

		private void refreshValue(final int position,
				final ViewHolder viewHolder) {

			hedge_inst_name.setText(instrName);

			viewHolder.ticket.setText(sellMaps.get(position)
					.get(HEDGE_TicketStr).toString());

			String d = DecimalUtil.formatAmount(Double.parseDouble((sellMaps
					.get(position).get(HEDGE_AMOUNT).toString())));
			viewHolder.amount.setText(d);

			digit = InstrumentUtil.getDigitByInstrument(instrName);
			double closePrice = Double.parseDouble(sellMaps.get(position)
					.get(HEDGE_CLOSEPIRCE).toString());
			viewHolder.closePrice.setText(DecimalUtil.formatDoubleParam(
					closePrice, digit));

			viewHolder.listview_item_edit.setTag(position);
			viewHolder.listview_item_edit.setText(left_edit_maps.get(position));
			//  editText  TextChangedListener,         hashMap
			//            position  
			viewHolder.listview_item_edit
					.addTextChangedListener(new TextWatcher() {
						@Override
						public void onTextChanged(CharSequence s, int start,
								int before, int count) {
						}

						@Override
						public void beforeTextChanged(CharSequence s,
								int start, int count, int after) {
						}

						@Override
						public void afterTextChanged(Editable s) {
							int position = (Integer) viewHolder.listview_item_edit
									.getTag();
							//  editText        HashMap 
							left_edit_maps.put(position, s.toString());

						}
					});

		}

		private View initViewHolder(ViewHolder viewHolder) {
			View convertView;
			convertView = getActivity().getLayoutInflater().inflate(
					R.layout.activity_openposition_hedge_item, null);

			viewHolder.ticket = (TextView) convertView
					.findViewById(R.id.ticket);
			viewHolder.ticket.setTypeface(faceroman);

			viewHolder.amount = (TextView) convertView
					.findViewById(R.id.amount);
			viewHolder.amount.setTypeface(faceroman);

			viewHolder.closePrice = (TextView) convertView
					.findViewById(R.id.closePrice);
			viewHolder.closePrice.setTypeface(faceroman);

			viewHolder.listview_item_edit = (EditText) convertView
					.findViewById(R.id.listview_item_edit);

			return convertView;
		}
	}

	public class ViewHolder {

		TextView ticket;
		TextView amount;
		TextView closePrice;
		EditText listview_item_edit;

	}

この先輩のアドバイスに感謝します.http://www.bubuko.com/infodetail-359182.htmlあ、ネット上の他の解決策は複雑すぎて、これは比較的簡単明瞭であるべきです.
==================================================
edittextを入力すると、カーソルが前に走っていることがわかります.フォーカスを取得し、値を変更するカーソルを再設定する必要があります.コードは次のとおりです.
viewHolder.listview_item_edit
					.addTextChangedListener(new TextWatcher() {
						@Override
						public void onTextChanged(CharSequence s, int start,
								int before, int count) {
						}

						@Override
						public void beforeTextChanged(CharSequence s,
								int start, int count, int after) {
						}

						@Override
						public void afterTextChanged(Editable s) {
							//          
							viewHolder.listview_item_edit
									.setSelection(viewHolder.listview_item_edit
											.getText().length());

							int position = (Integer) viewHolder.listview_item_edit
									.getTag();

							double selledit = getAmount(s.toString());
							//  editText        HashMap 
							sell_edit_maps.put(position, selledit + "");
							
							resetSellBuyCollUI();

						}
					});

			//   edittext    , amout     
			viewHolder.listview_item_edit
					.setOnFocusChangeListener(new android.view.View.OnFocusChangeListener() {
						@Override
						public void onFocusChange(View v, boolean hasFocus) {
							//          
							viewHolder.listview_item_edit
									.setSelection(viewHolder.listview_item_edit
											.getText().length());

							String damoutStr = "";
							if (hasFocus) {
								int position = (Integer) viewHolder.listview_item_edit
										.getTag();
								if ((sell_edit_maps.get(position) != null)) {
									damoutStr = DecimalUtil.formatAmount(Double
											.parseDouble((sell_edit_maps
													.get(position).toString())));
								} else {
									damoutStr = DecimalUtil.formatAmount(Double
											.parseDouble((sellMaps
													.get(position).get(
															HEDGE_AMOUNT)
													.toString())));
								}

								// double damout = Double.parseDouble((sellMaps
								// .get(position).get(HEDGE_AMOUNT)
								// .toString()));
								viewHolder.listview_item_edit
										.setText(damoutStr);
							} else {
								//              
							}
						}
					});

		}