如何在可编辑的列表视图中动态添加文本视图

时间:2015-07-27 17:36:17

标签: android listview textview

我遇到了问题,我创建了一个列表视图,其中从数据库中填充了一些列,然后它有一个编辑文本,允许用户键入任何数字。当用户在文本框中输入任何数字时,我想将该数字乘以另一列中的数字(从sqlite填充),

直到这里一切正常但问题出现的时候我在最后一列中设置了乘法结果,这是一个textview,因为它在下面动态添加是我列表的截图

在可编辑列表的情况下,编辑文本失去了对滚动的关注我已经解决了编辑文本问题但是我无法修复最后一列也失去了对滚动的关注

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

      final   ViewHolder holder;
      if (convertView == null) {
        convertView = inflater.inflate(R.layout.productslistviewadapter, parent, false);

        holder = new ViewHolder();
        holder.tvdrCode = (TextView) convertView.findViewById(R.id.tvname);
        holder.tvDrName = (TextView) convertView.findViewById(R.id.tvprodpack);
        holder.tvterrcode= (TextView) convertView.findViewById(R.id.textView3);
        holder.caption = (EditText)convertView.findViewById(R.id.editText1);
        holder.tvValue = (TextView) convertView.findViewById(R.id.value);
        holder.tvValue.setVisibility(View.GONE);
        convertView.setTag(holder);

    } 
   else {
        holder = (ViewHolder) convertView.getTag();
    }

    Products p = prodList.get(position);
    holder.tvdrCode.setText(p.getDocCode());
    holder.tvDrName.setText(p.getDocName());
    holder.tvterrcode.setText(p.getAdr());

    //for editText
    holder.caption.setTag(position);
    holder.caption.setText(p.getCaption());
    int tag_position=(Integer) holder.caption.getTag();
    holder.caption.setId(tag_position); 

    //for textview
    holder.tvValue.setTag(position);
    holder.tvValue.setText(p.getAmount());
    tag_position =(Integer) holder.tvValue.getTag();
    holder.tvValue.setId(tag_position); 

    holder.caption.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        public void onFocusChange(View v, boolean hasFocus) {
           if (!hasFocus) {
               /*
             * When focus is lost save the entered value for
             * later use
             */
               int position2; 
               position2 = holder.caption.getId();
               position2 = holder.tvValue.getId();
               final EditText Caption = (EditText) holder.caption;
               final TextView TvValue = (TextView) holder.tvValue;

               if(Caption.getText().toString().length()>0)
                 {
                   prodList.get(position2).setCaption(Caption.getText().toString());

                   String prodpack = prodList.get(position).getDocName().toString();
                   String prodname = prodList.get(position).getDocCode().toString();
                   String quantity = prodList.get(position2).getCaption();

                   int  value = Integer.parseInt(prodpack) * Integer.parseInt(quantity);

                   holder.tvValue.setText(Integer.toString(value)); 
                   holder.tvValue.setVisibility(View.VISIBLE);
                   Log.e("sum",Integer.toString(value)); 

               }  
               else{
               }
           }
        }
    });     

    return convertView;
} 

static class ViewHolder {

   TextView tvdrCode;
   TextView tvDrName;
   TextView tvterrcode;
   EditText caption;
   TextView tvValue;

 } }

1 个答案:

答案 0 :(得分:2)

有些时候listview不会在滚动后管理数据,所以制作可编辑列表不是更好的解决方案,我建议你使用布局inflater和膨胀项目并将它们垂直添加到线性布局中,这样滚动不会重复或丢失任何数据。 示例: -

 linearLayoutAddItemsHere.removeAllViews();

 LayoutInflater inflater = LayoutInflater.from(ActivityName);

    for (int i=0;i<YourDbList.size();i++){
        View view = inflater.inflate(R.layout.viewLikeListItems, null);
        final RelativeLayout relativeLayoutRow = (RelativeLayout) view.findViewById(R.id.ParentRelativeLayoutFromYourItem);
        relativeLayoutRow.setId(i+1);

        TextView tvExample = (TextView)relativeLayoutText.findViewById(R.id.tvExample);
        tvExample.setText(list.get(i).getDbData());

        linearLayoutAddItemsHere.addView(view);
    }