ListView中的EditText在ListView中滚动时丢失内容

时间:2014-11-19 12:46:23

标签: android listview android-listview android-edittext

我正在处理用户填写报告和民意调查的应用。在一个活动中,我有一个ListView,其中子项包含EditTexts,Checkbox和TextView。

EditTexts和Checkbox的问题在于滚动时,它们所拥有的内容会丢失。我能够通过在数组中保存检查状态来解决Checkbox问题,但我无法使用EditTexts解决问题。我知道有关于这个特定问题的一些问题,但他们提供的解决方案似乎并不起作用。所以这是我的适配器:

public class DiveProfileAdapter extends ArrayAdapter<DiveProfile> {

    private Context mContext;
    private List<DiveProfile> mTasks;
    ViewHolder holder;

    private ArrayList<Boolean> itemChecked = new ArrayList<>(); //for saving checkbox values

    public DiveProfileAdapter(Context context, List<DiveProfile> objects) {
        super(context, R.layout.row_dive_profile_divers_list_item, objects);
        this.mContext = context;
        this.mTasks = objects;

        for (int i = 0; i < this.getCount(); i++) {
            itemChecked.add(i, false); // initializes all items value with false
        }
    }

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

        if (convertView == null) {
            LayoutInflater mLayoutInflater = LayoutInflater.from(mContext);
            convertView = mLayoutInflater.inflate(R.layout.row_dive_profile_divers_list_item, null);

            holder = new ViewHolder();
            holder.diverAttended = (CheckBox) convertView.findViewById(R.id.diver_attended_checkbox);   
            holder.diveTime = (EditText) convertView.findViewById(R.id.dive_time_edit_text);
            holder.depth = (EditText) convertView.findViewById(R.id.depth_edit_text);

            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }

        final DiveProfile diverObjectTask = mTasks.get(position);

        holder.diverAttended.setChecked(diverObjectTask.isDiverAttended());
        holder.diveTime.setText(diverObjectTask.getDiveTime());

        holder.diverAttended.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                CheckBox cb = (CheckBox) v.findViewById(R.id.diver_attended_checkbox);

                if (cb.isChecked()) {
                    itemChecked.set(position, true);
                    // do some operations here
                } else if (!cb.isChecked()) {
                   itemChecked.set(position, false);
                   // do some operations here
                }
             }
        });
        holder.diverAttended.setChecked(itemChecked.get(position)); // this will Check or Uncheck the

        holder.diveTime.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                String txt = setDiveTimeDialog();
                holder.diveTime.setText(txt);
                mTasks.get(position).setCurrentText(txt); //add the setter to the current item's position
            }
        });
        holder.diveTime.setText(diverObjectTask.getCurrentText());

        return convertView;
    }

    static class ViewHolder {
        CheckBox diverAttended;
        EditText diveTime;
        EditText depth;
    }

    private String setDiveTimeDialog() {

        //final String[] items = finItems;
        final String[] items = getContext().getResources().getStringArray(R.array.certification_level_items);

        // Create list dialog with required parameters - context, title, and our array of items to fill the list.
        CustomListDialog.Builder builder = new CustomListDialog.Builder(getContext(), "Dive Time", items);

        // Now again we can use some extra methods on the builder to customise it more.
        builder.titleAlignment(BaseDialog.Alignment.LEFT);
        builder.itemAlignment(BaseDialog.Alignment.LEFT);
        builder.titleColor(getContext().getResources().getColor(R.color.primary_color));


        // Now we can build our dialog.
        CustomListDialog customListDialog = builder.build();

        // Finally we can show it.
        customListDialog.show();

        customListDialog.setListClickListener(new CustomListDialog.ListClickListener() {
            @Override
            public void onListItemSelected(int i, String[] strings, String s) {

                result = s;

            }
        });

        return result;
    }
}

2 个答案:

答案 0 :(得分:1)

你应该这样做:

final DiveProfile diverObjectTask = mTasks.get(position);
holder.diveTime.setText(diverObjectTask.getDiveTie());
//also set your CheckBox value

这些值变为空,因为你没有在getView中设置它(当列表滚动时会自动调用getView

<强>更新

查看您的问题,我认为最简单的方法是在DiveProfile课程中创建新变量,例如boolean isSelectedString currentText

之后,不需要使用itemChecked之类的单独列表。您可以getset当前项目的值。

使用getter的示例:

holder.diverAttended.setChecked(mTasks.get(position).getSelected()); // getSelected is isSelected getter method

setSelected setter

中致电isSelected holder.diverAttended onClick}

EditText setter的示例:

    holder.diveTime.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {

            String txt = setDiveTimeDialog();
            holder.diveTime.setText(txt);
            mTasks.get(position).setcurrentText(txt); //add the setter to the current item's position
        }
    });

并在getCurrentText()

中致电getView

答案 1 :(得分:1)

您的Viewholder变量必须位于您的getView中,最好是您之前声明的第一个变量if(convertView)。将ViewHolder缓存在外面会导致不可预测的行为。

ConvertView是返回的视图,因此您必须设置对转换视图所做的任何更改。