EditText in a list aren't working the way they should

前端 未结 3 1332
猫巷女王i
猫巷女王i 2021-01-27 16:01

I have a problem with EditText fields in a ListActivity.

The code complies all right, but the functionality is strange, typing in the first fie

相关标签:
3条回答
  • 2021-01-27 16:46

    I found the reason the behaviour of the strange focusing one has to add android:windowSoftInputMode="adjustPan" as value to the activity in the Projects Manifest but on the other hand the problem that often more than one value of the edidfield is changed is not solved rightnow

    0 讨论(0)
  • 2021-01-27 16:51

    This will help you

    ``

    private class EfficientAdapter extends BaseAdapter {
                private LayoutInflater mInflater;
                private String[] attitude_names;
                public String[] attitude_values;
                private String name;
        public static HashMap<Integer,String> myList=new HashMap<Integer,String>();
    
                public EfficientAdapter(Context context) {
                    mInflater = LayoutInflater.from(context);
                    attitude_names = context.getResources().getStringArray(R.array.COMP_ATTITUDE_NAME);
                    attitude_values = new String[attitude_names.length];
                }
        // initialize myList
        for(int i=0;i<attitude_names.length;i++)
        {
           myList.put(i,"");
        }
    
    
                public Object getItem(int position) {
                    return position;
                }
    
                public long getItemId(int position) {
                    return position;
                }
    
                public View getView(int position, View convertView, ViewGroup parent) {
                    final ViewHolder holder;
    
                    if (convertView == null) {
                        convertView = mInflater.inflate(R.layout.addcomp_attitude_row, null);
    
                        holder = new ViewHolder();
                        holder.Attitude_Name = (TextView) convertView.findViewById(R.id.addcomp_att_name);
                        holder.Attitude_Value = (EditText) convertView.findViewById(R.id.addcomp_att_value);
                        holder.Attitude_Value.addTextChangedListener(new TextWatcher()
                            {
                                public void afterTextChanged(Editable edt) 
                                {
                                     myList.put(pos,s.toString.trim());
                                    attitude_values[holder.ref] = edt.toString();
                                }
    
                                public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {}
    
                                public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
                                    //attitude_values[ref] = Attitude_Value.getText().toString();
                                }
                            });
    
                        convertView.setTag(holder);
                    } else {
                        holder = (ViewHolder) convertView.getTag();
                    }
    
    
                    holder.ref=position;
                    holder.Attitude_Name.setText(attitude_names[position]);
                    holder.Attitude_Value.setHint(attitude_names[position]);
                    holder.Attitude_Value.setText(myList.get(position));
    
    
    
    
                    return convertView;
                }
    
                class ViewHolder {
                    TextView Attitude_Name;
                    EditText Attitude_Value; 
                    int ref;
    
    
    
                }
    
                @Override
                public int getCount() {
                    return attitude_names.length;
                }
            }
    

    Here I have included a HashMap object which will keep on eye of what EditText contains value.And when you scroll the listview,it will be rendered again by calling its getView method.

    In this code,when you firstly load listview,all your edittext will be with no text.once you enter some text,it will be noted in myList.So when you again render the list,your text would be prevented.

    0 讨论(0)
  • 2021-01-27 16:56

    Problem solved by adding Entry in manifest and the use of a TextWatcher (this is needed because the view of one list row is internly called several times that means that for 500 list entries the programm uses only afew intances of the row.view-class to be mor efficient) therfore it is need to use a text watcher that saves the changed data in a extra datastructur for exsample an array..

            private class EfficientAdapter extends BaseAdapter {
                private LayoutInflater mInflater;
                private String[] attitude_names;
                public String[] attitude_values;
                private String name;
    
                public EfficientAdapter(Context context) {
                    mInflater = LayoutInflater.from(context);
                    attitude_names = context.getResources().getStringArray(R.array.COMP_ATTITUDE_NAME);
                    attitude_values = new String[attitude_names.length];
                }
    
                public Object getItem(int position) {
                    return position;
                }
    
                public long getItemId(int position) {
                    return position;
                }
    
                public View getView(int position, View convertView, ViewGroup parent) {
                    final ViewHolder holder;
    
                    if (convertView == null) {
                        convertView = mInflater.inflate(R.layout.addcomp_attitude_row, null);
    
                        holder = new ViewHolder();
                        holder.Attitude_Name = (TextView) convertView.findViewById(R.id.addcomp_att_name);
                        holder.Attitude_Value = (EditText) convertView.findViewById(R.id.addcomp_att_value);
                        holder.Attitude_Value.addTextChangedListener(new TextWatcher()
                            {
                                public void afterTextChanged(Editable edt) 
                                {
                                    attitude_values[holder.ref] = edt.toString();
                                }
    
                                public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {}
    
                                public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
                                    //attitude_values[ref] = Attitude_Value.getText().toString();
                                }
                            });
    
                        convertView.setTag(holder);
                    } else {
                        holder = (ViewHolder) convertView.getTag();
                    }
    
    
                    holder.ref=position;
                    holder.Attitude_Name.setText(attitude_names[position]);
                    holder.Attitude_Value.setHint(attitude_names[position]);
                    holder.Attitude_Value.setText(attitude_values[position]);
    
    
    
    
                    return convertView;
                }
    
                class ViewHolder {
                    TextView Attitude_Name;
                    EditText Attitude_Value; 
                    int ref;
    
    
    
                }
    
                @Override
                public int getCount() {
                    return attitude_names.length;
                }
            }
    
    0 讨论(0)
提交回复
热议问题