0

如何获取/识别视图层次结构中的唯一 ID。下面是我的代码片段 fot 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();
                }   
            }


        }
    });

现在单击下图中的“保存”按钮,当我获取文本时,它始终会获取底行值,因为所有视图都具有相同的 ID。请帮我。

在此处输入图像描述

4

1 回答 1

0

哦,您将EditText's 保留在类变量中,切勿对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();
            }
    }
});
于 2013-03-23T12:50:00.200 回答