Listview中的EditText超过6.0的android OS

时间:2016-12-09 09:02:33

标签: android listview

我有一个listview列表视图包含EditText和Spinner。 当我为edittext更改或添加新值时,为listview添加一个新项目==>这些价值观并没有保留。 我已经修复了os< 6.0。

Step1:在我的AndroidManifest.xml中

<activity
      android:launchMode="singleTop"
      android:screenOrientation="portrait"
      android:windowSoftInputMode="adjustPan"
      android:name=".business.main.TabHostFragmentMainActivity" />

第2步:在listview中

<ListView
       android:id="@+id/lvWeekday"
       android:descendantFocusability="beforeDescendants"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_marginLeft="@dimen/dimen_10" />

步骤3:在适配器

        //Fill EditText with the value you have in data source
        holder.edCost.setText(course.getCourseCost());
        holder.edCost.setId(position);

        //we need to update adapter once we finish with editing
        holder.edCost.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            public void onFocusChange(View v, boolean hasFocus) {
                if (!hasFocus) {
                    //final int position = v.getId();
                    final EditText Caption = (EditText) v;
                    course.setCourseCost(Caption.getText().toString());
                }
            }
        });

这是我的适配器。

public class BusinessCourseJoinAdapter extends ArrayAdapter<Course>{

    private List<Course> courseList;
    private Context context;
    private LayoutInflater inflater;
    private OnItemClickListener onItemClickListener;
    private String[] timeSetup = {"00:00", "01:00", "02:00", "03:00", "04:00", "05:00", "06:00", "07:00", "08:00", "09:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00", "17:00", "18:00", "18:00", "19:00", "20:00", "21:00", "22:00", "23:00","24:00"};

    public BusinessCourseJoinAdapter(Context context, int resource, List<Course> objects,OnItemClickListener listener) {
        super(context, resource, objects);
        this.context = context;
        this.courseList = objects;
        this.onItemClickListener = listener;
        this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final BusinessCourseJoinHolder holder;
        if (convertView == null){
            holder = new BusinessCourseJoinHolder();
            convertView = inflater.inflate(R.layout.item_business_join2_add_room, parent, false);

            holder.spinnerTimeStart = (Spinner) convertView.findViewById(R.id.spinnerTimeStart);
            holder.spinnerTimeEnd = (Spinner)convertView.findViewById(R.id.spinnerTimeEnd);

            ArrayAdapter<String> adapterWDStart = new ArrayAdapter<String>(context, R.layout.item_spinner_time, timeSetup);
            ArrayAdapter<String> adapterWDEnd = new ArrayAdapter<String>(context, R.layout.item_spinner_time, timeSetup);

            holder.spinnerTimeStart.setAdapter(adapterWDStart);
            holder.spinnerTimeEnd.setAdapter(adapterWDEnd);

            holder.edCost = (EditText) convertView.findViewById(R.id.edCost);
            holder.btDelete = (Button)convertView.findViewById(R.id.btDelete);

            convertView.setTag(holder);
        }else {

            holder = (BusinessCourseJoinHolder)convertView.getTag();
        }
        final Course course = courseList.get(position);
        if (position>0){
            holder.btDelete.setVisibility(View.VISIBLE);
        }else {
            holder.btDelete.setVisibility(View.INVISIBLE);
        }

        holder.spinnerTimeStart.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                course.setCourseTimeStart(""+position);
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
        holder.spinnerTimeStart.setSelection(Integer.parseInt(course.getCourseTimeStart()));

        holder.spinnerTimeEnd.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                course.setCourseTimeEnd(""+position);
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });

        holder.spinnerTimeEnd.setSelection(Integer.parseInt(course.getCourseTimeEnd()));

        //Fill EditText with the value you have in data source
        holder.edCost.setText(course.getCourseCost());
        holder.edCost.setId(position);

        //we need to update adapter once we finish with editing
        holder.edCost.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            public void onFocusChange(View v, boolean hasFocus) {
                if (!hasFocus) {
                    //final int position = v.getId();
                    final EditText Caption = (EditText) v;
                    course.setCourseCost(Caption.getText().toString());
                }
            }
        });


        holder.btDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                course.setCourseDelete(true);
                onItemClickListener.onItemClick(position);
            }
        });

        return convertView;
    }


    public static class BusinessCourseJoinHolder{

        Spinner spinnerTimeStart;
        Spinner spinnerTimeEnd;
        EditText edCost;
        Button btDelete;
    }
}

请帮帮我。

1 个答案:

答案 0 :(得分:0)

我有一个解决方案。不要用户ViewHolder它会正常工作

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final BusinessCourseJoinHolder holder;
        //if (convertView == null){
            holder = new BusinessCourseJoinHolder();
            convertView = inflater.inflate(R.layout.item_business_join2_add_room, parent, false);

            holder.spinnerTimeStart = (Spinner) convertView.findViewById(R.id.spinnerTimeStart);
            holder.spinnerTimeEnd = (Spinner)convertView.findViewById(R.id.spinnerTimeEnd);

            ArrayAdapter<String> adapterWDStart = new ArrayAdapter<String>(context, R.layout.item_spinner_time, timeSetup);
            ArrayAdapter<String> adapterWDEnd = new ArrayAdapter<String>(context, R.layout.item_spinner_time, timeSetup);

            holder.spinnerTimeStart.setAdapter(adapterWDStart);
            holder.spinnerTimeEnd.setAdapter(adapterWDEnd);

            holder.edCost = (EditText) convertView.findViewById(R.id.edCost);
            holder.btDelete = (Button)convertView.findViewById(R.id.btDelete);

            convertView.setTag(holder);
        //}else {
        //    holder = (BusinessCourseJoinHolder)convertView.getTag();
        //}
        final Course course = courseList.get(position);
        if (position>0){
            holder.btDelete.setVisibility(View.VISIBLE);
        }else {
            holder.btDelete.setVisibility(View.INVISIBLE);
        }

        holder.spinnerTimeStart.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                course.setCourseTimeStart(""+position);
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
        holder.spinnerTimeStart.setSelection(Integer.parseInt(course.getCourseTimeStart()));

        holder.spinnerTimeEnd.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                course.setCourseTimeEnd(""+position);
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });

        holder.spinnerTimeEnd.setSelection(Integer.parseInt(course.getCourseTimeEnd()));

        //Fill EditText with the value you have in data source
        holder.edCost.setText(course.getCourseCost());
        holder.edCost.setId(position);

        //we need to update adapter once we finish with editing
        holder.edCost.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            public void onFocusChange(View v, boolean hasFocus) {
                if (!hasFocus) {
                    //final int position = v.getId();
                    final EditText Caption = (EditText) v;
                    course.setCourseCost(Caption.getText().toString());
                }
            }
        });


        holder.btDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                course.setCourseDelete(true);
                onItemClickListener.onItemClick(position);
            }
        });

        return convertView;
    }

非常感谢。