Save value EditView in Listview on external button click

Hi,

Saving data in android listview on external button click is a tricky task. In list view when we scroll, the value of checkbox or textbox that are not visible on screen will lost. In order to save the values of all the values in list view we need to update the adapter on checkbox click or textbox 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; } }

In MainActivity.java on button click

 

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

btnSave.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View view) {
 SaveData();

 
 }
});
}
public View getViewByPosition(int pos, ListView listView) {
    final int firstListItemPosition = listView.getFirstVisiblePosition();
    final int lastListItemPosition = firstListItemPosition
            + listView.getChildCount() - 1;

    if (pos < firstListItemPosition || pos > lastListItemPosition) {
        return listView.getAdapter().getView(pos, null, listView);
    } else {
        final int childIndex = pos - firstListItemPosition;
        return listView.getChildAt(childIndex);
    }
}

public void SaveData(){
    int itemCount = lv.getCount();   

    for (int i = 0;i<itemCount;i++){
            // View v = lv.getAdapter().getView(i, null, null);
            View v = getViewByPosition(i, lv);
            CheckBox chk = (CheckBox) v.findViewById(R.id.chkAttendance);
            EditText txt = (EditText) v.findViewById(R.id.txtAmount);
            String check = txt.getText().toString();
              ------------ Your Code -----------------
        }
    }




}

Leave a Reply

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