EditText in the list does not work the way they should

I have a problem with EditText fields in ListActivity .

The code meets all the requirements, but the functionality is strange, entering in the first field and hiding the keyboard, after that the text appears in another editing field.

Help me with my logical problem

 package com.example.helloandroid; import android.app.ListActivity; import android.content.Context; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.EditText; import android.widget.TextView; public class AddComp extends ListActivity { static final int DATE_DIALOG_ID = 0; private class EfficientAdapter extends BaseAdapter { private LayoutInflater mInflater; private String[] attitude_names; private String[] attitude_values; 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) { 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); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.Attitude_Name.setText(attitude_names[position]); holder.Attitude_Value.setHint(attitude_names[position]); attitude_values[position] = holder.Attitude_Value.getText().toString(); return convertView; } class ViewHolder { TextView Attitude_Name; EditText Attitude_Value; } @Override public int getCount() { return attitude_names.length; } } public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setListAdapter(new EfficientAdapter(this)); setContentView(R.layout.addcomp); } } 
0
source share
3 answers

The problem was solved by adding an entry in the manifest and using TextWatcher (this is necessary because presenting one line of the list several times calls several times, which means that for 500 entries in the list the program uses only a few lines of the line .view-class should be more efficient) therefore, you must use a text observer that stores the changed data in an additional datastructur for the exsample 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; } } 
+3
source

It 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 included a HashMap object that will make sure that the EditText contains the value. And when you scroll through the list, it will be displayed again by calling its getView method.

In this code, when you first load the listview, all your edittexts will be without text.once, you enter some text, this will be noted in myList.So, when you render the list again, your text will be prevented.

+1
source

I found the reason for the strange focus behavior android:windowSoftInputMode="adjustPan" add android:windowSoftInputMode="adjustPan" as the value for the activity in the Project Manifest, but, on the other hand, the problem that often changes several edidfield values ​​is not resolved rightnow

0
source

Source: https://habr.com/ru/post/901410/


All Articles