简体   繁体   English

Android-自定义ListView中的TextView在滚动时会丢失其内容

[英]Android - TextView in a Custom ListView loses its content while scrolling

I have came across these type of questions a lot, But none of the answers solved my problem. 我遇到过很多这类问题,但是没有一个答案能解决我的问题。

I am developing a mobile shopping application model. 我正在开发一个移动购物应用程序模型。 I have a custom ListView with an ImageView , and EditText (quantity), 2 Buttons for increasing and decreasing the quantity, 3 TextView s one for item Name and other two for prices and I am dynamically loading a number of Buttons for NetWeights based on the requirement through Java code and I am implementing it using CustomAdapter(Class) extending BaseAdapter and getView(). 我有一个带有ImageViewEditText (数量)的自定义ListView ,2个用于增加和减少数量的Buttons ,3个TextView一个用于商品名称,另外两个用于价格,并且我正在动态加载基于NetWeights的多个按钮通过Java代码实现需求,而我正在使用CustomBase(Class)扩展BaseAdapter和getView()来实现它。

Problem 1: 问题一:

When I use the condition if(convertView==null), setTag(holder) and getTag() , the dynamically loaded Buttons are getting duplicated and gets doubled in number. 当我使用条件if(convertView==null), setTag(holder)getTag() ,动态加载的Button将重复并且数量增加一倍。

Problem 2: 问题2:

While scrolling the custom ListView I am loosing my data on 2 of the TextView s while the other TextView (name) is fine because in that TextView I don't Change data on runtime. 在滚动自定义ListView时,我在2个TextView上丢失了数据,而另一个TextView (名称)很好,因为在该TextView中,我不会在运行时更改数据。 On the other two TextView s(prices) I change the data on runtime using ButtonClicks(increase and decrease) where at that time when I scroll the two TextView s, it looses its content. 在其他两个TextView (价格),我在运行时使用ButtonClicks(增加和减少)来更改数据,而当我滚动两个TextView ,它会松散其内容。 Actually I had this same problem with the EditText(displaying quantity), I solved it using the TextWatcher by getting the value and passing it in an array and again setting it. 实际上,我在EditText(显示数量)上也遇到了同样的问题,我通过使用TextWatcher解决了这个问题,方法是获取值并将其传递到数组中,然后再次进行设置。 I used the same technique (ie) the TextWatcher for the TextView s But that did'nt Help. 我对TextView使用了相同的技术(即TextWatcher ),但这没有帮助。 I also tried removing the TextWatcher and setting the text with the array and adding TextWatcher . 我还尝试了删除TextWatcher并使用数组设置文本并添加TextWatcher Still I am loosing my content in those TextView s 我仍然在那些TextView丢失了我的内容

Any Suggestions?????? 有什么建议么??????

Problem 1: While using ViewHolder (setTag(), getTag()) we will be reusing the views. 问题1:在使用ViewHolder(setTag(),getTag())时,我们将重用视图。 For example: Let's say we have 10 listview items and the screen can display 3 items at a time. 例如:假设我们有10个列表视图项目,并且屏幕一次可以显示3个项目。 Lets say we add 2 buttons to list item 1. First time when we load the listview we will see two buttons. 可以说我们在列表项1中添加了2个按钮。第一次加载列表视图时,我们将看到两个按钮。 Lets say now we scroll down and come back to the list item 1 again, as we are reusing the views, we already have 2 buttons and if we have the code to add buttons using getTag() then 2 more buttons will be added. 假设现在我们向下滚动并再次回到列表项1,因为我们正在重新使用视图,所以我们已经有2个按钮,如果我们有使用getTag()添加按钮的代码,则会再添加2个按钮。 I think this may be causing the issue of button duplication. 我认为这可能是造成按钮重复的问题。 To add little more information, list item 1, list item 4, list item 7 and list item 10 will all use same view, as our screen in this example can display just 3 items. 为了添加更多信息,列表项1,列表项4,列表项7和列表项10都将使用相同的视图,因为在此示例中,我们的屏幕只能显示3个项目。

Problem 2: One idea is to store all these text values in the arrayList. 问题2:一种想法是将所有这些文本值存储在arrayList中。 For example, lets assume we have one textView inside each list item that can be incremented/ decremented. 例如,假设我们在每个列表项中都有一个可以递增/递减的textView。 Set textWatcher on this textView and whenever text changes add it to the arrrayList using 'position' as the index. 在此textView上设置textWatcher,并且每当文本更改时都使用'position'作为索引将其添加到arrrayList中。 Inside getView method when you do getTag() try to set the textView using previously stored text (that was stored inside the arrayList)if exists. 在执行getTag()时,在getView方法内部尝试使用以前存储的文本(存储在arrayList中)设置textView(如果存在)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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