英文:
How to get closest double values from ArrayList in Android
问题
我有一个名为valuelist
的双精度值列表。我有一个edittext
和一个按钮。当我按下按钮时,它将显示来自valuelist
的最接近edittext
的双精度值。怎么做呢?顺便说一下,我是新手应用开发者,不知道要使用哪种方法
我的代码如下:
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());
所以,如果ivalue
是2.5,那么当我按下按钮时,它将将result
设置为2.56,因为它是数组列表中最接近的双精度值。
提前致谢。
英文:
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.
答案1
得分: 0
你可以遍历列表,计算EditText
值与当前列表项的差值,最小的差值将导致最接近EditText
值的项。
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 nearestItem = Double.MAX_VALUE;
for (double item : list) {
if (difference > Math.abs(ivalue - item)) {
difference = Math.abs(ivalue - item);
nearestItem = item;
}
}
result.setText(String.valueOf(nearestItem));
}
});
确保处理EditText
中非double
类型的输入值。
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论