ListView with EditText
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".
Where can i download the project?
ReplyDeleteWhere can i download the project?
ReplyDeleteWhen i click the downlpad code diffrentproject apperes not thw listview project it drawble i think
ReplyDeleteHi Ba,
DeleteThanks for your comment.
I fixed it.
https://bayanlarsitesi.com/
ReplyDeleteAltınşehir
Karaköy
Alemdağ
Gürpınar
HECBTB
görüntülü show
ReplyDeleteücretlishow
Nİ2GTH
https://titandijital.com.tr/
ReplyDeletetokat parça eşya taşıma
amasya parça eşya taşıma
adıyaman parça eşya taşıma
hatay parça eşya taşıma
Z5OFNQ
45369
ReplyDeleteAntalya Lojistik
Çanakkale Parça Eşya Taşıma
İzmir Evden Eve Nakliyat
Erzincan Evden Eve Nakliyat
Bingöl Lojistik
A0787
ReplyDeleteYozgat Parça Eşya Taşıma
Kırklareli Lojistik
Ardahan Evden Eve Nakliyat
Sinop Evden Eve Nakliyat
Tokat Evden Eve Nakliyat
Artvin Şehir İçi Nakliyat
Şırnak Şehir İçi Nakliyat
Tekirdağ Evden Eve Nakliyat
Eskişehir Lojistik
04850
ReplyDeleteBursa Şehir İçi Nakliyat
Diyarbakır Şehirler Arası Nakliyat
Adıyaman Parça Eşya Taşıma
Sivas Parça Eşya Taşıma
Erzurum Lojistik
Samsun Şehirler Arası Nakliyat
Karapürçek Boya Ustası
Çerkezköy Marangoz
Silivri Fayans Ustası
2F0EA
ReplyDeleteçanakkale görüntülü sohbet odaları
En İyi Ücretsiz Sohbet Uygulamaları
Samsun Canlı Sohbet
Bayburt En İyi Ücretsiz Görüntülü Sohbet Siteleri
telefonda rastgele sohbet
kütahya görüntülü sohbet odaları
sinop görüntülü sohbet odaları
ısparta Bedava Görüntülü Sohbet Sitesi
goruntulu sohbet
DHBFGHBN
ReplyDeleteشركة كشف تسربات المياه
gtdfgfgyrtyhtrytuhyuy
ReplyDeleteشركة مكافحة حشرات بالاحساء