Android Listview Edittext With TextView Get/Set Text Values Of EditText

Android Listview Edittext With TextView Tutorial Example is today’s topic.

We will implement Custom Listview with EditText and will set and get text values of all edittext in all child elements of listview.

Many developers have oftern several problems regarding this topic like:

  1. Value of edittext changes when scrolling
  2. How to set edittext value in listview items
  3. How to retain edittext value while scrolling
  4. How to get all the text values of all the edittexts of listview and pass it to next activity.

Today’s tutorial will give perfect answers and solutions of above problems.

TextWatcher class is used to put logic when user enter text in any Edittext of Listview.

We will pass all the edittext values to next activity so that they can be used as per requirement of app.

Check out the output of Android Listview Edittext With TextView tutorial.

Download Source Code For Android ListView EditText Tutorial

1. Create a model class

Create a new class named “EditModel.java” and copy below code

We will use objects of this model to perfectly retain the edittext value when scrolling listview.

Getter and Setter methods are the key for every mode class. So we have created in this model also.

2. Making ListView Item

Open new resource layout file and give it a name lv_item.xml

This is the complete view of every single row item of listview.

It contains a textview along with edittext.

3. Preparing Custom Adapter

Make a new java class named “CustomeAdapter.java”.

Add following into this class

Explanation

This class is the heart of the whole tutorial.

The main logic is inside this lines of code

When user enter any text value, above method will be called.

Now we are using an ArrayList of the objects of EditModel class. So every single object of EditModel class contains value of edittext.

Now suppose, when user enters the text in the 1st edittext of listview, 1st object from ArrayList is called and the value of edittext is updated in that. Thus when user scrolls the listview, again listview will be populated and updated value of 1st edittext will be printed.

The ArrayList is defined as public static, so that it can be used in any other activity.

4. Changing MainActivity

Change you MainActivity.java class as per below

ListView is populated in MainActivity with the object of customAdapter class.

Button click event is also defined in above class.

Update your activity_main.xml

Add below code

5. NextActivity Creation

Make a new activity and give it a name “NextActivity”

Now write following source code into NextActivity.java class

This class simply prints the text valued of all the edittexts.

We have used ArrayList of the objects of EdiModel class which was defined in CustomAdapter.

Add below into activity_next.xml

Thus, you have seen that implementing a custom listview with edittext and textview is not big deal.

It is also easy to retain the value even when user scrolls the listview.

And finally, getting values of edittexts to any other future activities is also not complex.

Similar Tutorials

If you want learn some other easy listview tutorials then read:

Android ListView CheckBox Example

Android Listview Button Tutorial 

Thanks for reading this tutorial.

Hardik Parsania

I am mobile app developer. I am always passionate about solving complex and real world problems.I have developed many apps with great interest and superior logical skills.

You may also like...

Leave a Reply

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