适配器中的第一个edittext在添加新条目时丢失信息

时间:2016-02-05 02:57:40

标签: android listview android-adapter

我有一个这样的编辑文本:当我点击一个按钮时,它会在列表视图中展开。但是问题是当我单击按钮并且只有第一个edittext时,edittext中的项目没有保存并重置为空白。所有其他项目保存。

        // Set what happens when we click the next button (add preferences)
    btn_continue.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            SharedPreferences sharedPreferences = getActivity().getSharedPreferences(CreatePostingActivity.sharedPreferenceKey, Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putInt("task_length", job_task_array.size());
            Log.e("c_task_length", String.valueOf(job_task_array.size()));
            editor.putInt("requirements_length", job_requirements_array.size());

            for (int i = 0; i < job_task_array.size(); i++) {
                editor.putInt("taskvalue" + i, job_task_array.get(i).getJob_task_seekbar_value());
                editor.putString("taskname" + i, job_task_array.get(i).getJob_task_name());
            }
            for (int i = 0; i < job_requirements_array.size(); i++) {
                editor.putString("requirementname" + i, job_requirements_array.get(i).getJob_requirements());
            }
            editor.commit();
            d_PreviewPosting_fragment.onPageAppear(getActivity());
            CreatePostingActivity.mViewPager.setCurrentItem(3);
        }
    });

    add_task.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            job_task_array.add(new JobTask_Model("", 50));
            job_task_update(rootView, item_job_task_height);
            for (int i = 0; i < job_task_array.size(); i++)
                Log.e("job_task", job_task_array.get(i).getJob_task_name());
        }
    });

    add_requirement.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            job_requirements_array.add(new JobRequirements_Model(""));
            job_requirements_update(rootView, item_job_requirements_height);
            for (int i = 0; i < job_requirements_array.size(); i++)
                Log.e("job_requriements", job_requirements_array.get(i).getJob_requirements());
        }
    });

    // job_task_listView adapter stuff
    jobTasks_adapter = new JobTasks_Adapter(job_task_listView.getContext(), job_task_array);
    job_task_listView.setAdapter(jobTasks_adapter);

    // job_requirements listView adapter stuff... because the job_requirements uses a slightly different data model
    jobRequirements_adapter = new JobRequirements_Adapter(job_requirements_listView.getContext(), job_requirements_array);
    job_requirements_listView.setAdapter(jobRequirements_adapter);

    // Button clicking
    add_task.performClick();
    add_requirement.performClick();

    // Hack from job_task_listView to get height of job_task_listView
    job_task_listView.post(new Runnable() {
        @Override
        public void run() {
            job_task_height = job_task_listView.getMeasuredHeight();
            item_job_task_height = job_task_listView.getMeasuredHeight();
        }
    });

    // Hack from job_requirements_listView to get height of job_requirements_listView
    job_requirements_listView.post(new Runnable() {
        @Override
        public void run() {
            job_requirements_height = job_requirements_listView.getMeasuredHeight();
            item_job_requirements_height = job_requirements_listView.getMeasuredHeight();
        }
    });

    return rootView;
}

// This is called whenever we click 'add task' or 'delete task', does the whole animation cycle
public static void job_task_update(final View view, final int job_task_heightmove) {
    firsttask_value = job_task_array.get(0).getJob_task_seekbar_value();
    firsttask_name = job_task_array.get(0).getJob_task_name();
    add_task.setEnabled(false);
    Animation animation = new Animation() {
        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            job_task_listView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int) (job_task_height + (job_task_heightmove * interpolatedTime))));
        }
    };
    animation.setDuration(CreatePostingActivity.animationTime);
    animation.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {
        }

        @Override
        public void onAnimationEnd(Animation animation) {
            if (!firsttask) {
                // job_task_array.set(0, new JobTask_Model(firsttask_name, firsttask_value));
                firsttask = true;
            }
        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    });
    view.startAnimation(animation); // Update job_task_height's value AFTER the shit animates because apparently it takes the current job_task_height
    view.postOnAnimationDelayed(new Runnable() {
        @Override
        public void run() {
            job_task_height += job_task_heightmove;
            add_task.setEnabled(true);
        }
    }, CreatePostingActivity.animationTime * 2);
}

// This is called whenever we edit the text and create a enw one, does the whole animation cycle, CPP programming! HAHA
public static void job_requirements_update(View view, final int job_requirements_heightmove) {
    Animation animation = new Animation() {
        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            job_requirements_listView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int) (job_requirements_height + (job_requirements_heightmove * interpolatedTime))));
        }
    };
    animation.setDuration(CreatePostingActivity.animationTime);
    view.startAnimation(animation); // Update job_task_height's value AFTER the shit animates because apparently it takes the current job_task_height
    view.postOnAnimationDelayed(new Runnable() {
        @Override
        public void run() {
            job_requirements_height += job_requirements_heightmove;
            jobRequirements_adapter.notifyDataSetChanged();
        }
    }, CreatePostingActivity.animationTime * 2);
}

