我有一个使用MySQL数据库显示链接微调器Android的项目。我有两张桌子:省和市。省和城市我也有两个Spinner
个。当我选择省时,城市Spinner
应显示与所选省相关的城市。相反,它没有显示任何东西。任何人都可以帮助我吗?
public class MainActivity extends Activity {
private JSONArray data = null;
private static final String TAG_SEMUA_KOMENTAR = "semua_subsektor";
private static final String LINK = "http://10.0.2.2/asp/get_provinsi.php";
private static final String JURU = "http://10.0.2.2/asp/get_kabupaten.php";
ClassJSONParser json = new ClassJSONParser();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Permission StrictMode
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
}
// spinner1
final Spinner spin = (Spinner) findViewById(R.id.spinner1);
final Spinner spin2 = (Spinner) findViewById(R.id.spinner2);
try {
JSONObject obj = json.ambilJsonDariUrl(LINK);
data = obj.getJSONArray(TAG_SEMUA_KOMENTAR);
final ArrayList<HashMap<String, String>> MyArrList = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map;
for (int i = 0; i < data.length(); i++) {
JSONObject c = data.getJSONObject(i);
map = new HashMap<String, String>();
map.put("id_provinsi", c.getString("id_provinsi"));
map.put("nama_provinsi", c.getString("nama_provinsi"));
MyArrList.add(map);
}
SimpleAdapter sAdap;
sAdap = new SimpleAdapter(MainActivity.this, MyArrList,
R.layout.coba,
new String[] { "nama_provinsi" }, new int[] {
R.id.ColName});
spin.setAdapter(sAdap);
spin.clearFocus();
spin.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0,
View selectedItemView, int position, long id) {
try {
//spin.setSelection(position);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("id_provinsi", spin.getSelectedItem().toString()));
JSONObject obj1 = json.membuatHttpRequest(JURU, "GET", params);
data = obj1.getJSONArray(TAG_SEMUA_KOMENTAR);
final ArrayList<HashMap<String, String>> MyArrList = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map;
for (int i = 0; i < data.length(); i++) {
JSONObject c = data.getJSONObject(i);
map = new HashMap<String, String>();
map.put("id_kabupaten", c.getString("id_kabupaten"));
map.put("nama_kabupaten", c.getString("nama_kabupaten"));
map.put("id_provinsi", c.getString("id_provinsi"));
MyArrList.add(map);
}
SimpleAdapter sAdap1;
sAdap1 = new SimpleAdapter(MainActivity.this, MyArrList,
R.layout.coba,
new String[] { "nama_kabupaten" }, new int[] {
R.id.ColName});
spin2.setAdapter(sAdap1);
sAdap1.notifyDataSetChanged();
spin2.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0,
View selectedItemView, int position, long id) {
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this,
"Your Selected : Nothing", Toast.LENGTH_SHORT)
.show();
}
});
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this,
"Your Selected : Nothing", Toast.LENGTH_SHORT)
.show();
}
});
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}