Android 处理 ListView 中的多个 EditText 字段
只是一个基本问题:如果我有几十个 EditText 字段是 ListAdapter 的一部分,那么各个 EditText 字段如何知道它们属于哪一行?
目前我正在使用 TextWatcher 来监听文本输入。我尝试扩展 TextWatcher,以便可以将 EditText 的位置传递给 TextWatcher 的构造函数。
然而,当软键盘弹出时,各个EditText字段对应的位置会随机排列。
如何跟踪 EditText 字段到正确的位置?
我正在使用 GridView 来布局。每个项目的布局是一个 ImageView,其下方有一个 TextView 和 EditText 字段。
每个 EditText 的文本都保存在称为 strings 的全局字符串数组中。它最初是空的,并由我的 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 start, int before, int count) {}
public void afterTextChanged(Editable s) { strings[index] = s.toString(); }
public void setIndex(int newindex) { index = newindex; }
}
当我单击第一个 EditText(参见图片)时,EditText 会切换到笑脸下方的 EditText。
Just a basic question: If I have several dozen EditText fields that are part of a ListAdapter, how can the individual EditText fields know to which row they belong?
Currently I am using TextWatcher to listen for text input. I have tried extending TextWatcher so that I can pass in the position of the EditText to TextWatcher's constructor.
However, when the soft keyboard pops up, the positions that correspond to the various EditText fields shuffle.
How can I track the EditText fields to their proper position?
I am using a GridView to lay things out. The layout of each item is an ImageView with a TextView and EditText field below it.
The text for each EditText is held in a global String array called strings. It is initially empty, and is updated by my TextWatcher class.
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 start, int before, int count) {}
public void afterTextChanged(Editable s) { strings[index] = s.toString(); }
public void setIndex(int newindex) { index = newindex; }
}
When I click into the first EditText (see picture), the EditText shifts to the one under the smiley face.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不考虑这是否是一个好的 UI 设计,您可以这样做:
“DataBucket”是一个占位符。您需要使用您创建的任何类来存储在编辑文本中放入和编辑的数据。 TextWatcher 将具有对所引用的数据对象的引用。当您滚动时,编辑文本框应使用当前数据进行更新,并且应保存文本更改。您可能希望跟踪用户更改了哪些对象,以使数据/网络更新更加高效。
* 编辑 *
使用 int 位置而不是直接引用对象:
* 再次编辑 *
我觉得有必要为后代说,我实际上不会这样编码。我猜你想要比 String 数组更结构化的数据,并且你在外部维护 String 数组以及 ArrayAdapter,所以这是一种奇怪的并行情况。然而,这会很好地工作。
我的数据位于单个字符串数组中,而不是多维数组中。原因是因为支持 GridView 的数据模型只是一个简单的列表。这可能违反直觉,但事实就是如此。 GridView 应该自己进行布局,如果留给它自己的设备,将使用可变数量的单元格填充行,具体取决于您拥有的数据量和屏幕的宽度(AFAIK)。
聊够了。代码:
Not taking into account if this is a good UI design, here's how you'd do it:
'DataBucket' is a placeholder. You need to use whatever class you created to store the data that gets put into and edited in the edit text. The TextWatcher will have a reference to the data object referenced. As you scroll, the edit text boxes should get updated with current data, and text changes should be saved. You may want to track which objects were changed by the user to make data/network updates more efficient.
* Edit *
To use an int position rather than directly referencing the object:
* Edit Again *
I feel compelled to say for posterity, I wouldn't actually code it this way. I'd guess you want a little more structured data than a String array, and you're maintaining the String array outside, as well as an ArrayAdapter, so its sort of a weird parallel situation. However, this will work fine.
I have my data in a single String array rather than a multi-dimensional array. The reason is because the data model backing the GridView is just a simple list. That may be counterintuitive, but that's the way it is. GridView should do the layout itself, and if left to its own devices, will populate the row with variable numbers of cells, depending on how much data you have and how wide your screen is (AFAIK).
Enough chat. The code:
要跟踪行号,
EditText
中的每个侦听器都必须保留对列表中项目的引用,并使用getPosition(item)
获取中的位置列表视图
。我的示例使用Button
但我认为它可以应用于EditText
。To track the row number, each listener in
EditText
has to keep a reference to an item in a list and usegetPosition(item)
to get the position in aListView
. My example usesButton
but I think that it can be applied toEditText
.可能值得考虑是否需要将编辑文本存储在列表单元格中?当用户一次只编辑一个时,这似乎有点不必要。
虽然我不知道您的应用程序是如何设计的,但我建议稍微重新考虑您的用户体验,以便当按下列表项时会出现单个文本编辑供他们编辑。这样,您就可以像通常使用列表适配器一样获取列表项引用,在用户编辑时存储它,并在完成后更新它。
It might be worth considering whether you need the edit texts to be stored in the list cells? It seems a little bit unnecessary when the user will only be editing one at a time.
Whilst I do not know how your app is designed I would recommend rethinking your user experience slightly so that when an list item is pressed a single text edit appears for them to edit. That way you can just get the list items reference as you normally would with a list adapter, store it whilst the user is editing and update it when they have finished.
我不确定这是否是一个很好的设计,因为一旦列表视图滚动,EditText 内容很可能会出现问题(内容乱序、文本丢失)。考虑尝试 m6tt 的想法。
但如果你真的想按照自己的方式去做,你可以发布一些代码,特别是你的 TextWatcher 的代码吗?
i'm not sure if that's a nice design you have, as the EditText content will have a good chance of having problems (shuffling content, missing text) once your listview is scrolled. consider trying out m6tt's idea.
but if you really want to go your way, can you post some code, specifically of your TextWatcher?
我试图解决这个问题,正如你所看到的,有一个简单的方法 - 我在这里发布答案,因为它可能对某人有用。
列表视图时无法获取位置 ->编辑文本有一个文本观察器。
这是对我有用的解决方案:
在 get 视图中 -
添加了以下行
当我添加文本观察器侦听器来编辑文本时,我还在afterTextChanged 中
-给出了正确的位置编号,您可以根据位置编号进行修改。
I tried to solve this and as you can see there is a simple method - I am posting the answer here as it might be useful for someone.
Not able to get the position when list view -> edit text has a text watcher.
This is the solution that worked for me :
In get view -
when I add the text watcher listener to edit text, I also added the below line
in your afterTextChanged -
Gives the correct position number and you can do modifications based on the position number.