只是一个基本问题:如果我有几十个属于ListAdapter的EditText字段,那么各个EditText字段如何知道它们属于哪一行?
目前我正在使用TextWatcher来监听文本输入.我已经尝试扩展TextWatcher,以便我可以将EditText的位置传递给TextWatcher的构造函数.
但是,当弹出软键盘时,与各种EditText字段对应的位置会随机播放.
如何跟踪EditText字段到正确的位置?
我正在使用GridView来解决问题.每个项目的布局都是一个ImageView,其下方有TextView和EditText字段.
每个EditText的文本都保存在一个名为strings的全局String数组中.它最初是空的,并由我的TextWatcher类更新.
public void initList() { ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this,R.layout.shape,strings) { @Override public View getView(final int position,View convertView,ViewGroup parent) { if (convertView == null) { convertView = LayoutInflater.from(getContext()).inflate(R.layout.shape,null); } final String theData = getItem(position); final EditText editText = (EditText) convertView.findViewById(R.id.shape_edittext); editText.setText(theData); editText.addTextChangedListener( new MyTextWatcher(position,editText) ); ImageView image = (ImageView) convertView.findViewById(R.id.shape_image); image.setBackgroundResource(images[position]); TextView text = (TextView) convertView.findViewById(R.id.shape_text); if (gameType == SHAPES_ABSTRACT) text.setText("Seq:"); else text.setVisibility(View.GONE); return convertView; } @Override public String getItem(int position) { return strings[position]; } }; grid.setAdapter(listAdapter); } private class MyTextWatcher implements TextWatcher { private int index; private EditText edittext; public MyTextWatcher(int index,EditText edittext) { this.index = index; this.edittext = edittext; } public void beforeTextChanged(CharSequence s,int start,int count,int after) {} public void onTextChanged(CharSequence s,int before,int count) {} public void afterTextChanged(Editable s) { strings[index] = s.toString(); } public void setIndex(int newindex) { index = newindex; } }
当我点击第一个EditText(见图片)时,EditText转移到笑脸下的那个.
解决方法
如果这是一个很好的用户界面设计,请不要考虑以下内容:
public class TestList { public void blah() { ArrayAdapter<DataBucket> listAdapter = new ArrayAdapter<DataBucket>() { @Override public View getView(int position,ViewGroup parent) { if (convertView == null) { convertView = LayoutInflater.from(getContext()).inflate(R.layout.testlayout,null); } final DataBucket dataBucket = getItem(position); final EditText editText = (EditText) convertView.findViewById(R.id.theText); editText.setText(dataBucket.getSomeData()); editText.addTextChangedListener(new TextWatcher() { public void beforeTextChanged(CharSequence charSequence,int i,int i1,int i2) { } public void onTextChanged(CharSequence charSequence,int i2) { } public void afterTextChanged(Editable editable) { dataBucket.setSomeData(editable.toString()); } }); return convertView; } }; } public static class DataBucket { private String someData; public String getSomeData() { return someData; } public void setSomeData(String someData) { this.someData = someData; } } }
‘DataBucket’是一个占位符.您需要使用您创建的任何类来存储在编辑文本中放入和编辑的数据. TextWatcher将引用引用的数据对象.滚动时,编辑文本框应使用当前数据更新,并应保存文本更改.您可能希望跟踪用户更改了哪些对象,以使数据/网络更新更有效.
*编辑*
要使用int位置而不是直接引用对象:
ArrayAdapter<DataBucket> listAdapter = new ArrayAdapter<DataBucket>() { @Override public View getView(final int position,ViewGroup parent) { if (convertView == null) { convertView = LayoutInflater.from(getContext()).inflate(R.layout.testlayout,null); } final DataBucket dataBucket = getItem(position); final EditText editText = (EditText) convertView.findViewById(R.id.theText); editText.setText(dataBucket.getSomeData()); editText.addTextChangedListener(new TextWatcher() { public void beforeTextChanged(CharSequence charSequence,int i2) { } public void onTextChanged(CharSequence charSequence,int i2) { } public void afterTextChanged(Editable editable) { getItem(position).setSomeData(editable.toString()); } }); return convertView; } };
*再次编辑*
我觉得有必要为后代说,我实际上不会这样编码.我猜你想要一个比String数组更结构化的数据,并且你在外面维护String数组,以及一个ArrayAdapter,所以它是一种奇怪的并行情况.但是,这样可以正常工作.
我将数据放在单个String数组中而不是多维数组中.原因是支持GridView的数据模型只是一个简单的列表.这可能违反直觉,但事实就是如此. GridView应该自行进行布局,如果留给自己的设备,将使用可变数量的单元格填充行,具体取决于您拥有的数据量和屏幕的宽度(AFAIK).
够聊天了.代码:
public class TestList extends Activity { private String[] guess; //Other methods in here,onCreate,etc //Call me from somewhere else. Probably onCreate. public void initList() { ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this,/*some resourse id*/,guess) { @Override public View getView(final int position,null); } final String theData = getItem(position); final EditText editText = (EditText) convertView.findViewById(R.id.theText); editText.setText(theData); editText.addTextChangedListener( new MyTextWatcher(position) ); return convertView; } }; gridView.setAdapter(listAdapter); } class MyTextWatcher extends TextWatcher { private int position; public MyTextWatcher(int position) { this.position = position; } public void afterTextChanged(Editable s) { guess[position] = s.toString(); } // other methods are created,but empty } }