Edittext in listview android example
Here I have written simple step to create Listview with edittext .Please follow the step to do this task.
Step1 :
Create simple main.xml with listview
and set listview properties for the fast listview scrolling.
<ListView
android:id="@+id/listView1"android:layout_height="wrap_content" android:animationCache="false" android:scrollingCache="false" android:smoothScrollbar="true" >
</ListView>
|
Step 2:
Create ArrayList add data into the
arraylist.
lv=(ListView)findViewById(R.id.listView1); lv.setItemsCanFocus(true); for(int i=0;i<30 data-blogger-escaped-br="" data-blogger-escaped-i=""> list.add(i); } |
Step
3:
1)create adapter for
the listview and set the position as 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); |
Step
4:
Finally,add
the text watcher to the edittext and store the changes into 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()>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:
DOWNLOAD FULL SOURCE CODE
2 comments:
Thanks for the valuable code,
but facing one problem on scrolling list value edit text is loses its content.
Please help
Thanks
You have to use your Own view and Save your view while scrolling... Like
private Map myViews = new HashMap();
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = myViews.get(position);
if (view == null) {
myViews.put(position, view);
}
Post a Comment