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

lyt_listview_activity.xml

 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
 
    <ListView
        android:id="@+id/listViewMain"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>
 
</LinearLayout>

Then Create ListItem which will be your listview items:

lyt_listview_list.xml

lyt_listview_item.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
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:orientation="horizontal" >
 
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:textColor="@android:color/black"
        android:layout_height="wrap_content"
        android:text="15dp" />
 
    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@android:color/black"
        android:ems="10" >
 
        <requestFocus />
    </EditText>
 
</LinearLayout>

Now this will be your Activity code:

ListviewActivity.java

ListviewActivity.java

 
 
 
 
 
 

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
package com.example.testlistview;
 
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
 
public class ListviewActivity extends Activity {
 
    private String[] arrText =
            new String[]{"Text1","Text2","Text3","Text4"
            ,"Text5","Text6","Text7","Text8","Text9","Text10"
            ,"Text11","Text12","Text13","Text14","Text15"
            ,"Text16","Text17","Text18","Text19","Text20"
            ,"Text21","Text22","Text23","Text24"};
    private String[] arrTemp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lyt_listview_activity);
 
        arrTemp = new String[arrText.length];
        
        MyListAdapter myListAdapter = new MyListAdapter();
        ListView listView = (ListView) findViewById(R.id.listViewMain);
        listView.setAdapter(myListAdapter);
    }
 
    private class MyListAdapter extends BaseAdapter{
 
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            if(arrText != null && arrText.length != 0){
                return arrText.length;    
            }
            return 0;
        }
 
        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return arrText[position];
        }
 
        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }
 
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
 
            //ViewHolder holder = null;
            final ViewHolder holder;
            if (convertView == null) {
 
                holder = new ViewHolder();
                LayoutInflater inflater = ListviewActivity.this.getLayoutInflater();
                convertView = inflater.inflate(R.layout.lyt_listview_list, null);
                holder.textView1 = (TextView) convertView.findViewById(R.id.textView1);
                holder.editText1 = (EditText) convertView.findViewById(R.id.editText1);    
 
                convertView.setTag(holder);
 
            } else {
 
                holder = (ViewHolder) convertView.getTag();
            }
 
            holder.ref = position;
 
            holder.textView1.setText(arrText[position]);
            holder.editText1.setText(arrTemp[position]);
            holder.editText1.addTextChangedListener(new TextWatcher() {
 
                @Override
                public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
                    // TODO Auto-generated method stub
 
                }
 
                @Override
                public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                        int arg3) {
                    // TODO Auto-generated method stub
 
                }
 
                @Override
                public void afterTextChanged(Editable arg0) {
                    // TODO Auto-generated method stub
                    arrTemp[holder.ref] = arg0.toString();
                }
            });
 
            return convertView;
        }
 
        private class ViewHolder {
            TextView textView1;
            EditText editText1;
            int ref;
        }
 
 
    }
 
}

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

AndroidManifest.xml

AndroidManifest.xml

 
 
 
 
 
 

ActionScript

 
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
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.testlistview"
    android:versionCode="1"
    android:versionName="1.0" >
 
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />
 
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".ListviewActivity"
            android:screenOrientation="portrait"
            android:windowSoftInputMode="adjustPan">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
 
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
 
</manifest>

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…

