2013-03-23 3 views
0

Как получить/идентифицировать уникальную идентификационную иерархию. Ниже мой фрагмент кода фот getChildView (...)Получение уникального идентификатора в виде иерархии getchildView (...) в android

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 всегда получить нижние значения строки, так как все точки зрения имеют тот же идентификатор. Пожалуйста, помогите мне.

enter image description here

+0

Вы повторно использовать те же идентификаторы в каждой записи, используйте идентификатор записи, чтобы получить запись, а затем вызвать findViewById на эту запись, чтобы получить текущую компанию, интерес и продолжительность. Например. listview содержит элементы с одинаковым идентификатором для всех видов внутри, элементы различаются, используя идентификаторы элементов самого элемента, а не идентификаторы изнутри. – Tobrun

+0

спасибо за комментарии к ур. Это не работает для меня: expandableRow = (LinearLayout) convertView.findViewById (R.id.lnrExpListRow); \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 = (кнопка) expandableRow.findViewById (R.id.btnSave); \t \t btnDelete = (кнопка) expandableRow.findViewById (R.id.btnDelete); – sns

ответ

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(); 
      } 
    } 
}); 
+0

Как получить обновленные значения из EditText. Используя предлагаемое решение, я могу повторно сохранить только существующие старые данные. Пожалуйста, поправьте меня, если я ошибаюсь – sns

+0

Отредактировал свой ответ ... На этот раз я получил вашу проблему, я думаю. –

+0

Также используйте эффективные рекомендации по дизайну 'ListView', т. Е. Используйте шаблон ViewHolder в методе getChildView(). –