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

android listview edittext, android recyclerview with edittext, Kotlin Generate VCF, android select multiple images from gallery

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.

1. Create a model class

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

/**
 * Created by Parsania Hardik on 03-Jan-17.
 */
public class EditModel {

    private String editTextValue;

    public String getEditTextValue() {
        return editTextValue;
    }

    public void setEditTextValue(String editTextValue) {
        this.editTextValue = editTextValue;
    }
}

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

<?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">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

       <TextView
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_marginTop="12dp"
           android:textColor="#000"
           android:text="TextView:"/>

        <EditText
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:id="@+id/editid"
            android:layout_marginTop="10dp"
            android:paddingLeft="10dp"
            android:textColor="#000"
            android:text="hello"
            />

    </LinearLayout>


    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginTop="10dp"
        android:background="@color/colorAccent"/>

</LinearLayout>

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

import android.content.Context;
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 java.util.ArrayList;

/**
 * Created by Parsania Hardik on 03-Jan-17.
 */
public class CustomeAdapter extends BaseAdapter {

    private Context context;
    public static ArrayList<EditModel> editModelArrayList;

    public CustomeAdapter(Context context, ArrayList<EditModel> editModelArrayList) {

        this.context = context;
        this.editModelArrayList = editModelArrayList;
    }

    @Override
    public int getViewTypeCount() {
        return getCount();
    }
    @Override
    public int getItemViewType(int position) {

        return position;
    }

    @Override
    public int getCount() {
        return editModelArrayList.size();
    }

    @Override
    public Object getItem(int position) {
        return editModelArrayList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;

        if (convertView == null) {
            holder = new ViewHolder();
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.lv_item, null, true);

            holder.editText = (EditText) convertView.findViewById(R.id.editid);

            convertView.setTag(holder);
        }else {
            // the getTag returns the viewHolder object set as a tag to the view
            holder = (ViewHolder)convertView.getTag();
        }

        holder.editText.setText(editModelArrayList.get(position).getEditTextValue());

        holder.editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                    editModelArrayList.get(position).setEditTextValue(holder.editText.getText().toString());

            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        return convertView;
    }

    private class ViewHolder {

        protected EditText editText;

    }

}

Explanation

This class is the heart of the whole tutorial.

The main logic is inside this lines of code

 @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                    editModelArrayList.get(position).setEditTextValue(holder.editText.getText().toString());

 }

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

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.content.Intent;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private Button btn;
    private ListView lv;
    private CustomeAdapter customeAdapter;
    public ArrayList<EditModel> editModelArrayList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lv = (ListView) findViewById(R.id.listView);
        btn = (Button) findViewById(R.id.btn);

        editModelArrayList = populateList();
        customeAdapter = new CustomeAdapter(this,editModelArrayList);
        lv.setAdapter(customeAdapter);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this,NextActivity.class);
                startActivity(intent);
            }
        });

    }

    private ArrayList<EditModel> populateList(){

        ArrayList<EditModel> list = new ArrayList<>();

        for(int i = 0; i < 8; i++){
            EditModel editModel = new EditModel();
            editModel.setEditTextValue(String.valueOf(i));
            list.add(editModel);
        }

        return list;
    }

}

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

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.parsaniahardik.listview_edittext.MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btn"
        android:text="Pass All EditText value to Next Activity"/>
    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/listView"
        android:divider="@null"/>


</LinearLayout>

5. NextActivity Creation

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

Now write following source code into NextActivity.java class

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

public class NextActivity extends AppCompatActivity {

    private TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_next);

        tv = (TextView) findViewById(R.id.tv);

        for (int i = 0; i < CustomeAdapter.editModelArrayList.size(); i++){

                tv.setText(tv.getText() + " " + CustomeAdapter.editModelArrayList.get(i).getEditTextValue() +System.getProperty("line.separator"));

        }


    }
}

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

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
   tools:context="com.example.parsaniahardik.listview_edittext.NextActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:id="@+id/tv"/>

</RelativeLayout>

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.

Download Source Code For Android ListView EditText Tutorial

[sociallocker]Download Listview_edittext Source Code[/sociallocker]