获取Listview中EditText的值简单的解决方法



http://www.webplusandroid.com/creating-listview-with-edittext-and-textwatcher-in-android/


这个方法感觉很简单,原理:使用了final变量。


Today I am going to explain how to create a ListView with EditText and why will we need a TextWatcher to implement the same.

Before starting the topic, let us know why this topic is necessary.

Issue:

As we know ListView reuses the view of  ListItem as we scroll the whole list.

So problem arises when we have a custom  ListView with  EditText where if we enter any value in the first EditText and start scrolling then the value of EditText one is copied to another the EditTexts one by one as we scroll the listview .

This happens as the listview reuses the view and as the other listitem from another view i.e. the view which is not seen scrolls upwards it reuses the old lists view and hence the old value of that view is seen in the new edittext.

The issue can be seen it the below image:

device-2014-08-03-181411

Now Scroll the List:

device-2014-08-03-181457

From above we can see that Text1 EdiText data is copied to Text10 and so on and so forth. The above issue can be resolved and the code to resolve the above issue is as follows:

First create your parent ListView layout:

lyt_listview_activity.xml

Then Create ListItem which will be your listview items:

lyt_listview_list.xml

Now this will be your Activity code:

ListviewActivity.java

This will be your manisfest file: Notice the tag windowSoftInputMode=”adjustPan” this will resolve keyboard focus issue in editText in listview

AndroidManifest.xml

In above activity code the TextWatcher code handles the issue of duplicating of one editText value to other. This will happen using a temporary  variable which will store the previous value of editText with respect to its position and will set it when the same editText comes into focus or view.

 

Please comment if you have any doubts or any other issue. I will be happy to help…

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值