我正在为我的客户创建一个Android应用程序,在这个应用程序中,用户必须填写一个表单。我正在使用JSON Web服务,我知道如何发送和接收数据。问题是,当我获取必要的数据来填充表单并填充微调器(我的表单中有多个微调器)时,数据不会填充到微调器,但是我成功从Web服务获取数据。请查看我的代码并善意帮助我,因为我没有太多时间。提前谢谢。
public class AgentActivity extends AppCompatActivity {
Spinner spnr_commodity;
public Spinner spnr_ImpExp;
public Spinner spnr_unit;
public Spinner spnr_terminal;
public Spinner spnr_eta;
public Spinner spnr_lastport;
private String TAG = "Agent";
private TextView txtName_L;
private TextView txtName;
private TextView txtloa;
private TextView txtbeam;
private TextView txtgrt;
private TextView txtnrt;
private TextView txtpcno;
private ImageButton btnSearch;
private SearchView shipSearch;
private String strShipName = "Select Ship Name";
private String str_qty;
private AlphaAnimation buttonClick = new AlphaAnimation(1F, 0.8F);
/*private AnimationSet edtQty = new AnimationSet();*/
EditText a_date;
EditText d_date;
EditText a_time;
EditText qty;
DatePickerDialog datePickerDialog1;
DatePickerDialog datePickerDialog2;
TimePickerDialog timePickerDialog;
ProgressDialog progressDialog;
Toolbar mToolbar;
public String [] arr_impexp= {"Type","Import","Export"};
public String[] arr_lastport={"Select ","KPT","Singapore","China","Dubai","Indonesia","Korea"};
public String[] arr_shipname ;
public String[] arr_terminal;
public String [] arr_commodity ;
ArrayAdapter<String> unitAdapter;
ArrayAdapter<String> terminalAdapter;
ArrayAdapter<String> commodityAdapter;
ArrayAdapter<String> ImpExpAdapter;
ArrayAdapter<String> lastportAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences preference = getApplication().getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
setTitle(preference.getString("nameKey", null));
setContentView(R.layout.agent_activity);
Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
mToolbar.setNavigationIcon(R.drawable.ic_arrow_back_white_24dp);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
mToolbar.setTitleTextColor(Color.WHITE);
mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
txtName_L = (TextView) findViewById(R.id.l_shipName);
txtName = (TextView) findViewById(R.id.n_ship);
txtloa = (TextView) findViewById(R.id.loa);
txtbeam = (TextView) findViewById(R.id.beam);
txtgrt = (TextView) findViewById(R.id.grt);
txtnrt = (TextView) findViewById(R.id.nrt);
txtpcno = (TextView) findViewById(R.id.pc);
btnSearch = (ImageButton) findViewById(R.id.btnsearch);
qty = (EditText) findViewById(R.id.c_qty);
a_date = (EditText) findViewById(R.id.a_date);
d_date = (EditText) findViewById(R.id.d_date);
a_time = (EditText) findViewById(R.id.a_time);
spnr_ImpExp = (Spinner) findViewById(R.id.spnr_ImpExp);
spnr_ImpExp.setPrompt("Type");
spnr_unit = (Spinner) findViewById(R.id.spnr_unit);
spnr_terminal = (Spinner) findViewById(R.id.spnr_terminal);
spnr_eta = (Spinner) findViewById(R.id.spnr_eta);
spnr_lastport = (Spinner) findViewById(R.id.spnr_lastport);
//download data from php
new DownloadInitialInformation().execute("http://192.168.1.131/pqamssql/agent.php");
txtName_L.setText(strShipName);
List<String> list = new ArrayList<>();
list.add("Select Unit ");
list.add("TUE");
list.add("M.Ton");
list.add("C.Met");
list.add("Units");
unitAdapter = new ArrayAdapter<String>(this,R.layout.spinner_item, list);
unitAdapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
spnr_unit.setAdapter(unitAdapter);
ImpExpAdapter = new ArrayAdapter<String>(this, R.layout.spinner_item, arr_impexp);
ImpExpAdapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
spnr_ImpExp.setAdapter(ImpExpAdapter);
txtName_L.setText(strShipName);
// str_qty = qty.getText().toString();
// Toast.makeText(this,"QTY: "+str_qty,Toast.LENGTH_SHORT).show();
txtName.setText("SHIP NAME | " + Constants.getShipName());
if (!Constants.getShipName().equals(" ")) {
Log.i(TAG, "Ship Name : " + Constants.getShipName());
new ShipDetail().execute("http://192.168.1.131/pqamssql/initial.php?shipname=" + Constants.getShipName());
}
spnr_unit.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (position != 0) {
Toast.makeText(parent.getContext(),
"Position: " + position + " OnItemSelectedListener : " + parent.getItemAtPosition(position).toString(),
Toast.LENGTH_SHORT).show();
String str_craftType = parent.getItemAtPosition(position).toString();
//craft.setText(str_craftType);
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
btnSearch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// v.startAnimation(buttonClick);
Intent i = new Intent(AgentActivity.this, AgentListView.class);
startActivity(i);
}
});
// initiate the date picker and a button
// perform click event on edit text
a_date.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// calender class's instance and get current date , month and year from calender
final Calendar c = Calendar.getInstance();
int mYear = c.get(Calendar.YEAR); // current year
int mMonth = c.get(Calendar.MONTH); // current month
int mDay = c.get(Calendar.DAY_OF_MONTH); // current day
// date picker dialog
datePickerDialog1 = new DatePickerDialog(AgentActivity.this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
// set day of month , month and year value in the edit text
a_date.setText(dayOfMonth + "/"
+ (monthOfYear + 1) + "/" + year);
}
}, mYear, mMonth, mDay);
datePickerDialog1.show();
}
});
d_date.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// calender class's instance and get current date , month and year from calender
final Calendar c = Calendar.getInstance();
int mYear = c.get(Calendar.YEAR); // current year
int mMonth = c.get(Calendar.MONTH); // current month
int mDay = c.get(Calendar.DAY_OF_MONTH); // current day
// date picker dialog
datePickerDialog2 = new DatePickerDialog(AgentActivity.this,R.style.agentTheme,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
// set day of month , month and year value in the edit text
d_date.setText(dayOfMonth + "/"
+ (monthOfYear + 1) + "/" + year);
}
}, mYear, mMonth, mDay);
datePickerDialog2.show();
}
});
a_time.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Calendar c = Calendar.getInstance();
// Get the current hour and minute
int hour = c.get(Calendar.HOUR_OF_DAY);
int mint = c.get(Calendar.MINUTE);
timePickerDialog = new TimePickerDialog(AgentActivity.this,R.style.AppTheme_Light_Dialog, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
a_time.setText(hourOfDay + ":" + minute);
}
}, hour, mint,true);
timePickerDialog.show();
}
});
}
class ShipDetail extends AsyncTask<String, String, String[]> {
HttpURLConnection connection = null;
BufferedReader reader = null;
@Override
protected String[] doInBackground(String... params) {
Log.i(TAG, "URL : " + params[0]);
String result1;
String result2;
String result3;
String result4;
String result5;
String[] resultArray = new String[5];
try {
URL url = new URL(params[0]);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
Log.i(TAG, "Connected");
InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
Log.i(TAG, "Data Read");
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
String json = buffer.toString();
Log.i(TAG, "LINE ## : " + json);
if (json.equals("No Data Found")) {
resultArray[0] = "No Data Found";
} else {
JSONObject shipdetailObject = new JSONObject(json);
JSONObject parentKey = shipdetailObject.getJSONObject("detail");
result1 = parentKey.getString("loa");
result2 = parentKey.getString("beam");
result3 = parentKey.getString("grt");
result4 = parentKey.getString("nrt");
result5 = parentKey.getString("pcno");
resultArray[0] = result1;
resultArray[1] = result2;
resultArray[2] = result3;
resultArray[3] = result4;
resultArray[4] = result5;
return resultArray;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return resultArray;
}
@Override
protected void onPostExecute(String[] s) {
super.onPostExecute(s);
if (s[0].equals("No Data Found")) {
Toast.makeText(AgentActivity.this, "" + s[0], Toast.LENGTH_SHORT).show();
} else {
String str_loa = s[0];
String str_beam = s[1];
String str_nrt = s[2];
String str_grt = s[3];
String str_pcno = s[4];
Log.i(TAG, "Extracted From JSON LOA: " + str_loa);
Log.i(TAG, "Extracted From JSON BEAM: " + str_beam);
Log.i(TAG, "Extracted From JSON GRT: " + str_grt);
Log.i(TAG, "Extracted From JSON NRT: " + str_nrt);
Log.i(TAG, "Extracted From JSON PCNO: " + str_pcno);
txtloa.setText("L.O.A |" + str_loa);
txtbeam.setText("G.R.T |" + str_beam);
txtgrt.setText("N.R.T |" + str_grt);
txtnrt.setText("BEAM |" + str_nrt);
txtpcno.setText("P.C No. |" + str_pcno);
}
}
}
//downloading initial detail
class DownloadInitialInformation extends AsyncTask<String, String, String> {
HttpURLConnection connection = null;
BufferedReader reader = null;
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(AgentActivity.this,
R.style.AppTheme_Dark_Dialog);
// progressDialog.setIndeterminate(true);
progressDialog.setMessage("Downloading...");
progressDialog.show();
}
@Override
protected String doInBackground(String... params) {
Log.i(TAG, "URL : " + params[0]);
//Boolean flag= new Boolean(false);
String flag = "";
try {
URL url = new URL(params[0]);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
Log.i(TAG, "Connected");
InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
Log.i(TAG, "Data Read");
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
String json = buffer.toString();
Log.i(TAG, "LINE ## : " + json);
if (json.equals("No Data Found")) {
flag="N";
}
else{
JSONObject parentObject = new JSONObject(json);
JSONArray parentShipArray = parentObject.getJSONArray("Ship");
JSONArray parentTerminalArray = parentObject.getJSONArray("Terminals");
JSONArray parentCommodityArray = parentObject.getJSONArray("Commodity");
int v_len,t_len,c_len;
v_len = parentShipArray.length();
t_len = parentTerminalArray.length();
c_len = parentCommodityArray.length();
arr_shipname = new String[v_len];
arr_terminal = new String[t_len];
arr_commodity = new String[c_len];
Log.i(TAG, "Ship Length : " + v_len);
Log.i(TAG, "T Length : " + t_len);
Log.i(TAG, "C Length : " + c_len);
//getting ships
for (int i = 0; i < v_len; i++) {
JSONObject ship_json = parentShipArray.getJSONObject(i);
arr_shipname[i] = ship_json.getString("v");
Log.i(TAG, "Ship name : " + arr_shipname[i]);
Log.i(TAG, "Vessel name : "+arr_shipname[2]);
}
//getting terminals
for (int i = 0; i < t_len; i++) {
JSONObject terminal_json = parentTerminalArray.getJSONObject(i);
arr_terminal[i] = terminal_json.getString("t");
Log.i(TAG, "Terminal : " + arr_terminal[i]);
}
//getting commodity
for (int i = 0; i < c_len; i++) {
JSONObject commdty_json = parentCommodityArray.getJSONObject(i);
// arr_commodity.add(commdty_json.getString("c"));
arr_commodity[i] = commdty_json.getString("c");
Log.i(TAG, "Commodity : " + arr_commodity[i]);
}
flag = "Y";
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return flag;
}
@Override
protected void onPostExecute(String downloadStat) {
super.onPostExecute(downloadStat);
Log.i(TAG, "download stat : " + downloadStat);
if (downloadStat.equals("Y")) {
//set all textViews and spinner
// Log.i(TAG, "Commodity : " + arr_commodity[1]);
spnr_commodity = (Spinner) findViewById(R.id.spnr_commodity);
commodityAdapter = new ArrayAdapter<String>(AgentActivity.this, android.R.layout.simple_spinner_item,arr_commodity);
commodityAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//Log.i(TAG, "Commodity : " + arr_commodity);
spnr_commodity.setAdapter(commodityAdapter);
terminalAdapter = new ArrayAdapter<String>(AgentActivity.this, android.R.layout.simple_spinner_item, arr_terminal);
terminalAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnr_terminal.setAdapter(terminalAdapter);
//Log.i(TAG, "Terminal : " + arr_terminal.toString());
lastportAdapter = new ArrayAdapter<String>(AgentActivity.this,android. R.layout.simple_spinner_item, arr_lastport);
lastportAdapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
spnr_lastport.setAdapter(lastportAdapter);
//Log.i(TAG, "Last Port : " + arr_lastport.toString());
} else {
Toast.makeText(AgentActivity.this, "No Data Found", Toast.LENGTH_SHORT).show();
}
progressDialog.dismiss();
}
}
} // End of AgentActivity
*LogCat to Check populated array*
11-14 09:44:18.267 19660-19660/com.portqasim.personal.testinglayout I/TERMINAL﹕ (QICT)-1.1
11-14 09:44:18.267 19660-19660/com.portqasim.personal.testinglayout I/TERMINAL﹕ (QICT)-1.2
11-14 09:44:18.267 19660-19660/com.portqasim.personal.testinglayout I/TERMINAL﹕ (QICT)-2.1
11-14 09:44:18.267 19660-19660/com.portqasim.personal.testinglayout I/TERMINAL﹕ (QICT)-2.2
11-14 09:44:18.268 19660-19660/com.portqasim.personal.testinglayout I/TERMINAL﹕ EVTL-13
11-14 09:44:18.268 19660-19660/com.portqasim.personal.testinglayout I/TERMINAL﹕ FAP GT
11-14 09:44:18.268 19660-19660/com.portqasim.personal.testinglayout I/TERMINAL﹕ FOTCO OIL TERMINAL
11-14 09:44:18.268 19660-19660/com.portqasim.personal.testinglayout I/TERMINAL﹕ IOCB
11-14 09:44:18.268 19660-19660/com.portqasim.personal.testinglayout I/TERMINAL﹕ LCT
11-14 09:44:18.269 19660-19660/com.portqasim.personal.testinglayout I/TERMINAL﹕ MW-1
11-14 09:44:18.269 19660-19660/com.portqasim.personal.testinglayout I/TERMINAL﹕ MW-2
11-14 09:44:18.269 19660-19660/com.portqasim.personal.testinglayout I/TERMINAL﹕ MW-3
11-14 09:44:18.269 19660-19660/com.portqasim.personal.testinglayout I/TERMINAL﹕ MW-4
11-14 09:44:18.269 19660-19660/com.portqasim.personal.testinglayout I/TERMINAL﹕ SSGC/LPG
答案 0 :(得分:0)
我无法在xml文件中看到您的微调器。但是我遇到了类似的问题,并且在我在xml中的spinner中添加条目时修复了。我的所有适配器和微调器项都可以,但是如果不添加xml文件中的条目,就不会在微调器中显示。
答案 1 :(得分:0)
尝试在获取列表中的所有值后将适配器设置为微调器,调试代码并确保列表中包含数据,然后再将适配器设置为微调器。
答案 2 :(得分:0)
只需将微调器模式从对话框更改为下拉列表即可成功解决问题
以前
机器人:spinnerMode =&#34;对话框&#34;
现在
机器人:spinnerMode =&#34;下拉&#34;
之前的xml就在这里
正确的xml在这里