0

嗨有和可擴展的列表視圖,每擴展時只有一個孩子與一些EditText裏面。問題是,當我展開一個標題並將某些內容放在edittext上時,當我展開另一個標題時,我放入的值就是另一個標題的原始標題。我知道ListViews在滾動時重用視圖,並且我放置了ViewHolders,但我無法使其工作。EditText轉到另一個孩子上ExpandableListView

這裏是我的CustomAdapter代碼:

public class ExpandableListAddDataAdapter extends BaseExpandableListAdapter { 

private Context _context; 
private List<String> _listDataHeader = new ArrayList<>(); // header titles 
private Date sportsDate, medsDate, glucoseDate, mealDate, auxDate; 

public ExpandableListAddDataAdapter(Context context, List<String> listDataHeader) { 
    this._context = context; 
    this._listDataHeader = listDataHeader; 
    this.sportsDate = new Date(1900,1,1); 
    this.medsDate = new Date(1900,1,1); 
    this.glucoseDate = new Date(1900,1,1); 
    this.mealDate = new Date(1900,1,1); 
    this.auxDate = new Date(1900,1,1); 
} 

@Override 
public String[] getChild(int groupPosition, int childPosititon) { 
    return null; 
} 


@Override 
public long getChildId(int groupPosition, int childPosition) { 
    return childPosition; 
} 

@Override 
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { 
    final ChildViewHolder holder; 
    View row = convertView; 

    if (convertView == null) { 
     LayoutInflater infalInflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     row = infalInflater.inflate(R.layout.expandable_data_add_child, parent,false); 
     holder = new ChildViewHolder(row); 
     row.setTag(holder); 
    }else{ 
     holder = (ChildViewHolder) row.getTag(); 
    } 

    this.auxDate = new Date(1900,1,1); 
    switch (groupPosition){ 
     case 0: 
      auxDate = new Date(this.glucoseDate.getTime()); 
      break; 
     case 1: 
      auxDate = new Date(this.mealDate.getTime()); 
      break; 
     case 2: 
      auxDate = new Date(this.sportsDate.getTime()); 
      break; 
     case 3: 
      auxDate = new Date(this.medsDate.getTime()); 
      break; 
    } 


    final View finalConvertView = row; 
    holder.txtDate.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      int mYear, mMonth, mDay; 
      if(auxDate.getYear() != 1900){ 
       mYear = auxDate.getYear(); 
       mMonth = auxDate.getMonth(); 
       mDay = auxDate.getDay(); 
      }else{ 
       Calendar mcurrentDate = Calendar.getInstance(); 
       mYear = mcurrentDate.get(Calendar.YEAR); 
       mMonth = mcurrentDate.get(Calendar.MONTH); 
       mDay = mcurrentDate.get(Calendar.DAY_OF_MONTH); 
      } 

      DatePickerDialog mDatePicker = new DatePickerDialog(finalConvertView.getContext(), new DatePickerDialog.OnDateSetListener() { 
       @Override 
       public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { 
        auxDate.setYear(year - 1900); 
        auxDate.setMonth(monthOfYear); 
        auxDate.setDate(dayOfMonth); 
        if (dayOfMonth < 10) { 
         holder.txtDate.setText(0 + dayOfMonth + "/" + (monthOfYear + 1) + "/" + year); 
        } else { 
         holder.txtDate.setText(dayOfMonth + "/" + (monthOfYear + 1) + "/" + year); 
        } 
        switch (groupPosition){ 
         case 0: 
          glucoseDate = new Date(auxDate.getTime()); 
          break; 
         case 1: 
          mealDate = new Date(auxDate.getTime()); 
          break; 
         case 2: 
          sportsDate = new Date(auxDate.getTime()); 
          break; 
         case 3: 
          medsDate = new Date(auxDate.getTime()); 
          break; 
        } 
       } 
      }, mYear, mMonth, mDay); 
      mDatePicker.show(); 
     } 
    }); 
    holder.txtTime.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      int mHour, mMinute; 
      if(auxDate.getHours() != 0){ 
       mHour = auxDate.getHours(); 
       mMinute = auxDate.getMinutes(); 
      }else{ 
       Calendar mcurrentDate = Calendar.getInstance(); 
       mHour = mcurrentDate.get(Calendar.HOUR_OF_DAY); 
       mMinute = mcurrentDate.get(Calendar.MINUTE); 
      } 

      TimePickerDialog mTimePicker = new TimePickerDialog(finalConvertView.getContext(), new TimePickerDialog.OnTimeSetListener() { 
       @Override 
       public void onTimeSet(TimePicker view, int hourOfDay, int minute) { 
        auxDate.setHours(hourOfDay); 
        auxDate.setMinutes(minute); 
        if (minute < 10) { 
         holder.txtTime.setText(hourOfDay + ":" + 0 + minute); 
        } else { 
         holder.txtTime.setText(hourOfDay + ":" + minute); 
        } 
        switch (groupPosition){ 
         case 0: 
          glucoseDate = auxDate; 
          break; 
         case 1: 
          mealDate = auxDate; 
          break; 
         case 2: 
          sportsDate = auxDate; 
          break; 
         case 3: 
          medsDate = auxDate; 
          break; 
        } 
       } 
      }, mHour, mMinute, false); 
      mTimePicker.show(); 
     } 
    }); 
    return row; 
} 

