ListView with EditText

09:00 11 Comments


Placing EditText in ListView giving us some problems like edited text gone, randomly changing the text and focus changing. These all are happening because of the layout reusability.
In this post we are compensate the problems. To compensating the edited text gone problem we are using array list to store the edited text from ListView on the time the particular EditText changed. And we are setting the string from the array to EditText at the time the Layout reusing. This function dynamically happen on the time u scroll the ListView and after EditText changed. You Will Clearly understand at the time you see the code.
Solution
1. First we want to create Array or HashMap to store and retrieving  EditText.
String s[] = new String[]{
        "", "", "", "", "", "", "", "", "", "", "", "", ""
};
2. Now add this syntax in your list adapter.
editText.setText(values[position]);

editText.addTextChangedListener(new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        values[position] = editText.getText().toString();
    }
});
Sample Project
1. Create new project in android studio and edit  activity_main.xml like mentioned below.
activity_main.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=".MainActivity">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/listView"/>
</RelativeLayout>
2. Create new layout list_item.xml.
list_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">
    
    <EditText
        android:layout_width="match_parent"
        android:id="@+id/edit_text"
        android:layout_height="wrap_content" />
</LinearLayout>
3. Edit the MainActivity.java like mentioned below.
MainActivity.javapackage in.spro.listviewedittext;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    String s[] = new String[]{
            "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19"
    };
    final ArrayList<String> list = new ArrayList<>();

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

        for (int i = 0; i < s.length; i++) {
            list.add(s[i]);
        }

        ListView listView = (ListView) findViewById(R.id.listView);
        MyListAdapter adapter = new MyListAdapter(this, s);
        listView.setAdapter(adapter);
    }
}
4. Create adapter class MyListAdapter.java for ListView like mentioned below.
MyListAdapter.javapackage in.spro.listviewedittext;
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.ArrayAdapter;
import android.widget.EditText;

public class MyListAdapter extends ArrayAdapter<String> {
    private final Context context;
    private final String[] values;

    public MyListAdapter(Context context, String[] values) {
        super(context, R.layout.list_item, values);
        this.context = context;
        this.values = values;
    }

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

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View rowView = inflater.inflate(R.layout.list_item, parent, false);

        final EditText editText = (EditText) rowView.findViewById(R.id.edit_text);

        editText.setText(values[position]);

        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                values[position] = editText.getText().toString();
            }
        });

        return rowView;
    }
}
Now run your project and enjoy the "EditText in ListView".

Unknown

Some say he’s half man half fish, others say he’s more of a seventy/thirty split. Either way he’s a Mad

11 comments:

  1. Where can i download the project?

    ReplyDelete
  2. Where can i download the project?

    ReplyDelete
  3. When i click the downlpad code diffrentproject apperes not thw listview project it drawble i think

    ReplyDelete