当我们在Edittext中更改或添加文本时,如何在listView中找到特定行的位置

时间:2014-10-30 08:34:27

标签: android listview android-listview

我是android的新手。这是我的编码。 在editText(qtyValue)中输入文本后,我需要listView中特定行的位置。请帮我找到解决方案。谢谢。

private class MyListItemDataAdapter extends BaseAdapter
{

    List<ItemData> listItemData;
    private Context context;

    public MyListItemDataAdapter(Context context,List<ItemData> listItemData)
    {
        this.listItemData = listItemData;
        this.context = context;

    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
     View itemView = convertView;
    if(itemView == null)
     {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        itemView = inflater.inflate(R.layout.item, parent, false);

     }

     final ItemData currentItemData = listItemData.get(position);

     final TextView newItmTotCost = (TextView)itemView.findViewById(R.id.totCost);
     final TextView newItmCost = (TextView)itemView.findViewById(R.id.cost);
     final EditText perValue = (EditText)itemView.findViewById(R.id.perEdit); 
     final EditText qtyValue = (EditText)itemView.findViewById(R.id.qEdit);




     qtyValue.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)
        {

            newItmTotCost.setText(String.valueOf(String.format("%.2f", getTotalValue())));

        }

        private float getTotalValue()
        {
            try
            {
                if(!qtyValue.getText().toString().equals(""))
                {
                if(!perValue.getText().toString().equals(""))
                {
                    float percentage = Float.parseFloat(perValue.getText().toString());
                    int qty = Integer.valueOf(qtyValue.getText().toString());
                    float cost =Float.valueOf(currentItemData.getNewItemcost().toString());
                    float qtyCosttotal = qty * cost;
                    float perValue = (percentage / 100) * qtyCosttotal;
                    float total = qtyCosttotal - perValue;
                    return total;
                }
                else
                {
                    int qty = Integer.valueOf(qtyValue.getText().toString());
                    float cost =Float.valueOf(currentItemData.getNewItemcost().toString());
                    float total = qty * cost;
                    return total;
                }
                }
            return 0;
            }
            catch(Exception ex)
            {
                Toast.makeText(getActivity(), ex.toString(), Toast.LENGTH_SHORT).show();
                return 0;
            }

        }
    });


     perValue.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) 
        {

            newItmTotCost.setText(String.valueOf(String.format("%.2f",getTotalPerValue())));
        }

        private float getTotalPerValue()
        {
            try
            {
                float discountValue;
                float totalValue;
                int qty = Integer.valueOf(qtyValue.getText().toString());
                float productPrice =Float.valueOf(currentItemData.getNewItemcost().toString());

                if(!perValue.getText().toString().equals(""))
                {
                     discountValue = Float.parseFloat(perValue.getText().toString());
                     discountValue = productPrice * (discountValue / 100);

                }
                else
                {                       
                    discountValue=0;    
                }

                totalValue = (qty * productPrice) - discountValue;

                return totalValue;
            }
            catch (Exception ex)
            {
                return 0;
            }
        }
    });

     TextView newItmName = (TextView)itemView.findViewById(R.id.itemName);
     newItmName.setText(currentItemData.getNewItemName());

     newItmTotCost.setText(currentItemData.getNewItemTotalCost());

     TextView newItmPcs = (TextView)itemView.findViewById(R.id.pcs);
     newItmPcs.setText(getResources().getString(R.string.fa_cube)+" "+currentItemData.getNewItempcs());
     newItmPcs.setTypeface(fa);

     newItmCost.setText(currentItemData.getNewItemcost());

    return itemView;    
    }

    @Override
    public int getCount() 
    {
        // TODO Auto-generated method stub
        return listItemData.size();
    }

    @Override
    public Object getItem(int position)
    {
        // TODO Auto-generated method stub
        return listItemData.get(position);
    }

    @Override
    public long getItemId(int position) 
    {
        // TODO Auto-generated method stub
        return position;
    }

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

}   

1 个答案:

答案 0 :(得分:2)

 @Override
    public void afterTextChanged(Editable s)
    {
        System.out.println(position);
    }

会给你当前的位置