Display x and y with SimpleAdapter

38 views Asked by At

I have an application in which I set the range for values with min and max, then I define the step. After I click the button in one column x-values should be displayed and y-values in the other one. For that, I defined two TextView in a GridView. For example, if I set min to 1, max to 3 and step to 0.3 instead of the desired result I get o.5 printed in x column multiple times and a few y-values in another one. What am I doing wrong? And how can I change the output logic to fix this problem? Here is my MainActivity code:

public class MainActivity extends AppCompatActivity {
    EditText minX;
    EditText maxX;
    EditText stepX;
    double min;
    double max;
    double step;
    Set<String> keys;
    HashMap<String, String> hashMap;
    List<HashMap<String, String>> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        minX = findViewById(R.id.minX);
        maxX = findViewById(R.id.maxX);
        stepX = findViewById(R.id.stepX);
    }

    public void onClick(View view){
        min = Double.parseDouble(minX.getText().toString());
        max = Double.parseDouble(maxX.getText().toString());
        step = Double.parseDouble(stepX.getText().toString());

        hashMap = new HashMap<>();
        list = new ArrayList<>();

        while(min <= max){
            hashMap.put(String.valueOf(min), generateY(min));
            list.add(hashMap);
            min += step;
        }

        keys = hashMap.keySet();
        String[] from = keys.toArray(new String[keys.size()]);
        int[] to = {R.id.textView1, R.id.textView2};
        SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), list, R.layout.gridview_layout, from, to);
        GridView gridView = findViewById(R.id.gridView);
        gridView.setAdapter(adapter);
    }

    public String generateY(double x){
        return String.valueOf(1/(1 + x*x));
    }
 }

I would be grateful for any advice as I am only starting to learn Android development

UPDATE: Expected result for the defined above range values:

1.0  0.5
1.3  0.371
1.6  0.281
1.9  0.217
2.2  0.171
2.5  0.138
2.8  0.113
3.1  0.094
0

There are 0 answers