在android中获取getchildView(...)视图层次结构中的唯一id

时间:2013-03-23 12:35:29

标签: android expandablelistview expandablelistadapter

如何在视图层次结构中获取/标识唯一ID。下面是我的代码片段get 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

1 个答案:

答案 0 :(得分: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();
            }
    }
});