Java 有人能告诉我为什么我的listView不是';t填充JSON数据
我发现我现在的主要问题是我的意图代码。我不知道如何建立我的意图,使数据从网站回来,所以如果有人可以帮助我,这将是可怕的!我试图从OpenWeatherAPI获取JSON数据,我的应用程序将加载网页,而不是将其放入列表视图。现在,按照我的意图,一个网页打开了,上面有我需要的数据。任何帮助都会很好Java 有人能告诉我为什么我的listView不是';t填充JSON数据,java,android,json,Java,Android,Json,我发现我现在的主要问题是我的意图代码。我不知道如何建立我的意图,使数据从网站回来,所以如果有人可以帮助我,这将是可怕的!我试图从OpenWeatherAPI获取JSON数据,我的应用程序将加载网页,而不是将其放入列表视图。现在,按照我的意图,一个网页打开了,上面有我需要的数据。任何帮助都会很好 public class MainActivity extends AppCompatActivity { private String url = "http://api.op
public class MainActivity extends AppCompatActivity {
private String url = "http://api.openweathermap.org/data/2.5/weather?q=London,us&APPID=805bd9f830268d5274c60e41613c28a5";
private List<String> items = new ArrayList<String>();
private ArrayAdapter<String> mArrayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button btnRequest = (Button) findViewById(R.id.button); //your button id must be button
final ListView lv = (ListView) findViewById(R.id.listView); //your listview ide must be listview
final RequestQueue requestQueue = Volley.newRequestQueue(this);
final JsonObjectRequest jor = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONObject jcity = response.getJSONObject("city");
String name = jcity.getString("name");
JSONArray wlist = response.getJSONArray("list");
for (int i = 0; i < 10; i++) {
JSONObject jsonObject = wlist.getJSONObject(i);
String dt = jsonObject.getString("dt");
JSONArray warray = jsonObject.getJSONArray("weather");
JSONObject wobj = warray.getJSONObject(0);
String desc = wobj.getString("description");
items.add(dt + " " + desc);
Log.d("date", dt); //Log.d prints to the terminal
Log.d("desc", desc);//good for debugging
}
mArrayAdapter = new ArrayAdapter<String>
(getApplicationContext(),
android.R.layout.simple_list_item_1, items);
//lv.setAdapter(mArrayAdapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("Volley", "Error");
}
}
);
btnRequest.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
requestQueue.add(jor);
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
}
}
public类MainActivity扩展了AppCompatActivity{
专用字符串url=”http://api.openweathermap.org/data/2.5/weather?q=London,美国&APPID=805bd9f830268d5274c60e41613c28a5”;
私有列表项=新的ArrayList();
私人ArrayaAdapter MarrayaAdapter;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button btnRequest=(Button)findViewById(R.id.Button);//您的按钮id必须是Button
final ListView lv=(ListView)findViewById(R.id.ListView);//您的ListView ide必须是ListView
final RequestQueue RequestQueue=Volley.newRequestQueue(this);
final JsonObjectRequest jor=新JsonObjectRequest(Request.Method.GET,url,null,
新的Response.Listener(){
@凌驾
公共void onResponse(JSONObject响应){
试一试{
JSONObject jcity=response.getJSONObject(“城市”);
字符串名称=jcity.getString(“名称”);
JSONArray wlist=response.getJSONArray(“列表”);
对于(int i=0;i<10;i++){
JSONObject JSONObject=wlist.getJSONObject(i);
String dt=jsonObject.getString(“dt”);
JSONArray warray=jsonObject.getJSONArray(“天气”);
JSONObject wobj=warray.getJSONObject(0);
String desc=wobj.getString(“说明”);
项目。添加(dt+“”+desc);
Log.d(“date”,dt);//Log.d打印到终端
Log.d(“desc”,desc);//适于调试
}
MarrayaAdapter=新阵列适配器
(getApplicationContext(),
android.R.layout.simple_list_item_1,items);
//低压设置适配器(mArrayAdapter);
}捕获(JSONException e){
e、 printStackTrace();
}
}
},
新的Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
Log.e(“截击”、“错误”);
}
}
);
btnRequest.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
requestQueue.add(jor);
意向i=新意向(意向.行动\视图);
i、 setData(Uri.parse(url));
星触觉(i);
}
});
}
}
我想这是因为您错过了列表视图的设置适配器。还要确保你的物品里面有数据
mArrayAdapter = new ArrayAdapter<String> (getApplicationContext(), android.R.layout.simple_list_item_1, items);
lv.setAdapter(mArrayAdapter);
mArrayAdapter=newarrayadapter(getApplicationContext(),android.R.layout.simple\u list\u item\u 1,items);
低压设置适配器(mArrayAdapter);
我想这是因为您错过了列表视图的设置适配器。还要确保你的物品里面有数据
mArrayAdapter = new ArrayAdapter<String> (getApplicationContext(), android.R.layout.simple_list_item_1, items);
lv.setAdapter(mArrayAdapter);
mArrayAdapter=newarrayadapter(getApplicationContext(),android.R.layout.simple\u list\u item\u 1,items);
低压设置适配器(mArrayAdapter);
我以前确实有lv.setAdapter(mArrayAdapter)在那里,但我试着把它拿出来看看是否会有什么不同,但它没有。即使代码在那里,我的listView也不会被填充。应用程序将运行,当我点击“请求数据”按钮时,它将打开我需要的信息的网页,但它不会将信息带回listViewAdapter,它负责在列表视图中将数据集显示为列表项。所以,如果不设置它,列表将不会填充。另一方面,你在第二条评论中提到的问题完全不同。我建议您一步一步地调试代码,并对所需的功能进行研究。因为我不清楚。我以前确实有lv.setAdapter(MarrayaAdapter)在那里,但是我试着把它拿出来看看它是否会有什么不同,但它没有。即使代码在那里,我的listView也不会被填充。应用程序将运行,当我点击“请求数据”按钮时,它将打开我需要的信息的网页,但它不会将信息带回listViewAdapter,它负责在列表视图中将数据集显示为列表项。所以,如果不设置它,列表将不会填充。另一方面,你在第二条评论中提到的问题完全不同。我建议您一步一步地调试代码,并对所需的功能进行研究。因为我不清楚。