2013-03-23 111 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