2014-10-30 49 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

相關問題