listView获取item的Edit内容,listView中的edit内容在滚动时自动赋值了前面的内容的更多相关文章

  1. android 当ListView滚动时自动调用 onCheckedChanged 导致CheckBox 状态不停变化 的解决办法

    今天在做一个含有CheckBox 的ListView时,发现当初始化CheckBox的状态后, 滚动ListView,其中CheckBox 的选中状态不停的发生变化.最后发现原因是 ListView滚 ...

  2. 安卓Android控件ListView获取item中EditText值

    可以明确,现在没有直接方法可以获得ListView中每一行EditText的值. 解决方案:重写BaseAdapter,然后自行获取ListView中每行输入的EditText值. 大概算法:重写Ba ...

  3. Android控件ListView获取item中EditText值

    能够明白,如今没有直接方法能够获得ListView中每一行EditText的值. 解决方式:重写BaseAdapter,然后自行获取ListView中每行输入的EditText值. 大概算法:重写Ba ...

  4. Angular.js数据绑定时自动转义html标签及内容

    angularJS在进行数据绑定时默认是以字符串的形式数据,也就是对你数据中的html标签不进行转义照单全收,这样提高了安全性,防止html标签的注入攻击,但有时候需要,特别是从数据库读取带格式的文本 ...

  5. Xcode中给控件添加颜色时自动显示出颜色

    在iOS开发中,给一些控件设置颜色的时候,设置完不能立马看到颜色.必须要运行程序之后才能看到设置的颜色,如果颜色有偏差再回代码改参数,然后再运行看颜色很是麻烦.令人高兴得是Xcode有很多功能强大插件 ...

  6. SQL中让某一字段更新时自动加1

    update 表 set 字段名=字段名+1 where ....

  7. 安卓activity之间值共享解决办法,tabhost之间共享父类值,字符串类型的转换,获取每一个listview的item

    1.tabhost父类值共享的解决办法 dianzhanliebiao.java是传值页面,zhuyemian.java放的是tabhost,dianzhangaikuang.java是tabhost ...

  8. Delphi在Listview中加入Edit控件

    原帖 : http://www.cnblogs.com/hssbsw/archive/2012/06/03/2533092.html Listview是一个非常有用的控件,我们常常将大量的数据(如数据 ...

  9. android 在 ListView 的 item 中插入 GridView 仿微信朋友圈图片显示。

    转载请声明出处(http://www.cnblogs.com/linguanh/) 先上张效果图: 1,思路简述 这个肯定是要重写 baseAdapter的了,这里我分了两个数据适配器,一个是自定义的 ...

随机推荐

  1. 【javascript基础】6、new与构造函数

    前言 上篇说创建对象的时候提到了带返回值的构造函数,那里没有和大家说这个问题,今天就和大家一起学习构造函数和new操作符.我也是最近才稍微弄明白点这个构造函数,以前总是忽略一些问题,现在就是想到哪块不 ...

  2. Python绑定方法,未绑定方法,类方法,实例方法,静态方法

    >>> class foo(): clssvar=[1,2] def __init__(self): self.instance=[1,2,3] def hehe(self): pr ...

  3. Esfog_UnityShader教程_镜面反射SpecularReflection

    系列教程第四篇,本来打算昨天写的,有些小偷懒就今天写了,这一期我们来讨论一下关于镜面反射的基本原理和具体代码.这一篇是承接着上一篇<Esfog_UnityShader教程_漫反射DiffuseR ...

  4. TortoiseSVN 版本回滚

    尝试用TortoiseSVN进行版本回滚,回滚到的版本和实际的内容有出入,可能是点了太多次给点乱了,囧~ 不过发现一个比较靠谱的方法,如下: 右键点击文件TortoiseSVN->showlog ...

  5. C/C++中的指针数组和数组指针

    1. 指针数组 定义:int *p[n],由于[]的优先级高于*,p和[]结合成一个数组,该数组的元素存储的是int类型的指针,由于数组内容是指针,因此p+1的步长是sizeof(int*),在32位 ...

  6. nginx for linux安装及安装错误解决

    nginx:下载地址:http://www.nginx.org/ 1.GCC编译器 安装指令 :yum  install -y  gcc 如果你所使用的是ubuntu,则安装指令为:apt-get i ...

  7. noip2007解题报告

    T1.统计数字 给出n个数,统计每个数字出现的个数. n小,快排解决. T2.字符串的展开 给出一个字符串,其中形如 d-h,4-9之类的就展开,(前面比后面小的保留,相等也是),三个参数,P1表示大 ...

  8. JSP、HTML标签

    <%@ ...%> 表示是指令,主要用来提供整个JSP 网页相关的信息,并且用来设定JSP网页的相关属性,例如:网页的编码方式.语法.信息等.起始符号为: <%@ 终止符号为: %& ...

  9. mongodb查询返回内嵌符合条件的文档

    db.T_Forum_Thread.find({ "ThreadReply.ReplyContent" : /范甘迪/ }, { "ThreadReply.$" ...

  10. 自定义属性,资源文件attrs.xml

    1.attrs.xml中写:在values文件夹下. <?xml version="1.0" encoding="utf-8"?> <reso ...