Get item from spinner into url

Posted by ShadowCrowe on Stack Overflow See other posts from Stack Overflow or by ShadowCrowe
Published on 2013-11-08T09:35:12Z Indexed on 2013/11/08 9:53 UTC
Read the original article Hit count: 168

Filed under:
|
|
|

I searched for an answer but couldn't find it.
The problem:
Depending on the selected spinner-item the application should show a different image.
At this moment I can't get it to work.
The Url works like this: "my.site.com/images/" imc_met ".png"
were imc_met is the filename.
I can't get it to work.

Btw the app isn't finished yet

package example.myapplication;

    import java.io.IOException;
    import java.net.MalformedURLException;
    import java.net.URI;
    import java.net.URISyntaxException;
    import java.net.URL;
    import java.util.ArrayList;
    import java.util.List;
    import android.app.Activity;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.AdapterView;
    import android.widget.ArrayAdapter;
    import android.widget.Button;
    import android.widget.ImageView;
    import android.widget.Spinner;


public class itemsActivity extends Activity {

private Spinner spinner1, spinner2;
private Button btnSubmit;
private Bitmap image;
private ImageView imageView;
private String imc_met, imc;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.items);

    addItemsOnSpinner2();
    addListenerOnButton();
    addListenerOnSpinnerItemSelection();
}

// add items into spinner dynamically
public void addItemsOnSpinner2() {

    spinner2 = (Spinner) findViewById(R.id.spinner2);
    List<String> list = new ArrayList<String>();
    list.add("list 1");
    list.add("list 2");
    list.add("list 3");
    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, list);
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner2.setAdapter(dataAdapter);
}

public void addListenerOnSpinnerItemSelection() {
    spinner1 = (Spinner) findViewById(R.id.spinner1);
    spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener());
}

// get the selected dropdown list value
public void addListenerOnButton() {

    spinner1 = (Spinner) findViewById(R.id.spinner1);
    spinner2 = (Spinner) findViewById(R.id.spinner2);
    btnSubmit = (Button) findViewById(R.id.btnSubmit);

    spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                                   int arg2, long arg3) {
            if(spinner1.getSelectedItem()!=null){
                imc_met = spinner1.getSelectedItem().toString();

            }

        }



        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }
    });

imageView = (ImageView)findViewById(R.id.ImageView01);
btnSubmit.setOnClickListener(new OnClickListener() {

    public void onClick(View v)  {
        URL url = null;
        try {
            url = new URL("my.site.com"); //here should the right link appear.
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        try {
            if (url != null) {
                image = BitmapFactory.decodeStream(url.openStream());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        imageView.setImageBitmap(image);
    }
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

© Stack Overflow or respective owner

Related posts about java

Related posts about android