如何将 Json 数据放入来自 URL 的微调器中?
How to put Json data in spinners from URL?
我正在文本框中获取 json 我试图将这些数据放入微调器但无法这样做。下面是我的主要 activity class 我正在使用 Volley.
public class Mainactivity extends Activity {
private String urlJsonArry = "https://www.abc.json";
private static String TAG = MainActivity.class.getSimpleName();
private Button btnMakeArrayRequest;
// Progress dialog
private ProgressDialog pDialog;
private TextView txtResponse,txtResponse2,txtResponse3,txtResponse4;
// temporary string to show the parsed response
private String jsonResponse, jsonResponse2, jsonResponse3, jsonResponse4;
Spinner spinner;
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.spinners);
spinner= (Spinner) findViewById(R.id.spinner);
btnMakeArrayRequest = (Button) findViewById(R.id.btnArrayRequest);
txtResponse = (TextView) findViewById(R.id.txtResponse);
txtResponse2 = (TextView) findViewById(R.id.txtResponse2);
txtResponse3 = (TextView) findViewById(R.id.txtResponse3);
// txtResponse4 = (TextView) findViewById(R.id.txtResponse4);
pDialog = new ProgressDialog(this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
btnMakeArrayRequest.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// making json array request
makeJsonArrayRequest();
}
});
}
private void makeJsonArrayRequest () {
showpDialog();
JsonObjectRequest req = new JsonObjectRequest(urlJsonArry,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, response.toString());
try {
JSONArray array = response.getJSONArray("results");
// Parsing json array response
// loop through each json object
jsonResponse = "";
for (int i = 0; i < array.length(); i++) {
JSONObject person = (JSONObject) array
.get(i);
System.out.println(person.toString());
String syllabus = person.getString("grade");
String grade = person.getString("grade");
String subject = person.getString("subject");
jsonResponse += "Board: " + syllabus + "\n\n";
jsonResponse2 += "Class: " + grade + "\n\n";
jsonResponse3 += "Subject: " + subject + "\n\n";
// jsonResponse4 += "ID: " + id + "\n\n";
//Getting Compile time error here
adapter= new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,array);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);}
txtResponse.setText(jsonResponse);
txtResponse2.setText(jsonResponse2);
txtResponse3.setText(jsonResponse3);
//txtResponse4.setText(jsonResponse4);
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(),
"Error: " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
hidepDialog();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_SHORT).show();
hidepDialog();
}
});
您需要使用 Activity 上下文 this
不是 activity 上下文 尝试这样
adapter= new ArrayAdapter<String>(Mainactivity.this, android.R.layout.simple_spinner_item, array);
编辑:
更改数组格式。例如,在您的微调器中,您将显示主题,因此请执行以下操作
String[] mySubject = new String[array.length()];
for (int i = 0; i < array.length(); i++) {
JSONObject person = (JSONObject) array
.get(i);
System.out.println(person.toString());
String syllabus = person.getString("grade");
String grade = person.getString("grade");
String subject = person.getString("subject");
jsonResponse += "Board: " + syllabus + "\n\n";
jsonResponse2 += "Class: " + grade + "\n\n";
jsonResponse3 += "Subject: " + subject + "\n\n";
// jsonResponse4 += "ID: " + id + "\n\n";
mySubject[i] = subject;
}
adapter= new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, mySubject);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
注意:
您还需要将 set 适配器移到 for 循环之外。
您正在为 JsonObjectRequest 回调在内部 class 中填充适配器,因此这指的是匿名内部 class(new Response.Listener<JSONObject>()
)。要解决此问题,您可以更改行:
adapter= new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,array);
到
adapter= new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item,array);
我正在文本框中获取 json 我试图将这些数据放入微调器但无法这样做。下面是我的主要 activity class 我正在使用 Volley.
public class Mainactivity extends Activity {
private String urlJsonArry = "https://www.abc.json";
private static String TAG = MainActivity.class.getSimpleName();
private Button btnMakeArrayRequest;
// Progress dialog
private ProgressDialog pDialog;
private TextView txtResponse,txtResponse2,txtResponse3,txtResponse4;
// temporary string to show the parsed response
private String jsonResponse, jsonResponse2, jsonResponse3, jsonResponse4;
Spinner spinner;
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.spinners);
spinner= (Spinner) findViewById(R.id.spinner);
btnMakeArrayRequest = (Button) findViewById(R.id.btnArrayRequest);
txtResponse = (TextView) findViewById(R.id.txtResponse);
txtResponse2 = (TextView) findViewById(R.id.txtResponse2);
txtResponse3 = (TextView) findViewById(R.id.txtResponse3);
// txtResponse4 = (TextView) findViewById(R.id.txtResponse4);
pDialog = new ProgressDialog(this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
btnMakeArrayRequest.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// making json array request
makeJsonArrayRequest();
}
});
}
private void makeJsonArrayRequest () {
showpDialog();
JsonObjectRequest req = new JsonObjectRequest(urlJsonArry,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, response.toString());
try {
JSONArray array = response.getJSONArray("results");
// Parsing json array response
// loop through each json object
jsonResponse = "";
for (int i = 0; i < array.length(); i++) {
JSONObject person = (JSONObject) array
.get(i);
System.out.println(person.toString());
String syllabus = person.getString("grade");
String grade = person.getString("grade");
String subject = person.getString("subject");
jsonResponse += "Board: " + syllabus + "\n\n";
jsonResponse2 += "Class: " + grade + "\n\n";
jsonResponse3 += "Subject: " + subject + "\n\n";
// jsonResponse4 += "ID: " + id + "\n\n";
//Getting Compile time error here
adapter= new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,array);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);}
txtResponse.setText(jsonResponse);
txtResponse2.setText(jsonResponse2);
txtResponse3.setText(jsonResponse3);
//txtResponse4.setText(jsonResponse4);
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(),
"Error: " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
hidepDialog();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_SHORT).show();
hidepDialog();
}
});
您需要使用 Activity 上下文 this
不是 activity 上下文 尝试这样
adapter= new ArrayAdapter<String>(Mainactivity.this, android.R.layout.simple_spinner_item, array);
编辑:
更改数组格式。例如,在您的微调器中,您将显示主题,因此请执行以下操作
String[] mySubject = new String[array.length()];
for (int i = 0; i < array.length(); i++) {
JSONObject person = (JSONObject) array
.get(i);
System.out.println(person.toString());
String syllabus = person.getString("grade");
String grade = person.getString("grade");
String subject = person.getString("subject");
jsonResponse += "Board: " + syllabus + "\n\n";
jsonResponse2 += "Class: " + grade + "\n\n";
jsonResponse3 += "Subject: " + subject + "\n\n";
// jsonResponse4 += "ID: " + id + "\n\n";
mySubject[i] = subject;
}
adapter= new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, mySubject);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
注意: 您还需要将 set 适配器移到 for 循环之外。
您正在为 JsonObjectRequest 回调在内部 class 中填充适配器,因此这指的是匿名内部 class(new Response.Listener<JSONObject>()
)。要解决此问题,您可以更改行:
adapter= new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,array);
到
adapter= new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item,array);