Android Studio: How to make an EditText in a listview retain the changed value?

285 views Asked by At

I am making a listView and in each item, there is a TextView identifying the field, and an EditText for the user to fill out the value. When I input the text into the EditText, the text just reverts back to what it was before. How do I make it so that it saves the new input? I read an article on StackOverflow of someone with a similar problem and they suggested using a TextOnChanged listener, so I added that as well. When I ran my app, the app just froze. I think I am doing it wrong so it would be great if someone helped me. Thanks.

Here is my code for my custom adapter:

public class DataListAdapter extends ArrayAdapter<DataObject> {

    private Context mContext;
    int mResource;

    public DataListAdapter(Context context, int resource, List<DataObject> objects) {
        super(context, resource, objects);
        mResource = resource;
        mContext = context;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        String field = getItem(position).getField();
        String value = getItem(position).getValue();
        String unit = getItem(position).getUnit();

        DataObject dataObject = new DataObject(field, value, unit);

        LayoutInflater layoutInflater = LayoutInflater.from(mContext);
        convertView = layoutInflater.inflate(mResource, parent, false);

        TextView tvField = (TextView) convertView.findViewById(R.id.Field);
        final EditText tvValue = (EditText) convertView.findViewById(R.id.Value);

        tvField.setText(field);
        tvValue.setText(value);

        //the code I attempted to use to save the changed text
        tvValue.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) {
                String edit = s.toString();
                tvValue.setText(edit);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        return convertView;
    }
}

Here is my code for the main activity for now if needed:

public class MainActivity extends AppCompatActivity {

    ListView list;

    ArrayList<DataObject> data = new ArrayList<>();

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

        list =(ListView) findViewById(R.id.list);

        data.add(new DataObject("Vitamin A", "14", ""));
        data.add(new DataObject("Vitamin B", "15", ""));
        data.add(new DataObject("Vitamin C", "16", ""));


        DataListAdapter dataAdapter = new DataListAdapter(this, R.layout.row, data);
        list.setAdapter(dataAdapter);
    }
}
0

There are 0 answers