简体   繁体   中英

Edit Text strange behavior in ListView

I am using following code to show edit boxes in list view

My each ListView item contain following pattern

    [EditText 1] [EditText 2] [EditText 3 ][EditText  4][EditText 5]



       class CustomArrayAdapterForScore extends ArrayAdapter<ScoreModel> 
        {
            public CustomArrayAdapterForScore() 
            {
                super(MeetScore.this, R.layout.score_event_layout, myScoreArray);
            }

            @Override
            public View getView(final int position, View convertView, ViewGroup parent) 
            {
                    View view = null;
                    LayoutInflater inflator = MeetScore.this.getLayoutInflater();
                    view = inflator.inflate(R.layout.score_event_layout, parent, false);
                    final ViewHolder viewHolder = new ViewHolder();

                    viewHolder.title = (TextView) view.findViewById(R.id.title);

                    viewHolder.box1 = (EditText) view.findViewById(R.id.box1);
                    viewHolder.box2 = (EditText) view.findViewById(R.id.box2);
                    viewHolder.box3 = (EditText) view.findViewById(R.id.box3);
                    viewHolder.box4 = (EditText) view.findViewById(R.id.box4);
                    viewHolder.box5 = (EditText) view.findViewById(R.id.box5);
                    viewHolder.box6 = (EditText) view.findViewById(R.id.box6);

                    viewHolder.box1.addTextChangedListener(new TextWatcher() 
                    {
                        public void onTextChanged(CharSequence c, int arg1, int arg2, int arg3) 
                        {
                            ScoreModel score = myScoreArray.get(position);
                            score.box1 = Integer.valueOf(viewHolder.box1.getText().toString());
                            myScoreArray.set(position, score);
                            listView.invalidateViews();
                        }
                        public void beforeTextChanged(CharSequence c, int arg1, int arg2, int arg3){} 
                        public void afterTextChanged(Editable arg0){} 
                    });

    view.tag(viewHolder);
return view;

Whenver I change the text of Editbox, click on it and write using keyboard, e,g if i write 9 it shows 9 but as soon as the keyboard disappears the text is all but gone. The edit text shows blank space.

What is wrong?

  1. What is point of using a View holder, when you are not re-using convertView , you are always inflating a new View.

  2. I see a call to listView.invalidateViews() in getView() . This might end up with ListView getting invalidated on each text Change. On invalidated, getView() is called and a new TextChangedListener is being attached to the EditText . All such Listeners are doing the same thing n times, a recipe for chaos.

  3. Also, you are not setting text back from where you saved it ( myScoreArray ) when you inflate the views.

That's not how you use ViewHolder. This is how you are supposed to use it:

public View getView(...)
{
View rootView=convertView;
ViewHolder holder;
if(rootView==null)
  {
  rootView=inflater....
  rootView.setTag(holder =new ViewHolder());
  //...
  }
else holder=(ViewHolder) rootView.getTag();
//...
}

EditText in a listView is problematic, as it causes the listView items to move because of the softKeyboard. Either set the manifest:

android:windowSoftInputMode="adjustPan"

(I'm not sure that is the correct value). Or, make a dialog for editing the selected item.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM