Q:

How to get closest double values from ArrayList in Android

I've a list of double values named valuelist. I have a edittext and a button . when I press the button it will show the closest double value of edittext from valuelist.how to do that ?btw I'm new to app development and dont know any method to use :(
And my codes are:-
List<Double> list = new ArrayList<Double>();
    list.add(1.38);
    list.add(2.56);
    list.add(4.3);

EditText input = (EditText)findViewById(R.id.etinput);
Button convert = (Button)findViewById(R.id.btconvert);
TextView result = (TextView)findViewById(R.id.tvresult);

double ivalue = String.valueOf(result.getText());

so if ivalue is 2.5 then when I press the button it will set result to 2.56 as it is the nearest double value of array list. 
Thanks in advance.

A:

You can iterate over the list and get the difference between the EditText value and the current list item, and the lowest difference value will lead to the nearest item to the EditText value.
List<Double> list = new ArrayList<Double>();
list.add(1.38);
list.add(2.56);
list.add(4.3);

EditText input = (EditText) findViewById(R.id.etinput);
Button convert = (Button) findViewById(R.id.btconvert);
TextView result = (TextView) findViewById(R.id.tvresult);

convert.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        double ivalue = Double.parseDouble(input.getText().toString());
        double difference = Double.MAX_VALUE;
        double nearstItem = Double.MAX_VALUE;
        for (double item : list) {
            if (difference > Math.abs(ivalue - item)) {
                difference = Math.abs(ivalue - item);
                nearstItem = item;
            }
        }

        result.setText(String.valueOf(nearstItem));

    }
});

Make sure to handle the non-double input values to the EditText

