There is an really weird thing happening with my listview. I am creating an ListView with buttons and an editText.
我的listview发生了一件很奇怪的事情。我正在创建一个带有按钮和editText的列表视图。
It's disposed like this: [Button] [EditText] [Button], The buttons works like an "incrementer" and "decrementer" updating the numerical value of EditText in 1 unit per click. The problem is, when I click in an button, almost every time an editText of another list view element is changed (the editText of the clicked item is also changed). And if I click in a button of this erroneous changed item, it also changes the editText of the first one. They basically have the same reference of buttons and editText, although they have textViews with data, and this data is different between they.
它的处理方式是这样的:[按钮][EditText][按钮],按钮的工作方式类似于“incrementer”和“decrementer”,每点击1个单元,就会更新EditText的数值。问题是,当我单击一个按钮时,几乎每次都更改了另一个列表视图元素的editText(单击项目的editText也会更改)。如果我点击这个错误更改项目的按钮,它也会改变第一个的editText。它们基本上都有相同的按钮和editText的引用,尽管它们有数据的textview,而且它们之间的数据是不同的。
To accomplish that I created and custom adapter:
为了完成我创建的和自定义适配器:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null) {
convertView = mInflater.inflate(R.layout.lastproduct_row, null);
holder = new ViewHolder();
holder.btnAddQtd = (Button) convertView.findViewById(R.lastproduct_row.btn_add_qtd);
holder.btnSubQtd = (Button) convertView.findViewById(R.lastproduct_row.btn_sub_qtd);
holder.etQuantidade = (EditText) convertView.findViewById(R.lastproduct_row.et_quantidade);
TextView tv;
holder.tvList = new TextView[PRODUCTROW_INT_KEY.length];
for(int i = 0; i < PRODUCTROW_INT_KEY.length; i++) {
tv = (TextView) convertView.findViewById(PRODUCTROW_INT_KEY[i]);
holder.tvList[i] = tv;
}
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
HashMap<String, String> hm = productsList.get(position);
String key = hm.get(CODIGO_KEY);
for(int i = 0; i < PRODUCTROW_INT_KEY.length; i++) {
holder.tvList[i].setText(hm.get(PRODUCTROW_STR_KEY[i]));
}
holder.btnAddQtd.setTag(key+QTD_FLAG+ADD_ACTION);
holder.btnSubQtd.setTag(key+QTD_FLAG+SUB_ACTION);
holder.btnAddQtd.setOnClickListener(handle);
holder.btnSubQtd.setOnClickListener(handle);
if(novosEstoques.containsKey(key)) {
holder.etQuantidade.setText(MyParseFunctions.parseCentesimal(novosEstoques.get(key).getQuantidade()));
}
return convertView;
}
class ViewHolder {
private TextView []tvList;
private Button btnAddQtd, btnSubQtd;
private Button btnAddQtVol, btnSubQtVol;
private EditText etQuantidade, etQtVolume;
}
I added onClick listenners to the buttons, setting their tags with my listView element ID (concatenated with another informations). Then in my event listener I just get the button parent View (an LinearLayout) and get the EditText from that using getViewAt():
我将onClick侦听器添加到按钮,将它们的标记设置为我的listView元素ID(与其他信息连接)。然后,在我的事件监听器中,我只得到按钮父视图(一个LinearLayout),并从getViewAt()中获取EditText:
@Override
public void onClick(View v) {
String tag = (String) v.getTag();
if(tag.contains(QTD_FLAG)) {
String []info = ((String) v.getTag()).split(QTD_FLAG);
float qtd;
LinearLayout ll = (LinearLayout) v.getParent();
ll.setBackgroundColor(Color.rgb(0, 128, 30));
EditText et = (EditText) ll.getChildAt(2);
qtd = Float.parseFloat(et.getText().toString().replace(",", "."));
if(info[1].equals(ADD_ACTION)) {
qtd++;
}
else if(info[1].equals(SUB_ACTION)) {
if(qtd > 0)
qtd--;
}
Log.d("TESTE", "MODIFICAR KEY = "+info[0]);
et.setText(qtd+"");
}
}
I'm using an setBackgroundColor in this example to confirm that the LinearLayout instance is duplicated in the lisView. When I click an Button, it's painted in 2 different list view item.
在这个示例中,我使用了setBackgroundColor来确认线性布局实例在lisView中是重复的。当我点击一个按钮时,它被画在两个不同的列表视图项中。
Anyone can point me what could be doing this? I have found people with an duplicated ListView item, I don know if that is my case, cause I have TextView's inside my ListView, and they are not equal, only the LinearLayout portion with buttons and editText is "shared".
谁能告诉我这是怎么回事?我找到了一个有重复ListView项的人,我不知道这是不是我的情况,因为我有TextView在我的ListView中,它们不相等,只有带有按钮和editText的LinearLayout部分是“shared”。
I make some changes in my getView method and it's working now! It seems that every time the getView method is called i have not guarantee at all that my editTexts will be filled properly and I didn't realize that. So every getView call I make I set the editText value, if the user edit an ET value, I store it in a HashMap to restore in getView, if there is no entry in HashMap for the given editText, then I set it to the default value (zero):
我在getView方法中做了一些更改,现在它正在工作!似乎每次调用getView方法时,我都无法保证我的edittext将被正确填充,而我没有意识到这一点。所以每个getView调用我都设置了editText值,如果用户编辑了一个ET值,我将它存储在HashMap中以恢复getView,如果给定editText的HashMap中没有条目,那么我将它设置为默认值(0):
...
if(convertView == null) {
holder = new ViewHolder();
holder.btnAddQtd = (Button) convertView.findViewById(R.lastproduct_row.btn_add_qtd);
holder.btnSubQtd = (Button) convertView.findViewById(R.lastproduct_row.btn_sub_qtd);
holder.etQuantidade = (EditText) convertView.findViewById(R.lastproduct_row.et_quantidade);
//Now it is easier to get etQuantidade reference in button
//click handle, I just have to do:
// public onClick(View v) {
// EditText etButtonAssociated = (EditText) v.getTag();
// ...
// }
holder.btnAddQtd.setTag(holder.etQuantidade);
holder.btnSubQtd.setTag(holder.etQuantidade);
holder.btnAddQtd.setOnClickListener(handle);
holder.btnSubQtd.setOnClickListener(handle);
...
}
else {
...
}
holder.etQuantidade.setTag(key);
if(novosEstoques.containsKey(key)) {
holder.etQuantidade.setText(MyParseFunctions.parseCentesimal(novosEstoques.get(key).getQuantidade()));
}
else {
holder.etQuantidade.setText("0");
}
return convertView;
1 个解决方案
#1
0
Israel,
以色列,
After looking over your code, I was wondering about an implementation decision you have made. Since each Button is "bound" to a particular EditText
, have you considered setting the Tag
of those Buttons
to the EditText
? The Tag
may be any Object
including a UI element. This is especially useful for dynamic UI elements, such as a runtime populated list.
在查看了您的代码之后,我想知道您已经做出了一个实现的决定。由于每个按钮都“绑定”到特定的EditText,您是否考虑将这些按钮的标记设置为EditText?标签可以是任何对象,包括UI元素。这对于动态UI元素尤其有用,比如运行时列表。
Since this is handled in your Adapter
you wouldn't have to worry about duplicate Parents and such. Additionally, you could avoid having to worry about "finding" the control in your onClick()
because you would have it (It's the tag). I'm not sure exactly what your project needs are, but this seems like a potentially viable solution, unless you need those Buttons
to accomplish other tasks.
因为这是在您的适配器中处理的,所以您不必担心重复的父母。此外,您可以避免在onClick()中“查找”控件,因为您将拥有它(它是标记)。我不确定您的项目需要什么,但是这看起来是一个可能可行的解决方案,除非您需要这些按钮来完成其他任务。
Note of Caution Just make sure that you erase the Tags' references to the EditText
when you are done. Otherwise, you run the risk of leaking some memory.
注意,请确保在完成后删除标签对EditText的引用。否则,您就会冒泄露内存的风险。
FuzzicalLogic
FuzzicalLogic
#1
0
Israel,
以色列,
After looking over your code, I was wondering about an implementation decision you have made. Since each Button is "bound" to a particular EditText
, have you considered setting the Tag
of those Buttons
to the EditText
? The Tag
may be any Object
including a UI element. This is especially useful for dynamic UI elements, such as a runtime populated list.
在查看了您的代码之后,我想知道您已经做出了一个实现的决定。由于每个按钮都“绑定”到特定的EditText,您是否考虑将这些按钮的标记设置为EditText?标签可以是任何对象,包括UI元素。这对于动态UI元素尤其有用,比如运行时列表。
Since this is handled in your Adapter
you wouldn't have to worry about duplicate Parents and such. Additionally, you could avoid having to worry about "finding" the control in your onClick()
because you would have it (It's the tag). I'm not sure exactly what your project needs are, but this seems like a potentially viable solution, unless you need those Buttons
to accomplish other tasks.
因为这是在您的适配器中处理的,所以您不必担心重复的父母。此外,您可以避免在onClick()中“查找”控件,因为您将拥有它(它是标记)。我不确定您的项目需要什么,但是这看起来是一个可能可行的解决方案,除非您需要这些按钮来完成其他任务。
Note of Caution Just make sure that you erase the Tags' references to the EditText
when you are done. Otherwise, you run the risk of leaking some memory.
注意,请确保在完成后删除标签对EditText的引用。否则,您就会冒泄露内存的风险。
FuzzicalLogic
FuzzicalLogic