// TODO put the below in its own class
// Object model for the fragment_posting_2 item_job_requirements
private class JobRequirements_Model {
    private String job_requirements;

    public JobRequirements_Model(String job_requirements) {
        this.job_requirements = job_requirements;
    }

    public String getJob_requirements() {
        return job_requirements;
    }
}

// Adapter for below
public static class JobRequirements_Adapter extends ArrayAdapter {
    private Context context;

    public JobRequirements_Adapter(Context context, ArrayList arrayList) {
        super(context, R.layout.item_job_requirements_et, arrayList);
        this.context = context;
        job_requirements_array = arrayList;
    }

    // Holds our shit together
    static class JobRequirements_ViewHolder {
        EditText job_requirements_et;
        Button job_requirements_delete_btn;
    }

    // Get the job_requirements editText View
    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        View view = convertView;
        if (view == null) {
            LayoutInflater layoutInflater = ((Activity) context).getLayoutInflater();
            JobRequirements_ViewHolder viewHolder = new JobRequirements_ViewHolder();
            view = layoutInflater.inflate(R.layout.item_job_requirements_et, parent, false);
            viewHolder.job_requirements_et = (EditText) view.findViewById(R.id.item_job_requirements_et);
            viewHolder.job_requirements_delete_btn = (Button) view.findViewById(R.id.item_job_requirements_delete_btn);
            view.setTag(viewHolder);
        }
        JobRequirements_ViewHolder viewHolder = (JobRequirements_ViewHolder) view.getTag();

        viewHolder.job_requirements_et.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                job_requirements_array.get(position).job_requirements = s.toString();
            }

            @Override
            public void afterTextChanged(Editable s) {
                job_requirements_array.get(position).job_requirements = s.toString();
            }
        });

        // Set delete button
        viewHolder.job_requirements_delete_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                job_requirements_array.remove(position);
                job_requirements_update(rootView, -item_job_requirements_height);
            }
        });

        viewHolder.job_requirements_et.setText(job_requirements_array.get(position).getJob_requirements());
        return view;
    }
}

// TODO put the below in its own class
// Object model for the fragment_posting_2 job_task_listView
public static class JobTask_Model {
    private int job_task_seekbar_value;
    private String job_task_name;

    public JobTask_Model(String job_task_name, int job_task_seekbar_value) {
        this.job_task_seekbar_value = job_task_seekbar_value;
        this.job_task_name = job_task_name;
    }

    public String getJob_task_name() {
        return job_task_name;
    }

    public int getJob_task_seekbar_value() {
        return job_task_seekbar_value;
    }
}

/**
 * This class is the adapter for the tasks
 **/
public static class JobTasks_Adapter extends ArrayAdapter {
    private final ArrayList<JobTask_Model> job_task_array;
    private Context context;

    public JobTasks_Adapter(Context context, ArrayList arrayList) {
        super(context, R.layout.item_job_task, arrayList);
        this.context = context;
        job_task_array = arrayList;
    }

    // Create static class for our special viewholder model, references item_job_task layout
    static class JobTask_ViewHolder {
        EditText job_task_name;
        SeekBar job_task_seekbar;
        Button job_task_delete;
    }

    // This gets the view
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (view == null) {
            JobTask_ViewHolder viewHolder = new JobTask_ViewHolder();
            LayoutInflater layoutInflater = ((Activity) context).getLayoutInflater();
            view = layoutInflater.inflate(R.layout.item_job_task, parent, false);
            // configure view holder
            viewHolder.job_task_name = (EditText) view.findViewById(R.id.job_task_name);
            viewHolder.job_task_seekbar = (SeekBar) view.findViewById(R.id.job_task_seekbar);
            viewHolder.job_task_delete = (Button) view.findViewById(R.id.job_task_delete);
            view.setTag(viewHolder);
        }
        JobTask_ViewHolder viewHolder = (JobTask_ViewHolder) view.getTag();

        viewHolder.job_task_name.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                job_task_array.get(position).job_task_name = s.toString();
                Log.e("position", String.valueOf(position));
            }
        });

        // Set the bar value so it doesn't reset when we add another one in
        viewHolder.job_task_seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                job_task_array.get(position).job_task_seekbar_value = progress;
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });
        // Button to delete each task
        viewHolder.job_task_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                job_task_array.remove(position);
                job_task_update(rootView, -item_job_task_height);
            }
        });
        viewHolder.job_task_seekbar.setProgress(job_task_array.get(position).getJob_task_seekbar_value());
        viewHolder.job_task_name.setText(job_task_array.get(position).getJob_task_name());
        return view;
    }
}

点击之前:

Before clicking

点击后:

After Clicking

0 个答案:

没有答案