EditText在滚动列表视图中获得清除android

问题描述:

public class Custom_Student_marks_list_faculty_adapter extends ArrayAdapter<MarksStudentListFacultyObject> { 
    private Activity context; 
    private List<MarksStudentListFacultyObject> studentlist; 

    public Custom_Student_marks_list_faculty_adapter(Activity context,List<MarksStudentListFacultyObject> studentlist) { 
     super(context,R.layout.custom_listview_marks_faculty,studentlist); 
     this.context=context; 
     this.studentlist=studentlist; 
    } 

    @Override 
    public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) { 

     final ViewHolder holder; 
     if(convertView==null) { 
      LayoutInflater inflater = context.getLayoutInflater(); 
      convertView = inflater.inflate(R.layout.custom_listview_marks_faculty, null); 
      holder = new ViewHolder(); 
      holder.name = (TextView) convertView.findViewById(R.id.custom_listview_marks_faculty_stu_name_id); 
      holder.marks = (EditText) convertView.findViewById(R.id.custom_listview_marks_faculty_stu_marks_id); 
      String mark = holder.marks.getText().toString(); 

      MarksStudentListFacultyObject student_list = studentlist.get(position); 

      holder.name.setText(student_list.getName()); 
      holder.marks.setText(mark); 
      holder.marks.setTag(position); 
      convertView.setTag(holder); 
     } else { 
      holder =(ViewHolder)convertView.getTag(); 
     } 

     holder.name.setText(studentlist.get(position).getName()); 
     holder.marks.setText(studentlist.get(position).getMakrs()); 

     return convertView; 
    } 
} 

class ViewHolder { 
    protected TextView name; 
    protected EditText marks; 
} 

我已经看到很多解决方案,但它并没有为我工作。每次滚动时,ListView值都被清除。EditText在滚动列表视图中获得清除android

希望这将工作...

public Custom_Student_marks_list_faculty_adapter(Activity context,List<MarksStudentListFacultyObject> studentlist) { 
     super(context,R.layout.custom_listview_marks_faculty,studentlist); 
     this.context=context; 
     this.studentlist=studentlist; 
    } 


    @NonNull 
    @Override 
    public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) { 

     final ViewHolder holder; 
     if(convertView==null) { 
      LayoutInflater inflater = context.getLayoutInflater(); 
      convertView = inflater.inflate(R.layout.custom_listview_marks_faculty, null); 
      holder = new ViewHolder(); 
      holder.name = (TextView) convertView.findViewById(R.id.custom_listview_marks_faculty_stu_name_id); 
      holder.marks = (EditText) convertView.findViewById(R.id.custom_listview_marks_faculty_stu_marks_id); 
      convertView.setTag(holder); 
     } else { 
      holder =(ViewHolder)convertView.getTag(); 
     } 

      MarksStudentListFacultyObject student_list = studentlist.get(position); 
      holder.name.setText(student_list.getName()); 
      //holder.marks.setText(student_list.getMarks()); 

     return convertView; 
    } 
+0

不工作先生!值滚动列表视图 –

你需要在你的学生名单再次更新,因为列表视图的回收性,还可以设置每次getview方法的价值是价值的EditText调用。

喜欢的东西:

public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) { 

    final ViewHolder holder; 
    if(convertView==null) { 
     LayoutInflater inflater = context.getLayoutInflater(); 
     convertView = inflater.inflate(R.layout.custom_listview_marks_faculty, null); 
     holder = new ViewHolder(); 
     holder.name = (TextView) convertView.findViewById(R.id.custom_listview_marks_faculty_stu_name_id); 
     holder.marks = (EditText) convertView.findViewById(R.id.custom_listview_marks_faculty_stu_marks_id); 
     convertView.setTag(holder); 
    } else { 
     holder =(ViewHolder)convertView.getTag(); 
    } 

     MarksStudentListFacultyObject student_list = studentlist.get(position); 
     holder.name.setText(student_list.getName()); 
     holder.marks.setText(student_list.getMarks()); 
     holder.marks.addTextChangedListener(new TextWatcher() { 

     @Override 
     public void onTextChanged(CharSequence s, int start, 
       int before, int count) { 
      //set data to array when changed 
      student_list.setMarks(s.toString()); 
     } 

     @Override 
     public void beforeTextChanged(CharSequence s, int start, 
       int count, int after) { 

     } 

     @Override 
     public void afterTextChanged(Editable s) { 

     } 
    }); 

    return convertView; 
} 
+0

不工作先生,仍然是同样的问题来了! –

+0

你能分享你的代码清单吗?我认为代码可能存在另一个问题。据我所知,标记的edittext需要保存视图离开屏幕时的值,对吧? Theres另一种解决方案,您可以在其中做holder.setIsRecyclable(false);在回收者的意见,但我建议反对。 –

试试这个:

@Override 
public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) { 

    if(convertView==null) { 
     LayoutInflater inflater = context.getLayoutInflater(); 
     convertView = inflater.inflate(R.layout.custom_listview_marks_faculty, null); 
    } 

    TextView name = (TextView) convertView.findViewById(R.id.custom_listview_marks_faculty_stu_name_id); 
    EditText marks = (EditText) convertView.findViewById(R.id.custom_listview_marks_faculty_stu_marks_id); 

    name.setText(studentlist.get(position).getName()); 
    marks.setText(studentlist.get(position).getMakrs()); 

    return convertView; 
} 

编辑:在ArrayAdaptor

覆盖getCount将()方法:

@Override public int getCount(){ return studentlist.size(); } 
+0

不能正常工作!... –

+0

问题不在于您的ArrayAdaptor,问题在于您的xml布局或数据集或任何其他代码部分。你能提供更多的细节吗? –

+0

我已经发布了阵列适配器的完整代码,你需要xml代码来充气对象吗? –

尝试:

@Override 
    public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) { 

     final ViewHolder holder; 
     if(convertView==null) { 
      LayoutInflater inflater = context.getLayoutInflater(); 
      convertView = inflater.inflate(R.layout.custom_listview_marks_faculty, null); 
      holder = new ViewHolder(); 
      holder.name = (TextView) convertView.findViewById(R.id.custom_listview_marks_faculty_stu_name_id); 
      holder.marks = (EditText) convertView.findViewById(R.id.custom_listview_marks_faculty_stu_marks_id); 
         convertView.setTag(holder); 
     } else { 
      holder =(ViewHolder)convertView.getTag(); 
     } 


     String mark = holder.marks.getText().toString(); 

      MarksStudentListFacultyObject student_list = studentlist.get(position); 

      holder.name.setText(student_list.getName()); 
      holder.marks.setText(mark); 


      return convertView; 
    } 
} 
+0

不工作,数据仍然变得清晰! –