0

ビュー階層で一意の 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();
                }   
            }


        }
    });

下の画像の [保存] ボタンをクリックすると、すべてのビューが同じ ID を持つため、getText は常に一番下の行の値を取得します。私を助けてください。

ここに画像の説明を入力

4

1 に答える 1

0

ああEditText、クラス変数にを保持していますが、ListViewアイテムに対しては決してそうしないでください。getView()これを行うと、または呼び出されるたびに参照が変更され、参照getChildView()がぶらぶらしてしまいます。このようにすると、準備完了です。

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 に答える