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:
Now Scroll the List:
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
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
?>
>
ListView
>
>
>
|
Then Create ListItem which will be your listview items:
lyt_listview_list.xml
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
?>
>
TextView
>
EditText
>
>
>
>
|
Now this will be your Activity code:
ListviewActivity.java
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
;
;
;
;
;
;
;
;
;
;
;
;
{
=
;
;
@Override
{
// TODO Auto-generated method stub
;
;
;
;
;
;
}
{
@Override
{
// TODO Auto-generated method stub
{
}
;
}
@Override
{
// TODO Auto-generated method stub
;
}
@Override
{
// TODO Auto-generated method stub
;
}
@Override
{
//ViewHolder holder = null;
;
{
;
;
;
;
;
{
;
}
;
;
;
{
@Override
{
// TODO Auto-generated method stub
}
@Override
,
{
// TODO Auto-generated method stub
}
@Override
{
// TODO Auto-generated method stub
;
}
;
;
}
{
;
;
;
}
}
}
|
This will be your manisfest file: Notice the tag windowSoftInputMode=”adjustPan” this will resolve keyboard focus issue in editText in listview
AndroidManifest.xml
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
>
sdk
>
application
>
activity
>
>
>
>
>
>
>
>
|
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…