我正在开发一个应用程序,用户可以在其中填写报告和民意调查。在一个活动中,我有一个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;
    }
}

最佳答案

您应该执行以下操作:

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中调用isSelectedholder.diverAttendedonClick

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

07-24 21:31