我有 JSON 数据加载到对象中,我无法将它们连接到数组适配器以在多个微调器上显示它们
I have JSON data loaded into objects, I have problem to connect them to array adapter to show them on multiple spinners
因此,'selectcode.java' class 在向 android 微调器填充值时存在一些问题。代码中没有错误,但问题是我无法将 json 文件中的值检索到微调器。所以我认为我在 JSON 对象 <-> 数组适配器之间的某些地方做错了。谁能解决一下
MainActivity.java
package com.example.app;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import org.apache.http.protocol.HTTP;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
String JSON_STRING;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void addstep1(View view){
Intent intent = new Intent(this,addcode.class);
startActivity(intent);
}
public void addstep2(View view){
new BackgroundTask().execute();
Intent intent= new Intent(this,selectcode.class);
intent.putExtra("json_data", JSON_STRING);
startActivity(intent);
}
class BackgroundTask extends AsyncTask<Void, Void, String>{
String json_url="JSON_FILE_URL";
@Override
protected String doInBackground(Void... voids){
try {
URL url= new URL(json_url);
HttpURLConnection httpURLConnection= (HttpURLConnection) url.openConnection();
InputStream inputStream=httpURLConnection.getInputStream();
BufferedReader bufferedReader= new BufferedReader(new InputStreamReader(inputStream));
StringBuilder stringBuilder= new StringBuilder();
while ((JSON_STRING=bufferedReader.readLine())!=null){
stringBuilder.append(JSON_STRING+"\n");
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return stringBuilder.toString().trim();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
JSON_STRING=result;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}
}
Select Code.java
package com.example.app;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class selectcode extends AppCompatActivity {
String JSON_STRING;
List<String> converter = new ArrayList<>();
List<String> zone = new ArrayList<>();
List<String> code = new ArrayList<>();
JSONObject jsonObject;
JSONArray jsonArray;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_selectcode);
Spinner spinner1=(Spinner) findViewById(R.id.selectconverter);
ArrayAdapter<String> adapter1=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, converter);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter1);
Spinner spinner2=(Spinner) findViewById(R.id.selectzone);
ArrayAdapter<String> adapter2=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, zone);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(adapter2);
Spinner spinner3=(Spinner) findViewById(R.id.selectcode);
ArrayAdapter<String> adapter3=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, code);
adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner3.setAdapter(adapter3);
JSON_STRING= getIntent().getExtras().getString("json_data");
String converternumber,zonenumber,codeid;
try {
jsonObject= new JSONObject(JSON_STRING);
int count=0;
jsonArray= jsonObject.getJSONArray("server_response");
while(count<jsonArray.length()){
JSONObject JO= jsonArray.getJSONObject(count);
converternumber= JO.getString("converternumber");
zonenumber= JO.getString("zonenumber");
codeid= JO.getString("codeid");
adapter1.add(converternumber);
adapter2.add(zonenumber);
adapter3.add(codeid);
count++;
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
JSON 数据:
Connected to database{"server_response":[{"converternumber":"0","zonenumber":"","codeid":""},{"converternumber":"1","zonenumber":"23","codeid":"rg"},{"converternumber":"1","zonenumber":"2a","codeid":"test"},{"converternumber":"2","zonenumber":"1a","codeid":"test"},{"converternumber":"1","zonenumber":"1a","codeid":"test 008"},{"converternumber":"1","zonenumber":"2a","codeid":"test001"},{"converternumber":"1","zonenumber":"a1","codeid":"test001"},{"converternumber":"1","zonenumber":"a1","codeid":"test002"},{"converternumber":"1","zonenumber":"a1","codeid":"test003"},{"converternumber":"1","zonenumber":"a1","codeid":"test004"},{"converternumber":"1","zonenumber":"1a","codeid":"test006"},{"converternumber":"1","zonenumber":"1a","codeid":"test007"},{"converternumber":"1","zonenumber":"1a","codeid":"test008"},{"converternumber":"1","zonenumber":"1a","codeid":"test009"},{"converternumber":"1","zonenumber":"1a","codeid":"test010"},{"converternumber":"1","zonenumber":"1a","codeid":"test111"},{"converternumber":"1","zonenumber":"1a","codeid":"test112"},{"converternumber":"1","zonenumber":"12","codeid":"yx"}]}
您必须在添加项目后通知所有 Spinner 适配器。
try {
jsonObject= new JSONObject(JSON_STRING);
int count=0;
jsonArray= jsonObject.getJSONArray("server_response");
while(count<jsonArray.length()){
JSONObject JO= jsonArray.getJSONObject(count);
converternumber= JO.getString("converternumber");
zonenumber= JO.getString("zonenumber");
codeid= JO.getString("codeid");
adapter1.add(converternumber);
adapter2.add(zonenumber);
adapter3.add(codeid);
count++;
}
adapter1.notifyDataSetChanged();
adapter2.notifyDataSetChanged();
adapter3.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
希望对您有所帮助。
尝试使用此代码
adapter1.notifyDataSetChanged();
adapter2.notifyDataSetChanged();
adapter3.notifyDataSetChanged();
因此,'selectcode.java' class 在向 android 微调器填充值时存在一些问题。代码中没有错误,但问题是我无法将 json 文件中的值检索到微调器。所以我认为我在 JSON 对象 <-> 数组适配器之间的某些地方做错了。谁能解决一下
MainActivity.java
package com.example.app;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import org.apache.http.protocol.HTTP;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
String JSON_STRING;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void addstep1(View view){
Intent intent = new Intent(this,addcode.class);
startActivity(intent);
}
public void addstep2(View view){
new BackgroundTask().execute();
Intent intent= new Intent(this,selectcode.class);
intent.putExtra("json_data", JSON_STRING);
startActivity(intent);
}
class BackgroundTask extends AsyncTask<Void, Void, String>{
String json_url="JSON_FILE_URL";
@Override
protected String doInBackground(Void... voids){
try {
URL url= new URL(json_url);
HttpURLConnection httpURLConnection= (HttpURLConnection) url.openConnection();
InputStream inputStream=httpURLConnection.getInputStream();
BufferedReader bufferedReader= new BufferedReader(new InputStreamReader(inputStream));
StringBuilder stringBuilder= new StringBuilder();
while ((JSON_STRING=bufferedReader.readLine())!=null){
stringBuilder.append(JSON_STRING+"\n");
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return stringBuilder.toString().trim();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
JSON_STRING=result;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}
}
Select Code.java
package com.example.app;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class selectcode extends AppCompatActivity {
String JSON_STRING;
List<String> converter = new ArrayList<>();
List<String> zone = new ArrayList<>();
List<String> code = new ArrayList<>();
JSONObject jsonObject;
JSONArray jsonArray;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_selectcode);
Spinner spinner1=(Spinner) findViewById(R.id.selectconverter);
ArrayAdapter<String> adapter1=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, converter);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter1);
Spinner spinner2=(Spinner) findViewById(R.id.selectzone);
ArrayAdapter<String> adapter2=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, zone);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(adapter2);
Spinner spinner3=(Spinner) findViewById(R.id.selectcode);
ArrayAdapter<String> adapter3=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, code);
adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner3.setAdapter(adapter3);
JSON_STRING= getIntent().getExtras().getString("json_data");
String converternumber,zonenumber,codeid;
try {
jsonObject= new JSONObject(JSON_STRING);
int count=0;
jsonArray= jsonObject.getJSONArray("server_response");
while(count<jsonArray.length()){
JSONObject JO= jsonArray.getJSONObject(count);
converternumber= JO.getString("converternumber");
zonenumber= JO.getString("zonenumber");
codeid= JO.getString("codeid");
adapter1.add(converternumber);
adapter2.add(zonenumber);
adapter3.add(codeid);
count++;
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
JSON 数据:
Connected to database{"server_response":[{"converternumber":"0","zonenumber":"","codeid":""},{"converternumber":"1","zonenumber":"23","codeid":"rg"},{"converternumber":"1","zonenumber":"2a","codeid":"test"},{"converternumber":"2","zonenumber":"1a","codeid":"test"},{"converternumber":"1","zonenumber":"1a","codeid":"test 008"},{"converternumber":"1","zonenumber":"2a","codeid":"test001"},{"converternumber":"1","zonenumber":"a1","codeid":"test001"},{"converternumber":"1","zonenumber":"a1","codeid":"test002"},{"converternumber":"1","zonenumber":"a1","codeid":"test003"},{"converternumber":"1","zonenumber":"a1","codeid":"test004"},{"converternumber":"1","zonenumber":"1a","codeid":"test006"},{"converternumber":"1","zonenumber":"1a","codeid":"test007"},{"converternumber":"1","zonenumber":"1a","codeid":"test008"},{"converternumber":"1","zonenumber":"1a","codeid":"test009"},{"converternumber":"1","zonenumber":"1a","codeid":"test010"},{"converternumber":"1","zonenumber":"1a","codeid":"test111"},{"converternumber":"1","zonenumber":"1a","codeid":"test112"},{"converternumber":"1","zonenumber":"12","codeid":"yx"}]}
您必须在添加项目后通知所有 Spinner 适配器。
try {
jsonObject= new JSONObject(JSON_STRING);
int count=0;
jsonArray= jsonObject.getJSONArray("server_response");
while(count<jsonArray.length()){
JSONObject JO= jsonArray.getJSONObject(count);
converternumber= JO.getString("converternumber");
zonenumber= JO.getString("zonenumber");
codeid= JO.getString("codeid");
adapter1.add(converternumber);
adapter2.add(zonenumber);
adapter3.add(codeid);
count++;
}
adapter1.notifyDataSetChanged();
adapter2.notifyDataSetChanged();
adapter3.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
希望对您有所帮助。
尝试使用此代码
adapter1.notifyDataSetChanged();
adapter2.notifyDataSetChanged();
adapter3.notifyDataSetChanged();