[英]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 我的每个ListView项目都包含以下模式
[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. 每当我更改Editbox的文本时,单击它并使用键盘书写,例如,如果我写9,它将显示9,但是一旦键盘消失,该文本几乎消失了。 The edit text shows blank space. 编辑文本显示空白。
What is wrong? 怎么了?
What is point of using a View holder, when you are not re-using convertView
, you are always inflating a new View. 使用View持有者的意义在于,当您不重新使用convertView
,总是会膨胀一个新View。
I see a call to listView.invalidateViews()
in getView()
. 我在getView()
看到对listView.invalidateViews()
的调用。 This might end up with ListView
getting invalidated on each text Change. 这可能导致ListView
在每次文本更改时都失效。 On invalidated, getView()
is called and a new TextChangedListener
is being attached to the EditText
. 无效时,将调用getView()
,并将新的TextChangedListener
附加到EditText
。 All such Listeners are doing the same thing n times, a recipe for chaos. 所有这些侦听器都在做n次相同的事情,这是造成混乱的秘诀。
Also, you are not setting text back from where you saved it ( myScoreArray
) when you inflate the views. 另外,在膨胀视图时,也不会从保存文本的位置( myScoreArray
)重新设置文本。
That's not how you use ViewHolder. 那不是您使用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. listView中的EditText有问题,因为由于softKeyboard,它导致listView项移动。 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. 或者,创建一个对话框来编辑所选项目。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.