2013-03-23 115 views
0

如何獲取/識別視圖層次結構中的唯一ID。下面是我的代碼片段FOT getChildView(...)在android中獲取獨特的ID在getchildView(...)的層次結構

public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { 
    final ExpandListChild child = (ExpandListChild) getChild(groupPosition, childPosition); 

if (convertView == null) 
    { 
LayoutInflater infalInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
convertView = infalInflater.inflate(R.layout.manage_insurance, null); 
    } 

    company = (EditText) convertView.findViewById(R.id.et_CompanyName); 
    interest = (EditText) convertView.findViewById(R.id.et_Interest); 
    duration = (EditText) convertView.findViewById(R.id.et_Duration); 
    btnSave = (Button) convertView.findViewById(R.id.btnSave); 
    btnDelete = (Button) convertView.findViewById(R.id.btnDelete); 

    company.setTag(R.id.string_key1, childPosition); 
    //interest.setTag(childPosition, childPosition); 
    //duration.setTag(childPosition, childPosition);   
    btnSave.setTag(R.id.string_key2, childPosition); 
    //btnDelete.setTag(childPosition, childPosition); 

    company.setText(child.getCompanyName().toString()); 
    interest.setText(child.getInterest()+""); 
    duration.setText(child.getDuration()+""); 

    btnSave.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      int viewtag = (Integer) v.getTag(R.id.string_key2); 
      if (childPosition == viewtag){ 
       durationValue = (duration.getText().toString().equals("") ? 0.0f : Float.parseFloat(duration.getText().toString())); 
       interestValue = (interest.getText().toString().equals("") ? 0.0f : Float.parseFloat(interest.getText().toString())); 

       Log.v("durationValue", "durationValue ======" + durationValue +"========"+ interestValue); 

       if (checkingForEmptyFields()){ 
        int updatedRows = dbManager.updateInsurance(companyValue, durationValue, 15); 
        if (updatedRows > 0){ 
         Toast.makeText(mContext, "Successfully updated", Toast.LENGTH_SHORT).show();  
         mContext.startActivity(new Intent(mContext, InsurancePanelInflator.class)); 
        } 
        else 
        { 
         Toast.makeText(mContext, "Problem occured while updating", Toast.LENGTH_SHORT).show(); 
        } 

       } 
       else 
       { 
        Toast.makeText(mContext, "Fill Mandatory Fields First", Toast.LENGTH_SHORT).show(); 
       } 
      } 


     } 
    }); 

現在就點擊下方圖片保存按鈕時,我的gettext它總是底行值的所有視圖具有相同的ID。請幫幫我。

enter image description here

+0

要重複使用相同的ID在每個條目,使用條目的ID來檢索條目,然後調用findViewById該條目檢索當前公司利益和持續時間。例如。一個列表視圖包含所有視圖內部​​具有相同標識的項目,這些項目通過使用項目本身的項目標識而不是內部視圖的標識進行區分。 – Tobrun 2013-03-23 12:40:59

+0

感謝您的評論。它不適合我:expandableRow =(LinearLayout)convertView.findViewById(R.id.lnrExpListRow); \t \t \t \t company =(EditText)expandableRow.findViewById(R.id.et_CompanyName); \t \t interest =(EditText)expandableRow.findViewById(R.id.et_Interest); \t \t duration =(EditText)expandableRow.findViewById(R.id.et_Duration); \t \t btnSave =(Button)expandableRow.findViewById(R.id.btnSave); \t \t btnDelete =(Button)expandableRow.findViewById(R.id.btnDelete); – sns 2013-03-23 15:52:08

回答

0

哦,您把EditText的類變量,從來沒有做到這一點對ListView項目。這樣做會改變他們的參考每次getView()getChildView()被調用,你最終會引用dandling。像這樣做,你是好去:

final ExpandListChild child = (ExpandListChild) getChild(groupPosition, childPosition); 

if (convertView == null) { 
    LayoutInflater infalInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    convertView = infalInflater.inflate(R.layout.manage_insurance, null); 
} 

final EditText company = (EditText) convertView.findViewById(R.id.et_CompanyName); 
final EditText interest = (EditText) convertView.findViewById(R.id.et_Interest); 
final EditText duration = (EditText) convertView.findViewById(R.id.et_Duration); 
final Button btnSave = (Button) convertView.findViewById(R.id.btnSave); 
final Button btnDelete = (Button) convertView.findViewById(R.id.btnDelete); 
// no need for tags 
company.setText(child.getCompanyName().toString()); 
interest.setText(child.getInterest()+""); 
duration.setText(child.getDuration()+""); 

btnSave.setOnClickListener(new OnClickListener() { 
    public void onClick(View v) { 
     // no need to test position 
      durationValue = (duration.getText().toString().equals("") ? 0.0f : Float.parseFloat(duration.getText().toString())); 
      interestValue = (interest.getText().toString().equals("") ? 0.0f : Float.parseFloat(interest.getText().toString())); 

      Log.v("durationValue", "durationValue ======" + durationValue +"========"+ interestValue); 

      if (checkingForEmptyFields()){ 
       int updatedRows = dbManager.updateInsurance(companyValue, durationValue, 15); 
       if (updatedRows > 0){ 
        Toast.makeText(mContext, "Successfully updated", Toast.LENGTH_SHORT).show();  
        mContext.startActivity(new Intent(mContext, InsurancePanelInflator.class)); 
       } else { 
        Toast.makeText(mContext, "Problem occured while updating", Toast.LENGTH_SHORT).show(); 
       } 
      } else { 
       Toast.makeText(mContext, "Fill Mandatory Fields First", Toast.LENGTH_SHORT).show(); 
      } 
    } 
}); 
+0

我將如何從EditText獲取更新值。使用您建議的解決方案,我可以只保存現有的舊數據。請糾正我,如果我錯了 – sns 2013-03-23 15:18:16

+0

編輯我的答案...我猜這次我得到了你的問題。 – 2013-03-23 19:26:05

+0

也請使用高效的'ListView'設計指南,即在'getChildView()'方法中使用ViewHolder模式。 – 2013-03-23 19:29:41