我有一个“ 凯卡马坦 ”变量。
我想将变量( Kecamatan )中的值抛出到 jsonParseDesa.java ,但结果始终是< strong> null 。
从 AutoCompleteTextView 获取变量。
当我成功显示在日志中时,这里有“ Kecamatan ”变量。可以显示值。 代码setAdapter和setOnItemClickListener:
final KecamatanAdapter adapterKecamatan = new KecamatanAdapter(this, autoKecamatan.getText().toString());
autoKecamatan.setAdapter(adapterKecamatan);
autoKecamatan.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
JsonParseKecamatan jps=new JsonParseKecamatan();
Kecamatan selectedSuggestGetSet = adapterKecamatan.getAllUpdatedSuggestion().get(position);
kecamatan = selectedSuggestGetSet.getId().toString();
((JsonParseDesa) XsiswaAddActivity.this.getApplication()).setDistrictId(kecamatan);
Log.d("Kecamatan Result : ", kecamatan);
}
});
代码JsonParseDesa.java:
public class JsonParseDesa extends Application {
double current_latitude,current_longitude;
private String districtId ;
public String getDistrictId() {
return districtId;
}
public void setDistrictId(String idDistrict) {
districtId = idDistrict;
}
public JsonParseDesa(){
}
public JsonParseDesa(double current_latitude, double current_longitude){
this.current_latitude=current_latitude;
this.current_longitude=current_longitude;
}
public List<Desa> getParseJsonDesa(String sName)
{
List<Desa> ListData = new ArrayList<Desa>();
try {
String temp = sName.replace(" ", "%20");
URL js = new URL("http://examplewebservice.com/api/public/village/"+districtId+"/"+temp);
URLConnection jc = js.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(jc.getInputStream()));
String line = reader.readLine();
JSONObject jsonResponse = new JSONObject(line);
JSONArray jsonArray = jsonResponse.getJSONArray("desa_kecamatan_rbg");
for(int i = 0; i < jsonArray.length(); i++){
JSONObject r = jsonArray.getJSONObject(i);
ListData.add(new Desa(r.getString("id"),r.getString("district_id"),r.getString("name")));
}
Log.d("URL Result : ", js.toString());
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return ListData;
}
}
日志结果:
03-21 23:23:31.165 28230-28230/com.example.test.test I/TextInputLayout: EditText added is not a TextInputEditText. Please switch to using that class instead.
03-21 23:23:31.499 28230-28459/com.example.test.test D/OpenGLRenderer: endAllStagingAnimators on 0x558a4aa460 (RippleDrawable) with handle 0x558a53f110
03-21 23:23:38.681 28230-28538/com.example.test.test I/DpmTcmClient: RegisterTcmMonitor from: com.android.okhttp.TcmIdleTimerMonitor
03-21 23:23:39.367 28230-28230/com.example.test.test D/Kecamatan Result :: 3317100
03-21 23:23:39.380 28230-28230/com.example.test.test W/InputEventReceiver: Attempted to finish an input event but the input event receiver has already been disposed.
03-21 23:23:41.995 28230-28541/com.example.test.test D/URL Result :: http://examplewebservice.com/api/public/village/null/S
03-21 23:23:42.071 28230-28541/com.example.test.test D/URL Result :: http://examplewebservice.com/api/public/village/null/Su
03-21 23:23:42.310 28230-28541/com.example.test.test D/URL Result :: http://examplewebservice.com/api/public/village/null/Sum
03-21 23:23:44.026 28230-28541/com.example.test.test D/URL Result :: http://examplewebservice.com/api/public/village/null/Summ
03-21 23:23:44.196 28230-28541/com.example.test.test D/URL Result :: http://examplewebservice.com/api/public/village/null/Summm
为什么字符串districtId为空?代码错误在哪里?谢谢。