【问题标题】:EditText Values getting cleared on scrolling listview androidEditText值在滚动listview android上被清除
【发布时间】:2017-09-28 16:35:24
【问题描述】:
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 的值都会被清除。

【问题讨论】:

    标签: java android listview android-edittext


    【解决方案1】:

    希望这会奏效...

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

    【讨论】:

    • 不工作,先生!滚动列表视图中的值发生变化
    【解决方案2】:

    由于列表视图的回收特性,您需要再次更新学生列表中的 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;
    }
    

    【讨论】:

    • 你能在某处分享你的列表代码吗?我认为代码可能还有另一个问题。据我了解,标记的编辑文本需要在视图离开屏幕时保存值,对吗?还有另一种解决方案,您可以在其中执行 holder.setIsRecyclable(false);在回收商的观点中,但我建议不要这样做。
    【解决方案3】:

    试试:

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

    【讨论】:

      【解决方案4】:

      试试这个:

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

      【讨论】:

      • 问题不在于您的 ArrayAdaptor,问题在于您的 xml 布局或数据集或代码的任何其他部分。你能提供更多细节吗?
      • 我已经发布了数组适配器的完整代码,你需要膨胀对象的xml代码吗?
      猜你喜欢
      • 2014-02-02
      • 1970-01-01
      • 1970-01-01
      • 2015-05-29
      • 2014-12-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多