简体   繁体   中英

how to add a textview dynamically in an editable listview

I'm stuck in a problem, I've created a list view where some columns are populated from the database, then it has an edit text which allows users to type any number. When user enters any number in the text box I want to multiply that number with the number in the other column(populated from the sqlite),

till here everything works fine but the problem comes when i set the multiplication result in the last column which is a textview as it is adding dynamically below is the screenshot of my list

In case of editable list, the edit text lose focus on scrolling I have solved the edit text problem but I'm unable to fix the last column which also loses focus on scrolling

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

 } }

Some times listview don't manage data after scrolling so making editable list is not a better solution for this i ll advice you to use layout inflater and inflate items and add them into a linear layout vertically, In this way scrolling ll not repeat or lose any data. Example :-

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

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