1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
| public class produit extends ActionBarActivity {
ArrayList<String> listItems=new ArrayList<String>();
ArrayAdapter<String> adapter;
HttpURLConnection urlConnection = null;
Spinner sp,sp1;
Button button1;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.produit);
sp=(Spinner)findViewById(R.id.spinner5);
adapter=new ArrayAdapter<String>(this,R.layout.spin_layout,R.id.text,listItems);
sp.setAdapter(adapter);
button1=(Button)findViewById(R.id.button10);
button1.setOnClickListener(new Button.OnClickListener(){
public void onClick(View arg0) {
int index = sp.getSelectedItemPosition();
String message=sp.getSelectedItem().toString();
Toast.makeText(getBaseContext(),
"You have selected item : " + sp.getItemIdAtPosition(index),
Toast.LENGTH_SHORT).show();
Intent intent=new Intent();
intent.putExtra("MESSAGE",message);
intent.putExtra("MESS",index);
setResult(2,intent);
finish();
/*for (int pos = 0; pos<=2;pos++)
{
String message=(String) sp.getItemAtPosition(pos) ;
Intent intent=new Intent();
intent.putExtra("MESSAGE",message);
setResult(2,intent);
finish();
}*/}
});
}
public void onStart(){
super.onStart();
BackTask bt=new BackTask();
bt.execute();
}
private class BackTask extends AsyncTask<Void,Void,Void> {
ArrayList<String> list;
protected void onPreExecute(){
super.onPreExecute();
list=new ArrayList<String>();
}
protected Void doInBackground(Void...params){
InputStream is=null;
String result="";
try{
URL url = new URL("http://monserveur/fichier.php");
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.connect();
is = urlConnection.getInputStream();
}
catch(IOException e){
e.printStackTrace();
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-8"));
String line = null;
while ((line = reader.readLine()) != null) {
result+=line;
}
is.close();
}
catch(Exception e){
e.printStackTrace();
}
try{
JSONArray jArray =new JSONArray(result);
for(int i=0;i<jArray.length();i++){
JSONObject jsonObject=jArray.getJSONObject(i);
list.add(jsonObject.getString("nom_produit"));
}
}
catch(JSONException e){
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Void result){
listItems.addAll(list);
adapter.notifyDataSetChanged();
}
}
} |
Partager