Edittext in listview android example

Here I have written a simple step to create edittext in listview. Please follow the step to do this task.

Edittext in listview demo
Edittext in Listview Demo

Steps to create edittext in listview android

  • Create Listview with EditText
  • Setup data into Listview
  • Configure the Edittext
  • Add Textwatcher to Edittext

Related Post

Swipelistview in android

Listview item animation in android

customize listview android


Create Listview with EditText

Create simple main.xml with listview and set listview properties for the fast listview scrolling.

<ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="wrap_content" 
android:animationCache="false" 
android:scrollingCache="false" 
android:smoothScrollbar="true" > 
</ListView>

Create listview_adapter.xml adapter layout with edittext.

<?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="60dp"
    android:orientation="horizontal"
    android:padding="5dp">

    <TextView
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Enter Random Number"
        android:gravity="center_vertical"
        android:layout_weight="0.5" />

    <EditText
        android:id="@+id/number"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:inputType="number"
        android:layout_weight="1.5"
        android:ems="10" />

</LinearLayout>

Setup data into Listview

Create ArrayList add data into the ArrayList.

lv=(ListView)findViewById(R.id.listView1);
 lv.setItemsCanFocus(true);
 for(int i=0;i>=30;i++){
 list.add(i);
 }

Configure the Edittext

  1. create an adapter for the listview and set the position as a tag for the edittext.
  2. Normally, when scrolling the item position will change. So, you have to get the edittext tag and set it into the edittext id.from that you can avoid the change of the item position.
holder.caption = (EditText) convertView
.findViewById(R.id.editText12);
holder.caption.setTag(position);
holder.caption.setText(list.get(position).toString());
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
int tag_position=(Integer) holder.caption.getTag();
holder.caption.setId(tag_position);

Add Textwatcher to Edittext

Finally, add the text watcher to the edittext and store the changes into the correct position in the list.

holder.caption.addTextChangedListener(new TextWatcher() {
@Override
 public void onTextChanged(CharSequence s, int start, int before,
 int count) {
 final int position2 = holder.caption.getId();
 final EditText Caption = (EditText) holder.caption;
 if(Caption.getText().toString().length()&amp;amp;amp;amp;amp;amp;amp;gt;0){
 list.set(position2,Integer.parseInt(Caption.getText().toString()));
 }else{
 Toast.makeText(getApplicationContext(), "Please enter some value", Toast.LENGTH_SHORT).show();
 }
 }
 
@Override
 public void beforeTextChanged(CharSequence s, int start, int count,
 int after) {
 // TODO Auto-generated method stub
 }
 
@Override
 public void afterTextChanged(Editable s) {
 
 }
 
});

Screenshot

Edittext in listview
Download

5 thoughts on - Edittext in listview android example

Leave a Reply

Your email address will not be published. Required fields are marked *