@Override 
public int getChildrenCount(int groupPosition) { 
    return 1; 
} 

@Override 
public Object getGroup(int groupPosition) { 
    return this._listDataHeader.get(groupPosition); 
} 

@Override 
public int getGroupCount() { 
    return this._listDataHeader.size(); 
} 

@Override 
public long getGroupId(int groupPosition) { 
    return groupPosition; 
} 

@Override 
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { 
    final HeaderViewHolder holder; 
    View row = convertView; 
    String headerTitle = (String) getGroup(groupPosition); 
    if (convertView == null) { 
     LayoutInflater infalInflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     row = infalInflater.inflate(R.layout.expandable_data_add_header, parent,false); 
     holder = new HeaderViewHolder(row); 
     row.setTag(holder); 
    }else{ 
     holder = (HeaderViewHolder) row.getTag(); 
    } 
    if (isExpanded) { 
     holder.expandIndicator.setImageResource(R.drawable.minus); 
    } else { 
     holder.expandIndicator.setImageResource(R.drawable.plus); 
    } 
    holder.txtHeader.setTypeface(null, Typeface.BOLD); 
    holder.txtHeader.setText(headerTitle); 

    return row; 
} 

@Override 
public boolean hasStableIds() { 
    return false; 
} 

@Override 
public boolean isChildSelectable(int groupPosition, int childPosition) { 
    return true; 
} 

private static class ChildViewHolder { 
    EditText txtDate; 
    EditText txtTime; 
    EditText txtGlucose; 
    ChildViewHolder(View v) 
    { 
     txtDate = (EditText) v.findViewById(R.id.input_event_date); 
     txtTime = (EditText) v.findViewById(R.id.input_event_time); 
     txtGlucose = (EditText) v.findViewById(R.id.input_glucose); 

    } 
} 

private static class HeaderViewHolder { 
    TextView txtHeader; 
    ImageView expandIndicator; 

    HeaderViewHolder(View v) 
    { 
     txtHeader = (TextView) v.findViewById(R.id.expandable_add_data_header_text); 
     expandIndicator = (ImageView) v.findViewById(R.id.expandable_add_data_header_icon); 

    } 
    } 
} 

這是一些screenshoots的問題,我現在面臨

The first header expanded with the edittext

Second header expanded and the value of the firt edittext goes to the second header child

回答

0

最後我找到了解。該solutionm被添加TextWatcher每一個EditText上是這樣的:

holder.position = groupPosition; 
    holder.txtGlucose.setText(arrInput[groupPosition]); 
    holder.txtGlucose.addTextChangedListener(new TextWatcher() { 

     @Override 
     public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, 
             int arg3) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void afterTextChanged(Editable arg0) { 
      // TODO Auto-generated method stub 
      arrInput[holder.position] = arg0.toString(); 
     } 
    }); 

凡arrInput是一個字符串[]我在那裏的值存儲這個EditText上

相關問題