次のコードの問題を見つけるのを手伝ってくれませんか! スピナーからのアイテムが選択された場合、何も起こりません
すなわち: spinner.setOnItemSelectedListener の本体は見られません!
コード:
package com.example.spinner;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
import java.util.HashMap;
import org.apache.http.NameValuePair;
import org.json.JSONException;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.AsyncTask;
public class MainActivity extends Activity {
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> productsList;
// url to get all products list
private static String url_all_products = "http://10.0.2.2/RPM-connect/get_all_patients.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_DOCTOR = "products";
private static final String TAG_ID = "id";
private static final String TAG_F_NAME = "F_name";
private static final String TAG_S_NAME = "S_name";
private static final String TAG_L_NAME = "L_name";
// products JSONArray
JSONArray doctor = null;
ArrayList<String> spinnerArray ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Hashmap for ListView
productsList = new ArrayList<HashMap<String, String>>();
spinnerArray = new ArrayList<String>();
// Loading products in Background Thread
new LoadAllProducts().execute();
Spinner spinner = new Spinner(this);
//ArrayAdapter <HashMap<String, String>> spinnerArrayAdapter = new ArrayAdapter<HashMap<String, String>>(
ArrayAdapter <String> spinnerArrayAdapter = new ArrayAdapter <String >(
this, android.R.layout.simple_spinner_item, spinnerArray );
spinnerArrayAdapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
spinner = (Spinner) findViewById( R.id.spinner1 );
spinner.setAdapter(spinnerArrayAdapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> arg0,
View arg1, int arg2, long arg3) {
Toast.makeText(MainActivity.this, "You have selected : " ,
Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
} );
}
// Response from Edit Product Activity
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// if result code 100
if (resultCode == 100) {
Intent intent = getIntent();
finish();
startActivity(intent);
}
}
/**
* Background Async Task to Load all product by making HTTP Request
* */
@SuppressLint("NewApi")
class LoadAllProducts extends AsyncTask<String, String, String> {
/**
* getting All products from url
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);
// Check your log cat for JSON reponse
Log.d("All Products: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Products
doctor = json.getJSONArray(TAG_DOCTOR);
// looping through All Products
for (int i = 0; i < doctor.length(); i++) {
JSONObject c = doctor.getJSONObject(i);
// Storing each json item in variable
String ID = c.getString(TAG_ID);
String F_name = c.getString(TAG_F_NAME);
String S_name = c.getString(TAG_S_NAME);
String L_name = c.getString(TAG_L_NAME);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_ID, ID);
map.put(TAG_F_NAME, F_name);
map.put(TAG_S_NAME, S_name);
map.put(TAG_L_NAME, L_name);
// adding HashList to ArrayList
productsList.add(map);
Log.d("Before temp", productsList.toString());
spinnerArray.add(F_name + " " + L_name);
// temp[i]= F_name+" "+L_name ;
Log.d("TT",spinnerArray.get(i) );
}
} else {
// no products found
// Launch Add New product Activity
Intent i = new Intent(getApplicationContext(),
MainActivity.class);
// Closing all previous activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
}
}