2014-10-30 53 views
0

我是android的新版本。这是我的编码。 我在editText(qtyValue)中输入文本后需要listView中特定行的位置。请帮我找到解决办法。谢谢。如何在编辑文本中更改或添加文本时在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; 
    } 

} 
+0

你使用自定义适配器? – 2014-10-30 08:39:04

+0

发布一些代码,我们需要看看你已经取得了什么。 – 2014-10-30 08:42:38

+0

具体排是指哪排?你能否详细说明你的问题 – 2014-10-30 09:04:13

回答

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

会给你的当前位置

+3

非常感谢你的兄弟.... – Palanivelraghul 2014-10-30 09:57:34

相关问题