0

I am trying to make a sales screen using listView where each listItem has an editText for user to input sales quantity for that particular item. I am using an Array Adapter to generate the listView. Problem is, when I click on an editText (for instance, position 1), type something in it and scroll down, every 5th (position 5, 9, 13 and so on) editText is getting focused and the typed content is automatically getting repeated in those editTexts. Tried a lot of online resources, but not able to find out what am I doing wrong! Thanks in advance for any help.

class SalesEntryAdapter extends ArrayAdapter<SalesEntry> {
    List<SalesEntry> mList;
    public SalesEntryAdapter(List<SalesEntry> salesList) {
        super(SalesActivity.this, R.layout.listview_sales_item, salesList);
        this.mList = salesList;
    }

    class ViewHolder {
        TextView tv_itemCode;
        TextView tv_itemDescription;
        TextView tv_priceLot;
        TextView tv_tradePrice;
        TextView tv_discPrice;
        TextView tv_salePrice;
        TextView tv_qtyOnHand;
        TextView tv_lineTotal;
        EditText et_saleQty;
        int position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        LayoutInflater inflater = getLayoutInflater();
        double trade_price, net_price;

        if(convertView == null){ // ensures re-usability of views created runtime
            convertView = inflater.from(getContext()).inflate(R.layout.listview_sales_item, parent, false);
            holder = new ViewHolder();
            holder.tv_itemCode = convertView.findViewById(R.id.tv_itemCode);
            holder.tv_itemDescription = convertView.findViewById(R.id.tv_itemDescription);
            holder.tv_priceLot = convertView.findViewById(R.id.tv_priceLot);
            holder.tv_tradePrice = convertView.findViewById(R.id.tv_tradePrice);
            holder.tv_discPrice = convertView.findViewById(R.id.tv_discPrice);
            holder.tv_salePrice = convertView.findViewById(R.id.tv_salePrice);
            holder.tv_qtyOnHand = convertView.findViewById(R.id.tv_qtyOnHand);
            holder.tv_lineTotal = convertView.findViewById(R.id.tv_lineTotal);
            holder.et_saleQty = convertView.findViewById(R.id.et_saleQty);
            convertView.setTag(holder);

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


        SalesEntry salesEntry = mList.get(position);

        holder.position = position; // save the position in the ViewHolder
        holder.tv_itemCode.setText(salesEntry.get_item_code());
        holder.tv_itemDescription.setText(salesEntry.get_item_desc());
        holder.tv_priceLot.setText(salesEntry.get_price_lot());
        holder.tv_tradePrice.setText(Double.toString(salesEntry.get_trade_price()));
        holder.tv_discPrice.setText(Double.toString(salesEntry.get_net_price()));
        holder.tv_salePrice.setText(Double.toString(salesEntry.get_net_price()));

        holder.et_saleQty.setText(Integer.toString(salesEntry.get_sale_qty()));

        //holder.et_saleQty.setId(position); // this is to set a reference ID for each EditText

        trade_price = salesEntry.get_trade_price();

        /*
        holder.et_saleQty.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            public void onFocusChange(View v, boolean hasFocus) {
                if (!hasFocus){
                    int position = v.getId();
                    EditText saleQty = (EditText) v;
                    //salesList.get(position).sale_qty = Integer.parseInt(saleQty.getText().toString());
                }
            }
        });
        */
        return convertView;

    }
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Aditya Amit
  • 71
  • 2
  • 9
  • Can you please explain what exactly you trying on onFocusChange method. If you simply delete setOnFocusChangeListener then what happens ? – Krishna Sharma Aug 19 '18 at 10:11
  • `final int position` ...does that make sense? – Martin Zeitler Aug 19 '18 at 10:13
  • I was trying to override the recycling behavior of listview using that OnFocusChangeListener. Anyway, I have commented that part for now as it is not working. Still I am facing the same problem (focus & content repeated every 5th row). @KrishnaSharma – Aditya Amit Aug 19 '18 at 10:15
  • @MartinZeitler not sure about that. I have seen that in every resources I found so far. – Aditya Amit Aug 19 '18 at 10:16
  • https://stackoverflow.com/questions/15655012/how-does-the-final-keyword-in-java-work-i-can-still-modify-an-object – Martin Zeitler Aug 19 '18 at 10:33

0 Answers0