Spinners – get item selected from array

I have a spinner that gets populated from a text file stored on a web server. The contents of this text file are then stored in an ArrayList. My app is going to have the user add an item to this text file that they name themselves and therefore update the spinner. What I need to be able to do is have the spinner do something when an item is selected. As the user can give any name to an item they add, how can my app do something when that particular item is selected from the spinner if it doesn’t know what they named it?

Right now I have my app set up so that if spinner item equals “string” do this… but this obviously won’t work if the user has named an item themselves. I hope I have explained my question ok! This is my code so far:

public class MainActivity extends AppCompatActivity {

    String statusLink = "http://redacted.uk/pmt/status.txt";
    String deviceLink = "http://redacted.uk/pmt/devices.txt";
    String status;
    final String degree = "u00b0";

    ArrayList<String> devicesAL = new ArrayList<>();

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

        Thread thread = new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    // Set up connection to device.txt on web server
                    URL deviceUrl = new URL (deviceLink);
                    URLConnection deviceConn = deviceUrl.openConnection();
                    deviceConn.setDoOutput(true);
                    deviceConn.connect();

                    InputStream dis = deviceConn.getInputStream();
                    InputStreamReader disr = new InputStreamReader(dis, "UTF-8");
                    BufferedReader dbr = new BufferedReader(disr);

                    String deviceLine;

                    // Set up connection to status.txt on web server
                    URL statusUrl = new URL(statusLink);
                    URLConnection statusConn = statusUrl.openConnection();
                    statusConn.setDoOutput(true);
                    statusConn.connect();

                    InputStream sis = statusConn.getInputStream();
                    InputStreamReader sisr = new InputStreamReader(sis, "UTF-8");
                    BufferedReader sbr = new BufferedReader(sisr);

                    String statusLine;

                    try {
                        while ((deviceLine = dbr.readLine()) != null) {
                            //System.out.println(deviceLine);
                            devicesAL.add(deviceLine);

                            for (String str : devicesAL) {
                                System.out.println(str);
                            }
                        }

                        while ((statusLine = sbr.readLine()) != null) {
                            System.out.println(statusLine);
                            status = statusLine;

                            System.out.println("Status = " + status);
                            TextView output = (TextView) findViewById(R.id.textView);
                            System.out.println(status);
                        }

                        for (String str : devicesAL) {
                            System.out.println(str);
                        }

                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                //LOAD SPINNER
                                Spinner spinner = (Spinner) findViewById(R.id.spinner);
                                ArrayAdapter adp = new ArrayAdapter(MainActivity.this, android.R.layout.simple_spinner_item, devicesAL);
                                adp.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                                spinner.setAdapter(adp);
                                adp.notifyDataSetChanged();
                                spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

                                    @Override
                                    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                                        TextView output = (TextView) findViewById(R.id.textView);

                                        if (parent.getItemAtPosition(position).equals("Water Cooler")) {
                                            System.out.println("Water cooler selected");
                                            output.setText(status);
                                        }
                                    }

                                    @Override
                                    public void onNothingSelected(AdapterView<?> parent) {

                                    }
                                });
                            }
                        });
                    } finally {
                        sbr.close();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

        thread.start();
    }
}

1
Leave a Reply

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Jason Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Jason
Guest

Since you say you want to : If the user then selects “fridge” from the spinner, the data inside fridge.txt gets displayed So i think you can just get the file name from the spinner then show the content. It will be like this : spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { String selectedFileName = parent.getItemAtPosition(position); File sdcard = Environment.getExternalStorageDirectory(); File file = new File(sdcard, selectedFileName+".txt"); //Read text from file StringBuilder text = new StringBuilder(); try { BufferedReader br = new BufferedReader(new FileReader(file)); String line; while ((line = br.readLine()) != null) {… Read more »