列表视图中未更新编辑文本值

时间:2015-03-06 05:43:31

标签: android xml list view adapter

我有自定义适配器在列表视图中显示我的值(从服务器获取并使用XML解析)。

列表视图行将包含文本框和编辑文本。 我的问题是,

如果我想编辑编辑文本值并滚动列表视图,它会重复,也无法在从编辑文本焦点抬起后更新列表视图中的每一行。

  1. 如何更新每行编辑文本?
  2. 滚动时如何避免重复单元格值?
  3. 以下是代码:

    public View getView(int position, View convertView, ViewGroup parent) {
        convertView = null;
        final int i = position;
    
        final ViewHolder holder;
    
        final RowItem rowItem = (RowItem)getItem(position);
        if (convertView == null) {
            LayoutInflater mInflater = (LayoutInflater) context
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    
            convertView = mInflater.inflate(R.layout.category_list_edit,  parent, false);
    
            holder = new ViewHolder();
            holder.commodityName = (TextView) convertView.findViewById(R.id.commodityName);
            holder.id=(TextView)convertView.findViewById(R.id.vendorId);
            holder.unitValue=(TextView)convertView.findViewById(R.id.unitValue);
            holder.price=(EditText)convertView.findViewById(R.id.priceValue);
    
    
            convertView.setTag(holder);
    
        }
        else
        {
            holder = (ViewHolder) convertView.getTag();
    
        }
        holder.commodityName.setText(rowItem .getCommodityName());
        holder.price.setText(rowItem.getPrice());
        holder.unitValue.setText(rowItem .getQuantity());
        holder.id.setText(rowItem.getId());
    
        holder.price.setOnFocusChangeListener(new OnFocusChangeListener() {
            public void onFocusChange(View v, boolean hasFocus) {
                if (!hasFocus){
    
    
                    final EditText Caption = (EditText)v;
                    String value = Caption.getText().toString();
                    final  int primaryId=v.getId();
                    filteredList.get(primaryId).setPrice(value);
                    notifyDataSetChanged();
    
                }
                else
                {
    
                    final int position = v.getId();
                    final EditText Caption = (EditText) v;
                    String value = Caption.getText().toString();
    
                }
            }
        });
    }
    

2 个答案:

答案 0 :(得分:0)

也许你应该在其他部分使用notifyDataSetChanged();

试试这个

else
        {

            final int position = v.getId();
            final EditText Caption = (EditText) v;
            String value = Caption.getText().toString();
            notifyDataSetChanged();

        }

答案 1 :(得分:-1)

在适配器类中重写这两个方法,它会阻止列表项值在滚动时重复

  @Override
  public int getViewTypeCount() {
        return getCount();
  }

  @Override
  public int getItemViewType(int position) {
         return position;
  }