EditView value lost on listview scroll

Hi,

In android listview, Its been a big tsk to retain the values on textbox after scroll. In list view when we scroll, listview reset the value of textbox from adapter and hence latest value you entered will lost. So to retain the value of textbox we should update adapter on textbox value change. Please find the example below.

In main_activity.xml

<ListView
    android:id="@+id/lvCenter"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:descendantFocusability="afterDescendants"
    android:divider="#c8c8c8"
    android:dividerHeight="1dp"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:scrollingCache="true"
    android:visibility="visible" />

In item_view.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_vertical"
    android:orientation="horizontal">
<EditView
    android:id="@+id/txt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingBottom="1dp"
    android:paddingLeft="10dp"
    android:paddingTop="1dp"
    android:textColor="#646666"
    android:textSize="12sp"
    android:typeface="sans" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/llText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/chkID" android:orientation="vertical"> <TextView android:id="@+id/txtTitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingBottom="1dp" android:paddingLeft="10dp" android:paddingTop="1dp" android:textColor="@color/darktext" android:textSize="15sp" android:textStyle="bold" android:typeface="sans" /> </LinearLayout> </RelativeLayout>

In Adapter.java

public class CustomAdapter extends BaseAdapter {
    private static ArrayList<FormListItem> itemList;
    private Activity activity;
    private Context ctx;
    private LayoutInflater l_Inflater;
    private int lastFocussedPosition = -1;
    private Handler handler = new Handler();


    public CustomAdapter(Activity a, Context context, ArrayList<CustomListItem> results) {
        itemList = results;
        activity = a;
        l_Inflater = LayoutInflater.from(context);
        ctx = context;
    }

    public int getCount() {
        return itemList.size();
    }

    public Object getItem(int position) {
        return itemList.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

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

        final ViewHolder holder;

        if (convertView == null) {
            convertView = l_Inflater.inflate(R.layout.item_view, null);

            holder = new ViewHolder();
            holder.txt= (EditView) convertView.findViewById(R.id.txt);
            holder.txtTitle = (TextView) convertView.findViewById(R.id.txtTitle);
           
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.txtTitle.setText(itemList.get(position).getTitle());
        holder.txt.setTag(position);
holder.txt.addTextChangedListener(new TextWatcher() {
    @Override
    public void onTextChanged(CharSequence s, int start,
                              int before, int count) {

    }

    @Override
    public void beforeTextChanged(CharSequence s, int start,
                                  int count, int after) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        int pos = (Integer)holder.txt.getTag();
        Toast.makeText(ctx, "Value "+s.toString()+" !"+pos, Toast.LENGTH_SHORT).show();
        itemList.get(pos).setInputVal(s.toString());



    }
});
return convertView; } static class ViewHolder { EditView txt; TextView txtTitle; } }

EditView addTextChangedListner will change the adapter value on input  value change. and it will solve the issue.

Leave a Reply

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