EditText在滚动listview android上清除值

时间:2017-09-28 16:35:24

标签: java android listview android-edittext

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值都会被清除。

4 个答案:

答案 0 :(得分:0)

希望这会有用......

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;
    }

答案 1 :(得分:0)

由于列表视图的回收性质,您需要在学生列表中再次更新edittext值,并且还要在每次调用getview方法时设置值。

类似的东西:

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;
}

答案 2 :(得分:0)

试试这个:

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

答案 3 :(得分:0)

尝试:

    @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;
    }
}